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'
3
11
import * as React from 'react'
4
12
import { useIsMutating , useMutationState } from '../useMutationState'
5
13
import { useMutation } from '../useMutation'
@@ -12,6 +20,14 @@ import {
12
20
import type { MutationState , MutationStatus } from '@tanstack/query-core'
13
21
14
22
describe ( 'useIsMutating' , ( ) => {
23
+ beforeEach ( ( ) => {
24
+ vi . useFakeTimers ( )
25
+ } )
26
+
27
+ afterEach ( ( ) => {
28
+ vi . useRealTimers ( )
29
+ } )
30
+
15
31
it ( 'should return the number of fetching mutations' , async ( ) => {
16
32
const isMutatingArray : Array < number > = [ ]
17
33
const queryClient = createQueryClient ( )
@@ -25,17 +41,11 @@ describe('useIsMutating', () => {
25
41
function Mutations ( ) {
26
42
const { mutate : mutate1 } = useMutation ( {
27
43
mutationKey : [ 'mutation1' ] ,
28
- mutationFn : async ( ) => {
29
- await sleep ( 50 )
30
- return 'data'
31
- } ,
44
+ mutationFn : ( ) => sleep ( 50 ) . then ( ( ) => 'data' ) ,
32
45
} )
33
46
const { mutate : mutate2 } = useMutation ( {
34
47
mutationKey : [ 'mutation2' ] ,
35
- mutationFn : async ( ) => {
36
- await sleep ( 10 )
37
- return 'data'
38
- } ,
48
+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
39
49
} )
40
50
41
51
return (
@@ -57,7 +67,7 @@ describe('useIsMutating', () => {
57
67
58
68
const rendered = renderWithClient ( queryClient , < Page /> )
59
69
fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e 1 / i } ) )
60
- await sleep ( 10 )
70
+ await vi . advanceTimersByTimeAsync ( 10 )
61
71
fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e 2 / i } ) )
62
72
63
73
// we don't really care if this yields
@@ -66,10 +76,10 @@ describe('useIsMutating', () => {
66
76
// [ +0, 1, 2, 1, +0 ]
67
77
// our batching strategy might yield different results
68
78
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 ( ( ) =>
73
83
expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 ) ,
74
84
)
75
85
} )
@@ -87,17 +97,11 @@ describe('useIsMutating', () => {
87
97
function Page ( ) {
88
98
const { mutate : mutate1 } = useMutation ( {
89
99
mutationKey : [ 'mutation1' ] ,
90
- mutationFn : async ( ) => {
91
- await sleep ( 100 )
92
- return 'data'
93
- } ,
100
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
94
101
} )
95
102
const { mutate : mutate2 } = useMutation ( {
96
103
mutationKey : [ 'mutation2' ] ,
97
- mutationFn : async ( ) => {
98
- await sleep ( 100 )
99
- return 'data'
100
- } ,
104
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
101
105
} )
102
106
103
107
React . useEffect ( ( ) => {
@@ -109,7 +113,7 @@ describe('useIsMutating', () => {
109
113
}
110
114
111
115
renderWithClient ( queryClient , < Page /> )
112
- await waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
116
+ await vi . waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
113
117
} )
114
118
115
119
it ( 'should filter correctly by predicate' , async ( ) => {
@@ -128,17 +132,11 @@ describe('useIsMutating', () => {
128
132
function Page ( ) {
129
133
const { mutate : mutate1 } = useMutation ( {
130
134
mutationKey : [ 'mutation1' ] ,
131
- mutationFn : async ( ) => {
132
- await sleep ( 100 )
133
- return 'data'
134
- } ,
135
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
135
136
} )
136
137
const { mutate : mutate2 } = useMutation ( {
137
138
mutationKey : [ 'mutation2' ] ,
138
- mutationFn : async ( ) => {
139
- await sleep ( 100 )
140
- return 'data'
141
- } ,
139
+ mutationFn : ( ) => sleep ( 100 ) . then ( ( ) => 'data' ) ,
142
140
} )
143
141
144
142
React . useEffect ( ( ) => {
@@ -150,7 +148,7 @@ describe('useIsMutating', () => {
150
148
}
151
149
152
150
renderWithClient ( queryClient , < Page /> )
153
- await waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
151
+ await vi . waitFor ( ( ) => expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] ) )
154
152
} )
155
153
156
154
it ( 'should use provided custom queryClient' , async ( ) => {
@@ -161,10 +159,7 @@ describe('useIsMutating', () => {
161
159
const { mutate } = useMutation (
162
160
{
163
161
mutationKey : [ 'mutation1' ] ,
164
- mutationFn : async ( ) => {
165
- await sleep ( 10 )
166
- return 'data'
167
- } ,
162
+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
168
163
} ,
169
164
queryClient ,
170
165
)
@@ -182,7 +177,7 @@ describe('useIsMutating', () => {
182
177
183
178
const rendered = render ( < Page > </ Page > )
184
179
185
- await waitFor ( ( ) => rendered . getByText ( 'mutating: 1' ) )
180
+ await vi . waitFor ( ( ) => rendered . getByText ( 'mutating: 1' ) )
186
181
} )
187
182
} )
188
183
@@ -227,10 +222,7 @@ describe('useMutationState', () => {
227
222
function Mutate ( ) {
228
223
const { mutate, data } = useMutation ( {
229
224
mutationKey,
230
- mutationFn : async ( input : number ) => {
231
- await sleep ( 150 )
232
- return 'data' + input
233
- } ,
225
+ mutationFn : ( input : number ) => sleep ( 150 ) . then ( ( ) => 'data' + input ) ,
234
226
} )
235
227
236
228
return (
@@ -252,11 +244,11 @@ describe('useMutationState', () => {
252
244
253
245
const rendered = renderWithClient ( queryClient , < Page /> )
254
246
255
- await waitFor ( ( ) => rendered . getByText ( 'data: null' ) )
247
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: null' ) )
256
248
257
249
fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
258
250
259
- await waitFor ( ( ) => rendered . getByText ( 'data: data1' ) )
251
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: data1' ) )
260
252
261
253
expect ( variables ) . toEqual ( [ [ ] , [ 1 ] , [ ] ] )
262
254
} )
0 commit comments