1
- import { beforeEach , describe , expect , test , vi } from 'vitest'
1
+ import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
2
2
import { reactive , ref } from 'vue-demi'
3
3
import { sleep } from '@tanstack/query-test-utils'
4
4
import { useMutation } from '../useMutation'
@@ -7,6 +7,13 @@ import { useQueryClient } from '../useQueryClient'
7
7
vi . mock ( '../useQueryClient' )
8
8
9
9
describe ( 'useMutation' , ( ) => {
10
+ beforeEach ( ( ) => {
11
+ vi . useFakeTimers ( )
12
+ } )
13
+ afterEach ( ( ) => {
14
+ vi . useRealTimers ( )
15
+ } )
16
+
10
17
test ( 'should be in idle state initially' , ( ) => {
11
18
const mutation = useMutation ( {
12
19
mutationFn : ( params ) => sleep ( 0 ) . then ( ( ) => params ) ,
@@ -41,10 +48,10 @@ describe('useMutation', () => {
41
48
test ( 'should return error when request fails' , async ( ) => {
42
49
const mutation = useMutation ( {
43
50
mutationFn : ( ) =>
44
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
51
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
45
52
} )
46
53
mutation . mutate ( )
47
- await sleep ( 10 )
54
+ await vi . advanceTimersByTimeAsync ( 10 )
48
55
expect ( mutation ) . toMatchObject ( {
49
56
isIdle : { value : false } ,
50
57
isPending : { value : false } ,
@@ -58,12 +65,12 @@ describe('useMutation', () => {
58
65
test ( 'should return data when request succeeds' , async ( ) => {
59
66
const result = 'Mock data'
60
67
const mutation = useMutation ( {
61
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
68
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
62
69
} )
63
70
64
71
mutation . mutate ( result )
65
72
66
- await sleep ( 10 )
73
+ await vi . advanceTimersByTimeAsync ( 10 )
67
74
68
75
expect ( mutation ) . toMatchObject ( {
69
76
isIdle : { value : false } ,
@@ -80,15 +87,15 @@ describe('useMutation', () => {
80
87
const mutationCache = queryClient . getMutationCache ( )
81
88
const options = reactive ( {
82
89
mutationKey : [ 'foo' ] ,
83
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
90
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
84
91
} )
85
92
const mutation = useMutation ( options )
86
93
87
94
options . mutationKey = [ 'bar' ]
88
- await sleep ( 0 )
95
+ await vi . advanceTimersByTimeAsync ( 10 )
89
96
mutation . mutate ( 'xyz' )
90
97
91
- await sleep ( 0 )
98
+ await vi . advanceTimersByTimeAsync ( 10 )
92
99
93
100
const mutations = mutationCache . find ( { mutationKey : [ 'bar' ] } )
94
101
@@ -112,15 +119,15 @@ describe('useMutation', () => {
112
119
const mutationCache = queryClient . getMutationCache ( )
113
120
const options = reactive ( {
114
121
mutationKey,
115
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
122
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
116
123
} )
117
124
const mutation = useMutation ( options )
118
125
119
126
mutationKey . value [ 0 ] ! . otherObject . name = 'someOtherObjectName'
120
- await sleep ( 0 )
127
+ await vi . advanceTimersByTimeAsync ( 10 )
121
128
mutation . mutate ( 'xyz' )
122
129
123
- await sleep ( 0 )
130
+ await vi . advanceTimersByTimeAsync ( 10 )
124
131
125
132
const mutations = mutationCache . getAll ( )
126
133
const relevantMutation = mutations . find ( ( m ) => {
@@ -147,12 +154,12 @@ describe('useMutation', () => {
147
154
let proof = false
148
155
mutationFn . value = ( params : string ) => {
149
156
proof = true
150
- return sleep ( 0 ) . then ( ( ) => params )
157
+ return sleep ( 10 ) . then ( ( ) => params )
151
158
}
152
- await sleep ( 0 )
159
+ await vi . advanceTimersByTimeAsync ( 10 )
153
160
154
161
mutation . mutate ( 'xyz' )
155
- await sleep ( 0 )
162
+ await vi . advanceTimersByTimeAsync ( 10 )
156
163
157
164
const mutations = mutationCache . find ( { mutationKey : [ 'bar2' ] } )
158
165
expect ( mutations ?. options . mutationKey ) . toEqual ( [ 'bar2' ] )
@@ -162,12 +169,12 @@ describe('useMutation', () => {
162
169
test ( 'should reset state after invoking mutation.reset' , async ( ) => {
163
170
const mutation = useMutation ( {
164
171
mutationFn : ( ) =>
165
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
172
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
166
173
} )
167
174
168
175
mutation . mutate ( )
169
176
170
- await sleep ( 10 )
177
+ await vi . advanceTimersByTimeAsync ( 10 )
171
178
172
179
mutation . reset ( )
173
180
@@ -189,13 +196,13 @@ describe('useMutation', () => {
189
196
test ( 'should call onMutate when passed as an option' , async ( ) => {
190
197
const onMutate = vi . fn ( )
191
198
const mutation = useMutation ( {
192
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
199
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
193
200
onMutate,
194
201
} )
195
202
196
203
mutation . mutate ( '' )
197
204
198
- await sleep ( 10 )
205
+ await vi . advanceTimersByTimeAsync ( 10 )
199
206
200
207
expect ( onMutate ) . toHaveBeenCalledTimes ( 1 )
201
208
} )
@@ -204,41 +211,41 @@ describe('useMutation', () => {
204
211
const onError = vi . fn ( )
205
212
const mutation = useMutation ( {
206
213
mutationFn : ( ) =>
207
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
214
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
208
215
onError,
209
216
} )
210
217
211
218
mutation . mutate ( '' )
212
219
213
- await sleep ( 10 )
220
+ await vi . advanceTimersByTimeAsync ( 10 )
214
221
215
222
expect ( onError ) . toHaveBeenCalledTimes ( 1 )
216
223
} )
217
224
218
225
test ( 'should call onSuccess when passed as an option' , async ( ) => {
219
226
const onSuccess = vi . fn ( )
220
227
const mutation = useMutation ( {
221
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
228
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
222
229
onSuccess,
223
230
} )
224
231
225
232
mutation . mutate ( '' )
226
233
227
- await sleep ( 10 )
234
+ await vi . advanceTimersByTimeAsync ( 10 )
228
235
229
236
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
230
237
} )
231
238
232
239
test ( 'should call onSettled when passed as an option' , async ( ) => {
233
240
const onSettled = vi . fn ( )
234
241
const mutation = useMutation ( {
235
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
242
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
236
243
onSettled,
237
244
} )
238
245
239
246
mutation . mutate ( '' )
240
247
241
- await sleep ( 10 )
248
+ await vi . advanceTimersByTimeAsync ( 10 )
242
249
243
250
expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
244
251
} )
@@ -247,38 +254,38 @@ describe('useMutation', () => {
247
254
const onError = vi . fn ( )
248
255
const mutation = useMutation ( {
249
256
mutationFn : ( ) =>
250
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
257
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
251
258
} )
252
259
253
260
mutation . mutate ( undefined , { onError } )
254
261
255
- await sleep ( 10 )
262
+ await vi . advanceTimersByTimeAsync ( 10 )
256
263
257
264
expect ( onError ) . toHaveBeenCalledTimes ( 1 )
258
265
} )
259
266
260
267
test ( 'should call onSuccess when passed as an argument of mutate function' , async ( ) => {
261
268
const onSuccess = vi . fn ( )
262
269
const mutation = useMutation ( {
263
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
270
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
264
271
} )
265
272
266
273
mutation . mutate ( '' , { onSuccess } )
267
274
268
- await sleep ( 10 )
275
+ await vi . advanceTimersByTimeAsync ( 10 )
269
276
270
277
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
271
278
} )
272
279
273
280
test ( 'should call onSettled when passed as an argument of mutate function' , async ( ) => {
274
281
const onSettled = vi . fn ( )
275
282
const mutation = useMutation ( {
276
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
283
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
277
284
} )
278
285
279
286
mutation . mutate ( '' , { onSettled } )
280
287
281
- await sleep ( 10 )
288
+ await vi . advanceTimersByTimeAsync ( 10 )
282
289
283
290
expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
284
291
} )
@@ -287,13 +294,13 @@ describe('useMutation', () => {
287
294
const onSettled = vi . fn ( )
288
295
const onSettledOnFunction = vi . fn ( )
289
296
const mutation = useMutation ( {
290
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
297
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
291
298
onSettled,
292
299
} )
293
300
294
301
mutation . mutate ( '' , { onSettled : onSettledOnFunction } )
295
302
296
- await sleep ( 10 )
303
+ await vi . advanceTimersByTimeAsync ( 10 )
297
304
298
305
expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
299
306
expect ( onSettledOnFunction ) . toHaveBeenCalledTimes ( 1 )
@@ -308,10 +315,12 @@ describe('useMutation', () => {
308
315
test ( 'should resolve properly' , async ( ) => {
309
316
const result = 'Mock data'
310
317
const mutation = useMutation ( {
311
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
318
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
312
319
} )
313
320
314
- await expect ( mutation . mutateAsync ( result ) ) . resolves . toBe ( result )
321
+ await vi . waitFor ( ( ) =>
322
+ expect ( mutation . mutateAsync ( result ) ) . resolves . toBe ( result ) ,
323
+ )
315
324
316
325
expect ( mutation ) . toMatchObject ( {
317
326
isIdle : { value : false } ,
@@ -326,10 +335,12 @@ describe('useMutation', () => {
326
335
test ( 'should throw on error' , async ( ) => {
327
336
const mutation = useMutation ( {
328
337
mutationFn : ( ) =>
329
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
338
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
330
339
} )
331
340
332
- await expect ( mutation . mutateAsync ( ) ) . rejects . toThrowError ( 'Some error' )
341
+ await vi . waitFor ( ( ) =>
342
+ expect ( mutation . mutateAsync ( ) ) . rejects . toThrowError ( 'Some error' ) ,
343
+ )
333
344
334
345
expect ( mutation ) . toMatchObject ( {
335
346
isIdle : { value : false } ,
@@ -347,15 +358,13 @@ describe('useMutation', () => {
347
358
const err = new Error ( 'Expected mock error. All is well!' )
348
359
const boundaryFn = vi . fn ( )
349
360
const { mutate } = useMutation ( {
350
- mutationFn : ( ) => {
351
- return Promise . reject ( err )
352
- } ,
361
+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => Promise . reject ( err ) ) ,
353
362
throwOnError : boundaryFn ,
354
363
} )
355
364
356
365
mutate ( )
357
366
358
- await sleep ( 0 )
367
+ await vi . advanceTimersByTimeAsync ( 10 )
359
368
360
369
expect ( boundaryFn ) . toHaveBeenCalledTimes ( 1 )
361
370
expect ( boundaryFn ) . toHaveBeenCalledWith ( err )
0 commit comments