1
- import { describe , expect , test , vi } from 'vitest'
2
- import { waitFor } from '@testing-library/dom'
1
+ import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
3
2
import { MutationCache , MutationObserver } from '..'
4
3
import { createQueryClient , executeMutation , queryKey , sleep } from './utils'
5
4
6
5
describe ( 'mutationCache' , ( ) => {
6
+ beforeEach ( ( ) => {
7
+ vi . useFakeTimers ( )
8
+ } )
9
+
10
+ afterEach ( ( ) => {
11
+ vi . useRealTimers ( )
12
+ } )
13
+
7
14
describe ( 'MutationCacheConfig error callbacks' , ( ) => {
8
15
test ( 'should call onError and onSettled when a mutation errors' , async ( ) => {
9
16
const key = queryKey ( )
@@ -49,12 +56,12 @@ describe('mutationCache', () => {
49
56
const states : Array < number > = [ ]
50
57
const onError = async ( ) => {
51
58
states . push ( 1 )
52
- await sleep ( 1 )
59
+ await vi . advanceTimersByTimeAsync ( 1 )
53
60
states . push ( 2 )
54
61
}
55
62
const onSettled = async ( ) => {
56
63
states . push ( 5 )
57
- await sleep ( 1 )
64
+ await vi . advanceTimersByTimeAsync ( 1 )
58
65
states . push ( 6 )
59
66
}
60
67
const testCache = new MutationCache ( { onError, onSettled } )
@@ -68,19 +75,20 @@ describe('mutationCache', () => {
68
75
mutationFn : ( ) => Promise . reject ( new Error ( 'error' ) ) ,
69
76
onError : async ( ) => {
70
77
states . push ( 3 )
71
- await sleep ( 1 )
78
+ await vi . advanceTimersByTimeAsync ( 1 )
72
79
states . push ( 4 )
73
80
} ,
74
81
onSettled : async ( ) => {
75
82
states . push ( 7 )
76
- await sleep ( 1 )
83
+ await vi . advanceTimersByTimeAsync ( 1 )
77
84
states . push ( 8 )
78
85
} ,
79
86
} ,
80
87
'vars' ,
81
88
)
82
89
} catch { }
83
90
91
+ await vi . runAllTimersAsync ( )
84
92
expect ( states ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )
85
93
} )
86
94
} )
@@ -139,7 +147,7 @@ describe('mutationCache', () => {
139
147
const testCache = new MutationCache ( { onSuccess, onSettled } )
140
148
const testClient = createQueryClient ( { mutationCache : testCache } )
141
149
142
- await executeMutation (
150
+ executeMutation (
143
151
testClient ,
144
152
{
145
153
mutationKey : key ,
@@ -157,6 +165,7 @@ describe('mutationCache', () => {
157
165
} ,
158
166
'vars' ,
159
167
)
168
+ await vi . runAllTimersAsync ( )
160
169
161
170
expect ( states ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )
162
171
} )
@@ -195,7 +204,7 @@ describe('mutationCache', () => {
195
204
const testCache = new MutationCache ( { onMutate } )
196
205
const testClient = createQueryClient ( { mutationCache : testCache } )
197
206
198
- await executeMutation (
207
+ executeMutation (
199
208
testClient ,
200
209
{
201
210
mutationKey : key ,
@@ -208,6 +217,7 @@ describe('mutationCache', () => {
208
217
} ,
209
218
'vars' ,
210
219
)
220
+ await vi . runAllTimersAsync ( )
211
221
212
222
expect ( states ) . toEqual ( [ 1 , 2 , 3 , 4 ] )
213
223
} )
@@ -288,7 +298,7 @@ describe('mutationCache', () => {
288
298
const testCache = new MutationCache ( )
289
299
const testClient = createQueryClient ( { mutationCache : testCache } )
290
300
const onSuccess = vi . fn ( )
291
- await executeMutation (
301
+ executeMutation (
292
302
testClient ,
293
303
{
294
304
mutationKey : [ 'a' , 1 ] ,
@@ -298,10 +308,8 @@ describe('mutationCache', () => {
298
308
} ,
299
309
1 ,
300
310
)
301
-
302
311
expect ( testCache . getAll ( ) ) . toHaveLength ( 1 )
303
- await sleep ( 10 )
304
- await waitFor ( ( ) => {
312
+ await vi . waitFor ( ( ) => {
305
313
expect ( testCache . getAll ( ) ) . toHaveLength ( 0 )
306
314
} )
307
315
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
@@ -318,12 +326,12 @@ describe('mutationCache', () => {
318
326
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 0 )
319
327
observer . mutate ( 1 )
320
328
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 1 )
321
- await sleep ( 10 )
329
+ await vi . advanceTimersByTimeAsync ( 10 )
322
330
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 1 )
323
331
unsubscribe ( )
324
332
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 1 )
325
- await sleep ( 10 )
326
- await waitFor ( ( ) => {
333
+ await vi . advanceTimersByTimeAsync ( 10 )
334
+ await vi . waitFor ( ( ) => {
327
335
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 0 )
328
336
} )
329
337
} )
@@ -343,12 +351,12 @@ describe('mutationCache', () => {
343
351
observer . mutate ( 1 )
344
352
unsubscribe ( )
345
353
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 1 )
346
- await sleep ( 10 )
354
+ await vi . advanceTimersByTimeAsync ( 10 )
347
355
// unsubscribe should not remove even though gcTime has elapsed b/c mutation is still pending
348
356
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 1 )
349
- await sleep ( 10 )
357
+ await vi . advanceTimersByTimeAsync ( 10 )
350
358
// should be removed after an additional gcTime wait
351
- await waitFor ( ( ) => {
359
+ await vi . waitFor ( ( ) => {
352
360
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 0 )
353
361
} )
354
362
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
@@ -367,7 +375,7 @@ describe('mutationCache', () => {
367
375
const unsubscribe = observer . subscribe ( ( ) => undefined )
368
376
observer . mutate ( 1 )
369
377
unsubscribe ( )
370
- await waitFor ( ( ) => {
378
+ await vi . waitFor ( ( ) => {
371
379
expect ( queryClient . getMutationCache ( ) . getAll ( ) ) . toHaveLength ( 0 )
372
380
} )
373
381
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
0 commit comments