1- import { describe , expect , expectTypeOf , it } from 'vitest'
2- import { fireEvent , render , waitFor } from '@testing-library/react'
1+ import {
2+ afterEach ,
3+ beforeEach ,
4+ describe ,
5+ expect ,
6+ expectTypeOf ,
7+ it ,
8+ vi ,
9+ } from 'vitest'
10+ import { fireEvent , render } from '@testing-library/react'
311import * as React from 'react'
412import { useIsMutating , useMutationState } from '../useMutationState'
513import { useMutation } from '../useMutation'
@@ -12,6 +20,14 @@ import {
1220import type { MutationState , MutationStatus } from '@tanstack/query-core'
1321
1422describe ( 'useIsMutating' , ( ) => {
23+ beforeEach ( ( ) => {
24+ vi . useFakeTimers ( )
25+ } )
26+
27+ afterEach ( ( ) => {
28+ vi . useRealTimers ( )
29+ } )
30+
1531 it ( 'should return the number of fetching mutations' , async ( ) => {
1632 const isMutatingArray : Array < number > = [ ]
1733 const queryClient = createQueryClient ( )
@@ -25,17 +41,11 @@ describe('useIsMutating', () => {
2541 function Mutations ( ) {
2642 const { mutate : mutate1 } = useMutation ( {
2743 mutationKey : [ 'mutation1' ] ,
28- mutationFn : async ( ) => {
29- await sleep ( 50 )
30- return 'data'
31- } ,
44+ mutationFn : ( ) => sleep ( 50 ) . then ( ( ) => 'data' ) ,
3245 } )
3346 const { mutate : mutate2 } = useMutation ( {
3447 mutationKey : [ 'mutation2' ] ,
35- mutationFn : async ( ) => {
36- await sleep ( 10 )
37- return 'data'
38- } ,
48+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
3949 } )
4050
4151 return (
@@ -57,7 +67,7 @@ describe('useIsMutating', () => {
5767
5868 const rendered = renderWithClient ( queryClient , < Page /> )
5969 fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e 1 / i } ) )
60- await sleep ( 10 )
70+ await vi . advanceTimersByTimeAsync ( 10 )
6171 fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e 2 / i } ) )
6272
6373 // we don't really care if this yields
@@ -66,10 +76,10 @@ describe('useIsMutating', () => {
6676 // [ +0, 1, 2, 1, +0 ]
6777 // our batching strategy might yield different results
6878
69- await waitFor ( ( ) => expect ( isMutatingArray [ 0 ] ) . toEqual ( 0 ) )
70- await waitFor ( ( ) => expect ( isMutatingArray [ 1 ] ) . toEqual ( 1 ) )
71- await waitFor ( ( ) => expect ( isMutatingArray [ 2 ] ) . toEqual ( 2 ) )
72- await waitFor ( ( ) =>
79+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 0 ] ) . toEqual ( 0 ) )
80+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 1 ] ) . toEqual ( 1 ) )
81+ await vi . waitFor ( ( ) => expect ( isMutatingArray [ 2 ] ) . toEqual ( 2 ) )
82+ await vi . waitFor ( ( ) =>
7383 expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 ) ,
7484 )
7585 } )
@@ -87,17 +97,11 @@ describe('useIsMutating', () => {
8797 function Page ( ) {
8898 const { mutate : mutate1 } = useMutation ( {
8999 mutationKey : [ 'mutation1' ] ,
90- mutationFn : async ( ) => {
91- await sleep ( 100 )
92- return 'data'
93- } ,
100+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
94101 } )
95102 const { mutate : mutate2 } = useMutation ( {
96103 mutationKey : [ 'mutation2' ] ,
97- mutationFn : async ( ) => {
98- await sleep ( 100 )
99- return 'data'
100- } ,
104+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
101105 } )
102106
103107 React . useEffect ( ( ) => {
@@ -109,7 +113,7 @@ describe('useIsMutating', () => {
109113 }
110114
111115 renderWithClient ( queryClient , < Page /> )
112- await waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
116+ await vi . waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
113117 } )
114118
115119 it ( 'should filter correctly by predicate' , async ( ) => {
@@ -128,17 +132,11 @@ describe('useIsMutating', () => {
128132 function Page ( ) {
129133 const { mutate : mutate1 } = useMutation ( {
130134 mutationKey : [ 'mutation1' ] ,
131- mutationFn : async ( ) => {
132- await sleep ( 100 )
133- return 'data'
134- } ,
135+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
135136 } )
136137 const { mutate : mutate2 } = useMutation ( {
137138 mutationKey : [ 'mutation2' ] ,
138- mutationFn : async ( ) => {
139- await sleep ( 100 )
140- return 'data'
141- } ,
139+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
142140 } )
143141
144142 React . useEffect ( ( ) => {
@@ -150,7 +148,7 @@ describe('useIsMutating', () => {
150148 }
151149
152150 renderWithClient ( queryClient , < Page /> )
153- await waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
151+ await vi . waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
154152 } )
155153
156154 it ( 'should use provided custom queryClient' , async ( ) => {
@@ -161,10 +159,7 @@ describe('useIsMutating', () => {
161159 const { mutate } = useMutation (
162160 {
163161 mutationKey : [ 'mutation1' ] ,
164- mutationFn : async ( ) => {
165- await sleep ( 10 )
166- return 'data'
167- } ,
162+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
168163 } ,
169164 queryClient ,
170165 )
@@ -182,7 +177,7 @@ describe('useIsMutating', () => {
182177
183178 const rendered = render ( < Page > </ Page > )
184179
185- await waitFor ( ( ) => rendered . getByText ( 'mutating: 1' ) )
180+ await vi . waitFor ( ( ) => rendered . getByText ( 'mutating: 1' ) )
186181 } )
187182} )
188183
@@ -227,10 +222,7 @@ describe('useMutationState', () => {
227222 function Mutate ( ) {
228223 const { mutate, data } = useMutation ( {
229224 mutationKey,
230- mutationFn : async ( input : number ) => {
231- await sleep ( 150 )
232- return 'data' + input
233- } ,
225+ mutationFn : ( input : number ) => sleep ( 150 ) . then ( ( ) => 'data' + input ) ,
234226 } )
235227
236228 return (
@@ -252,11 +244,11 @@ describe('useMutationState', () => {
252244
253245 const rendered = renderWithClient ( queryClient , < Page /> )
254246
255- await waitFor ( ( ) => rendered . getByText ( 'data: null' ) )
247+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: null' ) )
256248
257249 fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
258250
259- await waitFor ( ( ) => rendered . getByText ( 'data: data1' ) )
251+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: data1' ) )
260252
261253 expect ( variables ) . toEqual ( [ [ ] , [ 1 ] , [ ] ] )
262254 } )
0 commit comments