1
- import { describe , expect , it } from 'vitest'
1
+ import { describe , expect , it , vi } from 'vitest'
2
+ import { QueryClient } from '@tanstack/query-core'
3
+ import { sleep } from '@tanstack/query-test-utils'
4
+ import { fireEvent } from '@testing-library/react'
2
5
import { mutationOptions } from '../mutationOptions'
6
+ import { useIsMutating , useMutation , useMutationState } from '..'
7
+ import { renderWithClient } from './utils'
8
+ import type { MutationState } from '@tanstack/query-core'
3
9
4
10
describe ( 'mutationOptions' , ( ) => {
5
11
it ( 'should return the object received as a parameter without any modification.' , ( ) => {
@@ -10,4 +16,113 @@ describe('mutationOptions', () => {
10
16
11
17
expect ( mutationOptions ( object ) ) . toStrictEqual ( object )
12
18
} )
19
+
20
+ it ( 'should return the number of fetching mutations when used with useIsMutating' , async ( ) => {
21
+ const isMutatingArray : Array < number > = [ ]
22
+ const queryClient = new QueryClient ( )
23
+
24
+ function IsMutating ( ) {
25
+ const isMutating = useIsMutating ( )
26
+ isMutatingArray . push ( isMutating )
27
+ return null
28
+ }
29
+
30
+ const mutationOpts = mutationOptions ( {
31
+ mutationKey : [ 'key' ] ,
32
+ mutationFn : ( ) => sleep ( 50 ) . then ( ( ) => 'data' ) ,
33
+ } )
34
+
35
+ function Mutation ( ) {
36
+ const { mutate } = useMutation ( mutationOpts )
37
+
38
+ return (
39
+ < div >
40
+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
41
+ </ div >
42
+ )
43
+ }
44
+
45
+ function Page ( ) {
46
+ return (
47
+ < div >
48
+ < IsMutating />
49
+ < Mutation />
50
+ </ div >
51
+ )
52
+ }
53
+
54
+ const rendered = renderWithClient ( queryClient , < Page /> )
55
+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
56
+
57
+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 0 ] ) . toEqual ( 0 ) )
58
+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 1 ] ) . toEqual ( 1 ) )
59
+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 2 ] ) . toEqual ( 0 ) )
60
+ await vi . waitFor ( ( ) =>
61
+ expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 ) ,
62
+ )
63
+ } )
64
+
65
+ it ( 'should return the number of fetching mutations when used with queryClient.isMutating' , async ( ) => {
66
+ const isMutatingArray : Array < number > = [ ]
67
+ const queryClient = new QueryClient ( )
68
+
69
+ const mutationOpts = mutationOptions ( {
70
+ mutationKey : [ 'mutation' ] ,
71
+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data' ) ,
72
+ } )
73
+
74
+ function Mutation ( ) {
75
+ const isMutating = queryClient . isMutating ( mutationOpts )
76
+ const { mutate } = useMutation ( mutationOpts )
77
+ isMutatingArray . push ( isMutating )
78
+
79
+ return (
80
+ < div >
81
+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
82
+ </ div >
83
+ )
84
+ }
85
+
86
+ const rendered = renderWithClient ( queryClient , < Mutation /> )
87
+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
88
+
89
+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 0 ] ) . toEqual ( 0 ) )
90
+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 1 ] ) . toEqual ( 1 ) )
91
+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 2 ] ) . toEqual ( 0 ) )
92
+ await vi . waitFor ( ( ) =>
93
+ expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 ) ,
94
+ )
95
+ } )
96
+
97
+ it ( 'should return the number of fetching mutations when used with useMutationState' , async ( ) => {
98
+ const mutationStateArray : Array <
99
+ MutationState < unknown , Error , unknown , unknown >
100
+ > = [ ]
101
+ const queryClient = new QueryClient ( )
102
+
103
+ const mutationOpts = mutationOptions ( {
104
+ mutationKey : [ 'mutation' ] ,
105
+ mutationFn : ( ) => Promise . resolve ( 'data' ) ,
106
+ } )
107
+
108
+ function Mutation ( ) {
109
+ const { mutate } = useMutation ( mutationOpts )
110
+ const data = useMutationState ( {
111
+ filters : { ...mutationOpts , status : 'success' } ,
112
+ } )
113
+ mutationStateArray . push ( ...data )
114
+
115
+ return (
116
+ < div >
117
+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
118
+ </ div >
119
+ )
120
+ }
121
+
122
+ const rendered = renderWithClient ( queryClient , < Mutation /> )
123
+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
124
+
125
+ await vi . waitFor ( ( ) => expect ( mutationStateArray . length ) . toEqual ( 1 ) )
126
+ await vi . waitFor ( ( ) => expect ( mutationStateArray [ 0 ] ?. data ) . toEqual ( 'data' ) )
127
+ } )
13
128
} )
0 commit comments