@@ -28,6 +28,7 @@ describe('useIsFetching', () => {
28
28
29
29
function IsFetching ( ) {
30
30
const isFetching = useIsFetching ( )
31
+
31
32
return < div > isFetching: { isFetching ( ) } </ div >
32
33
}
33
34
@@ -36,10 +37,7 @@ describe('useIsFetching', () => {
36
37
37
38
useQuery ( ( ) => ( {
38
39
queryKey : key ,
39
- queryFn : async ( ) => {
40
- await sleep ( 50 )
41
- return 'test'
42
- } ,
40
+ queryFn : ( ) => sleep ( 50 ) . then ( ( ) => 'test' ) ,
43
41
enabled : ready ( ) ,
44
42
} ) )
45
43
@@ -62,8 +60,8 @@ describe('useIsFetching', () => {
62
60
) )
63
61
64
62
expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
63
+
65
64
fireEvent . click ( rendered . getByRole ( 'button' , { name : / s e t R e a d y / i } ) )
66
- await vi . advanceTimersByTimeAsync ( 0 )
67
65
expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
68
66
await vi . advanceTimersByTimeAsync ( 50 )
69
67
expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
@@ -80,31 +78,29 @@ describe('useIsFetching', () => {
80
78
81
79
function IsFetching ( ) {
82
80
const isFetching = useIsFetching ( )
81
+
83
82
createRenderEffect ( ( ) => {
84
83
isFetchingArray . push ( isFetching ( ) )
85
84
} )
85
+
86
86
return null
87
87
}
88
88
89
89
function FirstQuery ( ) {
90
90
useQuery ( ( ) => ( {
91
91
queryKey : key1 ,
92
- queryFn : async ( ) => {
93
- await sleep ( 150 )
94
- return 'data'
95
- } ,
92
+ queryFn : ( ) => sleep ( 150 ) . then ( ( ) => 'data' ) ,
96
93
} ) )
94
+
97
95
return null
98
96
}
99
97
100
98
function SecondQuery ( ) {
101
99
useQuery ( ( ) => ( {
102
100
queryKey : key2 ,
103
- queryFn : async ( ) => {
104
- await sleep ( 200 )
105
- return 'data'
106
- } ,
101
+ queryFn : ( ) => sleep ( 200 ) . then ( ( ) => 'data' ) ,
107
102
} ) )
103
+
108
104
return null
109
105
}
110
106
@@ -133,8 +129,10 @@ describe('useIsFetching', () => {
133
129
< Page />
134
130
</ QueryClientProvider >
135
131
) )
132
+
136
133
// unlike react, Updating renderSecond wont cause a rerender for FirstQuery
137
134
await vi . advanceTimersByTimeAsync ( 300 )
135
+
138
136
expect ( isFetchingArray ) . toEqual ( [ 0 , 1 , 2 , 1 , 0 ] )
139
137
} )
140
138
@@ -148,22 +146,18 @@ describe('useIsFetching', () => {
148
146
function One ( ) {
149
147
useQuery ( ( ) => ( {
150
148
queryKey : key1 ,
151
- queryFn : async ( ) => {
152
- await sleep ( 10 )
153
- return 'test'
154
- } ,
149
+ queryFn : ( ) => sleep ( 10 ) . then ( ( ) => 'test' ) ,
155
150
} ) )
151
+
156
152
return null
157
153
}
158
154
159
155
function Two ( ) {
160
156
useQuery ( ( ) => ( {
161
157
queryKey : key2 ,
162
- queryFn : async ( ) => {
163
- await sleep ( 20 )
164
- return 'test'
165
- } ,
158
+ queryFn : ( ) => sleep ( 20 ) . then ( ( ) => 'test' ) ,
166
159
} ) )
160
+
167
161
return null
168
162
}
169
163
@@ -198,11 +192,12 @@ describe('useIsFetching', () => {
198
192
) )
199
193
200
194
expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
195
+
201
196
fireEvent . click ( rendered . getByRole ( 'button' , { name : / s e t S t a r t e d / i } ) )
202
- await vi . advanceTimersByTimeAsync ( 0 )
203
197
expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
204
198
await vi . advanceTimersByTimeAsync ( 20 )
205
199
expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
200
+
206
201
// at no point should we have isFetching: 2
207
202
expect ( isFetchingArray ) . toEqual ( expect . not . arrayContaining ( [ 2 ] ) )
208
203
} )
@@ -214,10 +209,7 @@ describe('useIsFetching', () => {
214
209
function Page ( ) {
215
210
useQuery ( ( ) => ( {
216
211
queryKey : key ,
217
- queryFn : async ( ) => {
218
- await sleep ( 10 )
219
- return 'test'
220
- } ,
212
+ queryFn : ( ) => sleep ( 10 ) . then ( ( ) => 'test' ) ,
221
213
} ) )
222
214
223
215
const isFetching = useIsFetching ( )
@@ -235,7 +227,6 @@ describe('useIsFetching', () => {
235
227
</ QueryClientProvider >
236
228
) )
237
229
238
- await vi . advanceTimersByTimeAsync ( 0 )
239
230
expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
240
231
await vi . advanceTimersByTimeAsync ( 10 )
241
232
expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
@@ -249,10 +240,7 @@ describe('useIsFetching', () => {
249
240
useQuery (
250
241
( ) => ( {
251
242
queryKey : key ,
252
- queryFn : async ( ) => {
253
- await sleep ( 10 )
254
- return 'test'
255
- } ,
243
+ queryFn : ( ) => sleep ( 10 ) . then ( ( ) => 'test' ) ,
256
244
} ) ,
257
245
( ) => queryClient ,
258
246
)
@@ -268,7 +256,8 @@ describe('useIsFetching', () => {
268
256
269
257
const rendered = render ( ( ) => < Page > </ Page > )
270
258
271
- await vi . advanceTimersByTimeAsync ( 0 )
272
259
expect ( rendered . getByText ( 'isFetching: 1' ) ) . toBeInTheDocument ( )
260
+ await vi . advanceTimersByTimeAsync ( 10 )
261
+ expect ( rendered . getByText ( 'isFetching: 0' ) ) . toBeInTheDocument ( )
273
262
} )
274
263
} )
0 commit comments