@@ -26,26 +26,22 @@ describe('useIsMutating', () => {
26
26
27
27
function IsMutating ( ) {
28
28
const isMutating = useIsMutating ( )
29
+
29
30
createRenderEffect ( ( ) => {
30
31
isMutatingArray . push ( isMutating ( ) )
31
32
} )
33
+
32
34
return null
33
35
}
34
36
35
37
function Mutations ( ) {
36
38
const { mutate : mutate1 } = useMutation ( ( ) => ( {
37
39
mutationKey : [ 'mutation1' ] ,
38
- mutationFn : async ( ) => {
39
- await sleep ( 150 )
40
- return 'data'
41
- } ,
40
+ mutationFn : ( ) => sleep ( 150 ) . then ( ( ) => 'data' ) ,
42
41
} ) )
43
42
const { mutate : mutate2 } = useMutation ( ( ) => ( {
44
43
mutationKey : [ 'mutation2' ] ,
45
- mutationFn : async ( ) => {
46
- await sleep ( 50 )
47
- return 'data'
48
- } ,
44
+ mutationFn : ( ) => sleep ( 50 ) . then ( ( ) => 'data' ) ,
49
45
} ) )
50
46
51
47
createEffect ( ( ) => {
@@ -74,6 +70,7 @@ describe('useIsMutating', () => {
74
70
) )
75
71
76
72
await vi . advanceTimersByTimeAsync ( 150 )
73
+
77
74
expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 2 , 1 , 0 ] )
78
75
} )
79
76
@@ -83,26 +80,22 @@ describe('useIsMutating', () => {
83
80
84
81
function IsMutating ( ) {
85
82
const isMutating = useIsMutating ( ( ) => ( { mutationKey : [ 'mutation1' ] } ) )
83
+
86
84
createRenderEffect ( ( ) => {
87
85
isMutatingArray . push ( isMutating ( ) )
88
86
} )
87
+
89
88
return null
90
89
}
91
90
92
91
function Page ( ) {
93
92
const { mutate : mutate1 } = useMutation ( ( ) => ( {
94
93
mutationKey : [ 'mutation1' ] ,
95
- mutationFn : async ( ) => {
96
- await sleep ( 100 )
97
- return 'data'
98
- } ,
94
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
99
95
} ) )
100
96
const { mutate : mutate2 } = useMutation ( ( ) => ( {
101
97
mutationKey : [ 'mutation2' ] ,
102
- mutationFn : async ( ) => {
103
- await sleep ( 100 )
104
- return 'data'
105
- } ,
98
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
106
99
} ) )
107
100
108
101
createEffect ( ( ) => {
@@ -121,6 +114,7 @@ describe('useIsMutating', () => {
121
114
122
115
// Unlike React, IsMutating Wont re-render twice with mutation2
123
116
await vi . advanceTimersByTimeAsync ( 100 )
117
+
124
118
expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] )
125
119
} )
126
120
@@ -133,26 +127,22 @@ describe('useIsMutating', () => {
133
127
predicate : ( mutation ) =>
134
128
mutation . options . mutationKey ?. [ 0 ] === 'mutation1' ,
135
129
} ) )
130
+
136
131
createRenderEffect ( ( ) => {
137
132
isMutatingArray . push ( isMutating ( ) )
138
133
} )
134
+
139
135
return null
140
136
}
141
137
142
138
function Page ( ) {
143
139
const { mutate : mutate1 } = useMutation ( ( ) => ( {
144
140
mutationKey : [ 'mutation1' ] ,
145
- mutationFn : async ( ) => {
146
- await sleep ( 100 )
147
- return 'data'
148
- } ,
141
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
149
142
} ) )
150
143
const { mutate : mutate2 } = useMutation ( ( ) => ( {
151
144
mutationKey : [ 'mutation2' ] ,
152
- mutationFn : async ( ) => {
153
- await sleep ( 100 )
154
- return 'data'
155
- } ,
145
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
156
146
} ) )
157
147
158
148
createEffect ( ( ) => {
@@ -171,6 +161,7 @@ describe('useIsMutating', () => {
171
161
172
162
// Again, No unnecessary re-renders like React
173
163
await vi . advanceTimersByTimeAsync ( 100 )
164
+
174
165
expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] )
175
166
} )
176
167
@@ -182,16 +173,17 @@ describe('useIsMutating', () => {
182
173
const { mutate } = useMutation (
183
174
( ) => ( {
184
175
mutationKey : [ 'mutation1' ] ,
185
- mutationFn : async ( ) => {
186
- await sleep ( 10 )
187
- return 'data'
188
- } ,
176
+ mutationFn : ( ) => sleep ( 20 ) . then ( ( ) => 'data' ) ,
189
177
} ) ,
190
178
( ) => queryClient ,
191
179
)
180
+
192
181
createEffect ( ( ) => {
193
- mutate ( )
182
+ setActTimeout ( ( ) => {
183
+ mutate ( )
184
+ } , 10 )
194
185
} )
186
+
195
187
return (
196
188
< div >
197
189
< div > mutating: { isMutating ( ) } </ div >
@@ -201,8 +193,11 @@ describe('useIsMutating', () => {
201
193
202
194
const rendered = render ( ( ) => < Page > </ Page > )
203
195
204
- await vi . advanceTimersByTimeAsync ( 0 )
196
+ expect ( rendered . getByText ( 'mutating: 0' ) ) . toBeInTheDocument ( )
197
+ await vi . advanceTimersByTimeAsync ( 10 )
205
198
expect ( rendered . getByText ( 'mutating: 1' ) ) . toBeInTheDocument ( )
199
+ await vi . advanceTimersByTimeAsync ( 20 )
200
+ expect ( rendered . getByText ( 'mutating: 0' ) ) . toBeInTheDocument ( )
206
201
} )
207
202
208
203
// eslint-disable-next-line vitest/expect-expect
@@ -231,12 +226,10 @@ describe('useIsMutating', () => {
231
226
232
227
function Page ( ) {
233
228
const [ mounted , setMounted ] = createSignal ( true )
229
+
234
230
const { mutate : mutate1 } = useMutation ( ( ) => ( {
235
231
mutationKey : [ 'mutation1' ] ,
236
- mutationFn : async ( ) => {
237
- await sleep ( 10 )
238
- return 'data'
239
- } ,
232
+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
240
233
} ) )
241
234
242
235
createEffect ( ( ) => {
@@ -258,6 +251,7 @@ describe('useIsMutating', () => {
258
251
< Page />
259
252
</ QueryClientProvider >
260
253
) )
254
+
261
255
fireEvent . click ( rendered . getByText ( 'unmount' ) )
262
256
263
257
// Should not display the console error
0 commit comments