1
1
import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
2
2
import { fireEvent , render } from '@testing-library/react'
3
3
import * as React from 'react'
4
- import { queryKey } from '@tanstack/query-test-utils'
4
+ import { queryKey , sleep } from '@tanstack/query-test-utils'
5
5
import { QueryCache , QueryClient , useIsFetching , useQuery } from '..'
6
6
import { renderWithClient , setActTimeout } from './utils'
7
7
@@ -31,7 +31,7 @@ describe('useIsFetching', () => {
31
31
useQuery ( {
32
32
queryKey : key ,
33
33
queryFn : async ( ) => {
34
- await vi . advanceTimersByTimeAsync ( 50 )
34
+ await sleep ( 50 )
35
35
return 'test'
36
36
} ,
37
37
enabled : ready ,
@@ -51,19 +51,15 @@ describe('useIsFetching', () => {
51
51
52
52
const { getByText, getByRole } = renderWithClient ( queryClient , < Page /> )
53
53
54
- await vi . waitFor ( ( ) => {
55
- expect ( getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
56
- } )
54
+ expect ( getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
57
55
58
56
fireEvent . click ( getByRole ( 'button' , { name : / s e t R e a d y / i } ) )
59
57
60
- await vi . waitFor ( ( ) => {
61
- expect ( getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
62
- } )
58
+ await vi . advanceTimersByTimeAsync ( 0 )
59
+ expect ( getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
63
60
64
- await vi . waitFor ( ( ) => {
65
- expect ( getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
66
- } )
61
+ await vi . advanceTimersByTimeAsync ( 51 )
62
+ expect ( getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
67
63
} )
68
64
69
65
it ( 'should not update state while rendering' , async ( ) => {
@@ -85,7 +81,7 @@ describe('useIsFetching', () => {
85
81
useQuery ( {
86
82
queryKey : key1 ,
87
83
queryFn : async ( ) => {
88
- await vi . advanceTimersByTimeAsync ( 100 )
84
+ await sleep ( 100 )
89
85
return 'data'
90
86
} ,
91
87
} )
@@ -96,7 +92,7 @@ describe('useIsFetching', () => {
96
92
useQuery ( {
97
93
queryKey : key2 ,
98
94
queryFn : async ( ) => {
99
- await vi . advanceTimersByTimeAsync ( 100 )
95
+ await sleep ( 100 )
100
96
return 'data'
101
97
} ,
102
98
} )
@@ -123,9 +119,8 @@ describe('useIsFetching', () => {
123
119
124
120
renderWithClient ( queryClient , < Page /> )
125
121
126
- await vi . waitFor ( ( ) => {
127
- expect ( isFetchingArray ) . toEqual ( [ 0 , 1 , 1 , 2 , 1 , 0 ] )
128
- } )
122
+ await vi . advanceTimersByTimeAsync ( 151 )
123
+ expect ( isFetchingArray ) . toEqual ( [ 0 , 1 , 1 , 2 , 1 , 0 ] )
129
124
} )
130
125
131
126
it ( 'should be able to filter' , async ( ) => {
@@ -139,7 +134,7 @@ describe('useIsFetching', () => {
139
134
useQuery ( {
140
135
queryKey : key1 ,
141
136
queryFn : async ( ) => {
142
- await vi . advanceTimersByTimeAsync ( 10 )
137
+ await sleep ( 10 )
143
138
return 'test'
144
139
} ,
145
140
} )
@@ -150,7 +145,7 @@ describe('useIsFetching', () => {
150
145
useQuery ( {
151
146
queryKey : key2 ,
152
147
queryFn : async ( ) => {
153
- await vi . advanceTimersByTimeAsync ( 20 )
148
+ await sleep ( 20 )
154
149
return 'test'
155
150
} ,
156
151
} )
@@ -177,21 +172,17 @@ describe('useIsFetching', () => {
177
172
)
178
173
}
179
174
180
- const { findByText , getByRole } = renderWithClient ( queryClient , < Page /> )
175
+ const { getByText , getByRole } = renderWithClient ( queryClient , < Page /> )
181
176
182
- await vi . waitFor ( ( ) => {
183
- findByText ( 'isFetching: 0' )
184
- } )
177
+ getByText ( 'isFetching: 0' )
185
178
186
179
fireEvent . click ( getByRole ( 'button' , { name : / s e t S t a r t e d / i } ) )
187
180
188
- await vi . waitFor ( ( ) => {
189
- findByText ( 'isFetching: 1' )
190
- } )
181
+ await vi . advanceTimersByTimeAsync ( 0 )
182
+ getByText ( 'isFetching: 1' )
191
183
192
- await vi . waitFor ( ( ) => {
193
- findByText ( 'isFetching: 0' )
194
- } )
184
+ await vi . advanceTimersByTimeAsync ( 11 )
185
+ getByText ( 'isFetching: 0' )
195
186
196
187
// at no point should we have isFetching: 2
197
188
expect ( isFetchingArray ) . toEqual ( expect . not . arrayContaining ( [ 2 ] ) )
@@ -205,7 +196,7 @@ describe('useIsFetching', () => {
205
196
useQuery ( {
206
197
queryKey : key ,
207
198
queryFn : async ( ) => {
208
- await vi . advanceTimersByTimeAsync ( 10 )
199
+ await sleep ( 10 )
209
200
return 'test'
210
201
} ,
211
202
} )
@@ -221,13 +212,11 @@ describe('useIsFetching', () => {
221
212
222
213
const rendered = renderWithClient ( queryClient , < Page /> )
223
214
224
- await vi . waitFor ( ( ) => {
225
- expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
226
- } )
215
+ await vi . advanceTimersByTimeAsync ( 0 )
216
+ expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
227
217
228
- await vi . waitFor ( ( ) => {
229
- expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
230
- } )
218
+ await vi . advanceTimersByTimeAsync ( 11 )
219
+ expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
231
220
} )
232
221
233
222
it ( 'should use provided custom queryClient' , async ( ) => {
@@ -239,7 +228,7 @@ describe('useIsFetching', () => {
239
228
{
240
229
queryKey : key ,
241
230
queryFn : async ( ) => {
242
- await vi . advanceTimersByTimeAsync ( 10 )
231
+ await sleep ( 10 )
243
232
return 'test'
244
233
} ,
245
234
} ,
@@ -257,8 +246,7 @@ describe('useIsFetching', () => {
257
246
258
247
const rendered = render ( < Page > </ Page > )
259
248
260
- await vi . waitFor ( ( ) => {
261
- expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
262
- } )
249
+ await vi . advanceTimersByTimeAsync ( 0 )
250
+ expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
263
251
} )
264
252
} )
0 commit comments