Skip to content

Commit 5fb38e9

Browse files
committed
test(react-query/useIsFetching): remove unnecessary 'advanceTimersByTimeAsync', unify to 'rendered', and add detailed 'expect' for custom queryClient and state updates during rendering
1 parent 34657e5 commit 5fb38e9

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

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

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ describe('useIsFetching', () => {
1616

1717
// See https://github.com/tannerlinsley/react-query/issues/105
1818
it('should update as queries start and stop fetching', async () => {
19-
const queryCache = new QueryCache()
20-
const queryClient = new QueryClient({ queryCache })
19+
const queryClient = new QueryClient()
2120
const key = queryKey()
2221

2322
function IsFetching() {
2423
const isFetching = useIsFetching()
24+
2525
return <div>isFetching: {isFetching}</div>
2626
}
2727

@@ -46,20 +46,19 @@ describe('useIsFetching', () => {
4646
)
4747
}
4848

49-
const { getByText, getByRole } = renderWithClient(queryClient, <Page />)
49+
const rendered = renderWithClient(queryClient, <Page />)
5050

51-
expect(getByText('isFetching: 0')).toBeInTheDocument()
51+
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
5252

53-
fireEvent.click(getByRole('button', { name: /setReady/i }))
53+
fireEvent.click(rendered.getByRole('button', { name: /setReady/i }))
5454
await vi.advanceTimersByTimeAsync(0)
55-
expect(getByText('isFetching: 1')).toBeInTheDocument()
55+
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
5656
await vi.advanceTimersByTimeAsync(51)
57-
expect(getByText('isFetching: 0')).toBeInTheDocument()
57+
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
5858
})
5959

6060
it('should not update state while rendering', async () => {
61-
const queryCache = new QueryCache()
62-
const queryClient = new QueryClient({ queryCache })
61+
const queryClient = new QueryClient()
6362

6463
const key1 = queryKey()
6564
const key2 = queryKey()
@@ -68,23 +67,27 @@ describe('useIsFetching', () => {
6867

6968
function IsFetching() {
7069
const isFetching = useIsFetching()
70+
7171
isFetchingArray.push(isFetching)
72-
return null
72+
73+
return <div>isFetching: {isFetching}</div>
7374
}
7475

7576
function FirstQuery() {
7677
useQuery({
7778
queryKey: key1,
78-
queryFn: () => sleep(100).then(() => 'data'),
79+
queryFn: () => sleep(100).then(() => 'data1'),
7980
})
81+
8082
return null
8183
}
8284

8385
function SecondQuery() {
8486
useQuery({
8587
queryKey: key2,
86-
queryFn: () => sleep(100).then(() => 'data'),
88+
queryFn: () => sleep(100).then(() => 'data2'),
8789
})
90+
8891
return null
8992
}
9093

@@ -108,7 +111,18 @@ describe('useIsFetching', () => {
108111

109112
renderWithClient(queryClient, <Page />)
110113

111-
await vi.advanceTimersByTimeAsync(151)
114+
expect(isFetchingArray[0]).toEqual(0)
115+
await vi.advanceTimersByTimeAsync(0)
116+
expect(isFetchingArray[1]).toEqual(1)
117+
await vi.advanceTimersByTimeAsync(50)
118+
expect(isFetchingArray[2]).toEqual(1)
119+
await vi.advanceTimersByTimeAsync(1)
120+
expect(isFetchingArray[3]).toEqual(2)
121+
await vi.advanceTimersByTimeAsync(50)
122+
expect(isFetchingArray[4]).toEqual(1)
123+
await vi.advanceTimersByTimeAsync(50)
124+
expect(isFetchingArray[5]).toEqual(0)
125+
112126
expect(isFetchingArray).toEqual([0, 1, 1, 2, 1, 0])
113127
})
114128

@@ -122,16 +136,18 @@ describe('useIsFetching', () => {
122136
function One() {
123137
useQuery({
124138
queryKey: key1,
125-
queryFn: () => sleep(10).then(() => 'test'),
139+
queryFn: () => sleep(10).then(() => 'test1'),
126140
})
141+
127142
return null
128143
}
129144

130145
function Two() {
131146
useQuery({
132147
queryKey: key2,
133-
queryFn: () => sleep(20).then(() => 'test'),
148+
queryFn: () => sleep(20).then(() => 'test2'),
134149
})
150+
135151
return null
136152
}
137153

@@ -155,15 +171,15 @@ describe('useIsFetching', () => {
155171
)
156172
}
157173

158-
const { getByText, getByRole } = renderWithClient(queryClient, <Page />)
174+
const rendered = renderWithClient(queryClient, <Page />)
159175

160-
expect(getByText('isFetching: 0')).toBeInTheDocument()
176+
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
161177

162-
fireEvent.click(getByRole('button', { name: /setStarted/i }))
178+
fireEvent.click(rendered.getByRole('button', { name: /setStarted/i }))
163179
await vi.advanceTimersByTimeAsync(0)
164-
expect(getByText('isFetching: 1')).toBeInTheDocument()
180+
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
165181
await vi.advanceTimersByTimeAsync(11)
166-
expect(getByText('isFetching: 0')).toBeInTheDocument()
182+
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
167183

168184
// at no point should we have isFetching: 2
169185
expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
@@ -190,14 +206,16 @@ describe('useIsFetching', () => {
190206

191207
const rendered = renderWithClient(queryClient, <Page />)
192208

193-
await vi.advanceTimersByTimeAsync(0)
194209
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
195210
await vi.advanceTimersByTimeAsync(11)
196211
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
197212
})
198213

199214
it('should use provided custom queryClient', async () => {
200-
const queryClient = new QueryClient()
215+
const onSuccess = vi.fn()
216+
217+
const queryCache = new QueryCache({ onSuccess })
218+
const queryClient = new QueryClient({ queryCache })
201219
const key = queryKey()
202220

203221
function Page() {
@@ -218,9 +236,11 @@ describe('useIsFetching', () => {
218236
)
219237
}
220238

221-
const rendered = render(<Page></Page>)
239+
const rendered = render(<Page />)
222240

223-
await vi.advanceTimersByTimeAsync(0)
224241
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
242+
await vi.advanceTimersByTimeAsync(11)
243+
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
244+
expect(onSuccess).toHaveBeenCalledOnce()
225245
})
226246
})

0 commit comments

Comments
 (0)