1
1
import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
2
- import { waitFor } from '@testing-library/dom'
3
2
import { MutationObserver } from '../mutationObserver'
4
3
import { createQueryClient , executeMutation , queryKey , sleep } from './utils'
5
4
import type { QueryClient } from '..'
@@ -9,12 +8,14 @@ describe('mutations', () => {
9
8
let queryClient : QueryClient
10
9
11
10
beforeEach ( ( ) => {
11
+ vi . useFakeTimers ( )
12
12
queryClient = createQueryClient ( )
13
13
queryClient . mount ( )
14
14
} )
15
15
16
16
afterEach ( ( ) => {
17
17
queryClient . clear ( )
18
+ vi . useRealTimers ( )
18
19
} )
19
20
20
21
test ( 'mutate should accept null values' , async ( ) => {
@@ -54,10 +55,7 @@ describe('mutations', () => {
54
55
55
56
test ( 'mutation should set correct success states' , async ( ) => {
56
57
const mutation = new MutationObserver ( queryClient , {
57
- mutationFn : async ( text : string ) => {
58
- await sleep ( 10 )
59
- return text
60
- } ,
58
+ mutationFn : ( text : string ) => sleep ( 10 ) . then ( ( ) => text ) ,
61
59
onMutate : ( text ) => text ,
62
60
} )
63
61
@@ -87,7 +85,7 @@ describe('mutations', () => {
87
85
88
86
mutation . mutate ( 'todo' )
89
87
90
- await sleep ( 0 )
88
+ await vi . advanceTimersByTimeAsync ( 0 )
91
89
92
90
expect ( states [ 0 ] ) . toEqual ( {
93
91
context : undefined ,
@@ -107,7 +105,7 @@ describe('mutations', () => {
107
105
submittedAt : expect . any ( Number ) ,
108
106
} )
109
107
110
- await sleep ( 5 )
108
+ await vi . advanceTimersByTimeAsync ( 5 )
111
109
112
110
expect ( states [ 1 ] ) . toEqual ( {
113
111
context : 'todo' ,
@@ -127,7 +125,7 @@ describe('mutations', () => {
127
125
submittedAt : expect . any ( Number ) ,
128
126
} )
129
127
130
- await sleep ( 20 )
128
+ await vi . advanceTimersByTimeAsync ( 20 )
131
129
132
130
expect ( states [ 2 ] ) . toEqual ( {
133
131
context : 'todo' ,
@@ -150,10 +148,8 @@ describe('mutations', () => {
150
148
151
149
test ( 'mutation should set correct error states' , async ( ) => {
152
150
const mutation = new MutationObserver ( queryClient , {
153
- mutationFn : async ( _ : string ) => {
154
- await sleep ( 20 )
155
- return Promise . reject ( new Error ( 'err' ) )
156
- } ,
151
+ mutationFn : ( _ : string ) =>
152
+ sleep ( 20 ) . then ( ( ) => Promise . reject ( new Error ( 'err' ) ) ) ,
157
153
onMutate : ( text ) => text ,
158
154
retry : 1 ,
159
155
retryDelay : 1 ,
@@ -167,7 +163,7 @@ describe('mutations', () => {
167
163
168
164
mutation . mutate ( 'todo' ) . catch ( ( ) => undefined )
169
165
170
- await sleep ( 0 )
166
+ await vi . advanceTimersByTimeAsync ( 0 )
171
167
172
168
expect ( states [ 0 ] ) . toEqual ( {
173
169
context : undefined ,
@@ -187,7 +183,7 @@ describe('mutations', () => {
187
183
submittedAt : expect . any ( Number ) ,
188
184
} )
189
185
190
- await sleep ( 10 )
186
+ await vi . advanceTimersByTimeAsync ( 10 )
191
187
192
188
expect ( states [ 1 ] ) . toEqual ( {
193
189
context : 'todo' ,
@@ -207,7 +203,7 @@ describe('mutations', () => {
207
203
submittedAt : expect . any ( Number ) ,
208
204
} )
209
205
210
- await sleep ( 20 )
206
+ await vi . advanceTimersByTimeAsync ( 20 )
211
207
212
208
expect ( states [ 2 ] ) . toEqual ( {
213
209
context : 'todo' ,
@@ -227,7 +223,7 @@ describe('mutations', () => {
227
223
submittedAt : expect . any ( Number ) ,
228
224
} )
229
225
230
- await sleep ( 30 )
226
+ await vi . advanceTimersByTimeAsync ( 30 )
231
227
232
228
expect ( states [ 3 ] ) . toEqual ( {
233
229
context : 'todo' ,
@@ -405,7 +401,7 @@ describe('mutations', () => {
405
401
} ,
406
402
} )
407
403
408
- await waitFor ( ( ) => expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 ) )
404
+ await vi . waitFor ( ( ) => expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 ) )
409
405
410
406
expect ( onSuccess ) . toHaveBeenCalledWith ( 2 )
411
407
} )
@@ -417,7 +413,7 @@ describe('mutations', () => {
417
413
418
414
const results : Array < string > = [ ]
419
415
420
- const execute1 = executeMutation (
416
+ executeMutation (
421
417
queryClient ,
422
418
{
423
419
mutationKey : key1 ,
@@ -441,7 +437,7 @@ describe('mutations', () => {
441
437
isPaused : false ,
442
438
} )
443
439
444
- const execute2 = executeMutation (
440
+ executeMutation (
445
441
queryClient ,
446
442
{
447
443
mutationKey : key2 ,
@@ -465,7 +461,7 @@ describe('mutations', () => {
465
461
isPaused : true ,
466
462
} )
467
463
468
- await Promise . all ( [ execute1 , execute2 ] )
464
+ await vi . runAllTimersAsync ( )
469
465
470
466
expect ( results ) . toStrictEqual ( [
471
467
'start-A' ,
@@ -482,7 +478,7 @@ describe('mutations', () => {
482
478
483
479
const results : Array < string > = [ ]
484
480
485
- const execute1 = executeMutation (
481
+ executeMutation (
486
482
queryClient ,
487
483
{
488
484
mutationKey : key1 ,
@@ -496,7 +492,7 @@ describe('mutations', () => {
496
492
'vars1' ,
497
493
)
498
494
499
- const execute2 = executeMutation (
495
+ executeMutation (
500
496
queryClient ,
501
497
{
502
498
mutationKey : key2 ,
@@ -510,7 +506,7 @@ describe('mutations', () => {
510
506
'vars2' ,
511
507
)
512
508
513
- await Promise . all ( [ execute1 , execute2 ] )
509
+ await vi . runAllTimersAsync ( )
514
510
515
511
expect ( results ) . toStrictEqual ( [
516
512
'start-A' ,
@@ -523,7 +519,7 @@ describe('mutations', () => {
523
519
test ( 'each scope should run should run in parallel, serial within scope' , async ( ) => {
524
520
const results : Array < string > = [ ]
525
521
526
- const execute1 = executeMutation (
522
+ executeMutation (
527
523
queryClient ,
528
524
{
529
525
scope : {
@@ -539,7 +535,7 @@ describe('mutations', () => {
539
535
'vars1' ,
540
536
)
541
537
542
- const execute2 = executeMutation (
538
+ executeMutation (
543
539
queryClient ,
544
540
{
545
541
scope : {
@@ -555,7 +551,7 @@ describe('mutations', () => {
555
551
'vars2' ,
556
552
)
557
553
558
- const execute3 = executeMutation (
554
+ executeMutation (
559
555
queryClient ,
560
556
{
561
557
scope : {
@@ -571,7 +567,7 @@ describe('mutations', () => {
571
567
'vars1' ,
572
568
)
573
569
574
- const execute4 = executeMutation (
570
+ executeMutation (
575
571
queryClient ,
576
572
{
577
573
scope : {
@@ -587,7 +583,7 @@ describe('mutations', () => {
587
583
'vars2' ,
588
584
)
589
585
590
- await Promise . all ( [ execute1 , execute2 , execute3 , execute4 ] )
586
+ await vi . runAllTimersAsync ( )
591
587
592
588
expect ( results ) . toStrictEqual ( [
593
589
'start-A1' ,
0 commit comments