Skip to content

Commit c000d13

Browse files
authored
test(react-query): use fakeTimers in useIsFetching.test.tsx (#8982)
1 parent e5f57f6 commit c000d13

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

packages/react-query/src/__tests__/useIsFetching.test.tsx

+56-20
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { describe, expect, it } from 'vitest'
2-
import { fireEvent, render, waitFor } from '@testing-library/react'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { fireEvent, render } from '@testing-library/react'
33
import * as React from 'react'
44
import { QueryCache, useIsFetching, useQuery } from '..'
55
import {
66
createQueryClient,
77
queryKey,
88
renderWithClient,
99
setActTimeout,
10-
sleep,
1110
} from './utils'
1211

1312
describe('useIsFetching', () => {
13+
beforeEach(() => {
14+
vi.useFakeTimers()
15+
})
16+
17+
afterEach(() => {
18+
vi.useRealTimers()
19+
})
20+
1421
// See https://github.com/tannerlinsley/react-query/issues/105
1522
it('should update as queries start and stop fetching', async () => {
1623
const queryCache = new QueryCache()
@@ -28,7 +35,7 @@ describe('useIsFetching', () => {
2835
useQuery({
2936
queryKey: key,
3037
queryFn: async () => {
31-
await sleep(50)
38+
await vi.advanceTimersByTimeAsync(50)
3239
return 'test'
3340
},
3441
enabled: ready,
@@ -48,10 +55,19 @@ describe('useIsFetching', () => {
4855

4956
const { findByText, getByRole } = renderWithClient(queryClient, <Page />)
5057

51-
await findByText('isFetching: 0')
58+
await vi.waitFor(() => {
59+
findByText('isFetching: 0')
60+
})
61+
5262
fireEvent.click(getByRole('button', { name: /setReady/i }))
53-
await findByText('isFetching: 1')
54-
await findByText('isFetching: 0')
63+
64+
await vi.waitFor(() => {
65+
findByText('isFetching: 1')
66+
})
67+
68+
await vi.waitFor(() => {
69+
findByText('isFetching: 0')
70+
})
5571
})
5672

5773
it('should not update state while rendering', async () => {
@@ -73,7 +89,7 @@ describe('useIsFetching', () => {
7389
useQuery({
7490
queryKey: key1,
7591
queryFn: async () => {
76-
await sleep(100)
92+
await vi.advanceTimersByTimeAsync(100)
7793
return 'data'
7894
},
7995
})
@@ -84,7 +100,7 @@ describe('useIsFetching', () => {
84100
useQuery({
85101
queryKey: key2,
86102
queryFn: async () => {
87-
await sleep(100)
103+
await vi.advanceTimersByTimeAsync(100)
88104
return 'data'
89105
},
90106
})
@@ -110,7 +126,10 @@ describe('useIsFetching', () => {
110126
}
111127

112128
renderWithClient(queryClient, <Page />)
113-
await waitFor(() => expect(isFetchingArray).toEqual([0, 1, 1, 2, 1, 0]))
129+
130+
await vi.waitFor(() => {
131+
expect(isFetchingArray).toEqual([0, 1, 1, 2, 1, 0])
132+
})
114133
})
115134

116135
it('should be able to filter', async () => {
@@ -124,7 +143,7 @@ describe('useIsFetching', () => {
124143
useQuery({
125144
queryKey: key1,
126145
queryFn: async () => {
127-
await sleep(10)
146+
await vi.advanceTimersByTimeAsync(10)
128147
return 'test'
129148
},
130149
})
@@ -135,7 +154,7 @@ describe('useIsFetching', () => {
135154
useQuery({
136155
queryKey: key2,
137156
queryFn: async () => {
138-
await sleep(20)
157+
await vi.advanceTimersByTimeAsync(20)
139158
return 'test'
140159
},
141160
})
@@ -164,10 +183,20 @@ describe('useIsFetching', () => {
164183

165184
const { findByText, getByRole } = renderWithClient(queryClient, <Page />)
166185

167-
await findByText('isFetching: 0')
186+
await vi.waitFor(() => {
187+
findByText('isFetching: 0')
188+
})
189+
168190
fireEvent.click(getByRole('button', { name: /setStarted/i }))
169-
await findByText('isFetching: 1')
170-
await findByText('isFetching: 0')
191+
192+
await vi.waitFor(() => {
193+
findByText('isFetching: 1')
194+
})
195+
196+
await vi.waitFor(() => {
197+
findByText('isFetching: 0')
198+
})
199+
171200
// at no point should we have isFetching: 2
172201
expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
173202
})
@@ -180,7 +209,7 @@ describe('useIsFetching', () => {
180209
useQuery({
181210
queryKey: key,
182211
queryFn: async () => {
183-
await sleep(10)
212+
await vi.advanceTimersByTimeAsync(10)
184213
return 'test'
185214
},
186215
})
@@ -196,8 +225,13 @@ describe('useIsFetching', () => {
196225

197226
const rendered = renderWithClient(queryClient, <Page />)
198227

199-
await rendered.findByText('isFetching: 1')
200-
await rendered.findByText('isFetching: 0')
228+
await vi.waitFor(() => {
229+
rendered.findByText('isFetching: 1')
230+
})
231+
232+
await vi.waitFor(() => {
233+
rendered.findByText('isFetching: 0')
234+
})
201235
})
202236

203237
it('should use provided custom queryClient', async () => {
@@ -209,7 +243,7 @@ describe('useIsFetching', () => {
209243
{
210244
queryKey: key,
211245
queryFn: async () => {
212-
await sleep(10)
246+
await vi.advanceTimersByTimeAsync(10)
213247
return 'test'
214248
},
215249
},
@@ -227,6 +261,8 @@ describe('useIsFetching', () => {
227261

228262
const rendered = render(<Page></Page>)
229263

230-
await waitFor(() => rendered.getByText('isFetching: 1'))
264+
await vi.waitFor(() => {
265+
rendered.getByText('isFetching: 1')
266+
})
231267
})
232268
})

0 commit comments

Comments
 (0)