11import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
2- import { waitFor } from '@testing-library/dom'
32import { MutationObserver } from '../mutationObserver'
43import { createQueryClient , executeMutation , queryKey , sleep } from './utils'
54import type { QueryClient } from '..'
@@ -9,12 +8,14 @@ describe('mutations', () => {
98 let queryClient : QueryClient
109
1110 beforeEach ( ( ) => {
11+ vi . useFakeTimers ( )
1212 queryClient = createQueryClient ( )
1313 queryClient . mount ( )
1414 } )
1515
1616 afterEach ( ( ) => {
1717 queryClient . clear ( )
18+ vi . useRealTimers ( )
1819 } )
1920
2021 test ( 'mutate should accept null values' , async ( ) => {
@@ -54,10 +55,7 @@ describe('mutations', () => {
5455
5556 test ( 'mutation should set correct success states' , async ( ) => {
5657 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 ) ,
6159 onMutate : ( text ) => text ,
6260 } )
6361
@@ -87,7 +85,7 @@ describe('mutations', () => {
8785
8886 mutation . mutate ( 'todo' )
8987
90- await sleep ( 0 )
88+ await vi . advanceTimersByTimeAsync ( 0 )
9189
9290 expect ( states [ 0 ] ) . toEqual ( {
9391 context : undefined ,
@@ -107,7 +105,7 @@ describe('mutations', () => {
107105 submittedAt : expect . any ( Number ) ,
108106 } )
109107
110- await sleep ( 5 )
108+ await vi . advanceTimersByTimeAsync ( 5 )
111109
112110 expect ( states [ 1 ] ) . toEqual ( {
113111 context : 'todo' ,
@@ -127,7 +125,7 @@ describe('mutations', () => {
127125 submittedAt : expect . any ( Number ) ,
128126 } )
129127
130- await sleep ( 20 )
128+ await vi . advanceTimersByTimeAsync ( 20 )
131129
132130 expect ( states [ 2 ] ) . toEqual ( {
133131 context : 'todo' ,
@@ -150,10 +148,8 @@ describe('mutations', () => {
150148
151149 test ( 'mutation should set correct error states' , async ( ) => {
152150 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' ) ) ) ,
157153 onMutate : ( text ) => text ,
158154 retry : 1 ,
159155 retryDelay : 1 ,
@@ -167,7 +163,7 @@ describe('mutations', () => {
167163
168164 mutation . mutate ( 'todo' ) . catch ( ( ) => undefined )
169165
170- await sleep ( 0 )
166+ await vi . advanceTimersByTimeAsync ( 0 )
171167
172168 expect ( states [ 0 ] ) . toEqual ( {
173169 context : undefined ,
@@ -187,7 +183,7 @@ describe('mutations', () => {
187183 submittedAt : expect . any ( Number ) ,
188184 } )
189185
190- await sleep ( 10 )
186+ await vi . advanceTimersByTimeAsync ( 10 )
191187
192188 expect ( states [ 1 ] ) . toEqual ( {
193189 context : 'todo' ,
@@ -207,7 +203,7 @@ describe('mutations', () => {
207203 submittedAt : expect . any ( Number ) ,
208204 } )
209205
210- await sleep ( 20 )
206+ await vi . advanceTimersByTimeAsync ( 20 )
211207
212208 expect ( states [ 2 ] ) . toEqual ( {
213209 context : 'todo' ,
@@ -227,7 +223,7 @@ describe('mutations', () => {
227223 submittedAt : expect . any ( Number ) ,
228224 } )
229225
230- await sleep ( 30 )
226+ await vi . advanceTimersByTimeAsync ( 30 )
231227
232228 expect ( states [ 3 ] ) . toEqual ( {
233229 context : 'todo' ,
@@ -405,7 +401,7 @@ describe('mutations', () => {
405401 } ,
406402 } )
407403
408- await waitFor ( ( ) => expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 ) )
404+ await vi . waitFor ( ( ) => expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 ) )
409405
410406 expect ( onSuccess ) . toHaveBeenCalledWith ( 2 )
411407 } )
@@ -417,7 +413,7 @@ describe('mutations', () => {
417413
418414 const results : Array < string > = [ ]
419415
420- const execute1 = executeMutation (
416+ executeMutation (
421417 queryClient ,
422418 {
423419 mutationKey : key1 ,
@@ -441,7 +437,7 @@ describe('mutations', () => {
441437 isPaused : false ,
442438 } )
443439
444- const execute2 = executeMutation (
440+ executeMutation (
445441 queryClient ,
446442 {
447443 mutationKey : key2 ,
@@ -465,7 +461,7 @@ describe('mutations', () => {
465461 isPaused : true ,
466462 } )
467463
468- await Promise . all ( [ execute1 , execute2 ] )
464+ await vi . runAllTimersAsync ( )
469465
470466 expect ( results ) . toStrictEqual ( [
471467 'start-A' ,
@@ -482,7 +478,7 @@ describe('mutations', () => {
482478
483479 const results : Array < string > = [ ]
484480
485- const execute1 = executeMutation (
481+ executeMutation (
486482 queryClient ,
487483 {
488484 mutationKey : key1 ,
@@ -496,7 +492,7 @@ describe('mutations', () => {
496492 'vars1' ,
497493 )
498494
499- const execute2 = executeMutation (
495+ executeMutation (
500496 queryClient ,
501497 {
502498 mutationKey : key2 ,
@@ -510,7 +506,7 @@ describe('mutations', () => {
510506 'vars2' ,
511507 )
512508
513- await Promise . all ( [ execute1 , execute2 ] )
509+ await vi . runAllTimersAsync ( )
514510
515511 expect ( results ) . toStrictEqual ( [
516512 'start-A' ,
@@ -523,7 +519,7 @@ describe('mutations', () => {
523519 test ( 'each scope should run should run in parallel, serial within scope' , async ( ) => {
524520 const results : Array < string > = [ ]
525521
526- const execute1 = executeMutation (
522+ executeMutation (
527523 queryClient ,
528524 {
529525 scope : {
@@ -539,7 +535,7 @@ describe('mutations', () => {
539535 'vars1' ,
540536 )
541537
542- const execute2 = executeMutation (
538+ executeMutation (
543539 queryClient ,
544540 {
545541 scope : {
@@ -555,7 +551,7 @@ describe('mutations', () => {
555551 'vars2' ,
556552 )
557553
558- const execute3 = executeMutation (
554+ executeMutation (
559555 queryClient ,
560556 {
561557 scope : {
@@ -571,7 +567,7 @@ describe('mutations', () => {
571567 'vars1' ,
572568 )
573569
574- const execute4 = executeMutation (
570+ executeMutation (
575571 queryClient ,
576572 {
577573 scope : {
@@ -587,7 +583,7 @@ describe('mutations', () => {
587583 'vars2' ,
588584 )
589585
590- await Promise . all ( [ execute1 , execute2 , execute3 , execute4 ] )
586+ await vi . runAllTimersAsync ( )
591587
592588 expect ( results ) . toStrictEqual ( [
593589 'start-A1' ,
0 commit comments