Open History to see the last commit
1import { createKey, sleep } from '@tanstack/query-test-utils'
2import { fireEvent, render } from '@testing-library/react'
3import * as React from 'react'
4import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
5import { QueryCache, QueryClient, useIsFetching, useQuery } from '..'
6import { renderWithClient, setActTimeout } from './utils'
7import type { Query } from '..'
8β
9describe('useIsFetching', () => {
10 beforeEach(() => {
11 vi.useFakeTimers()
12 })
13β
14 afterEach(() => {
15 vi.useRealTimers()
16 })
17β
18 // See https://github.com/tannerlinsley/react-query/issues/105
19 it('should update as queries start and stop fetching', async () => {
20 const queryClient = new QueryClient()
21 const key = createKey()
22β
23 function IsFetching() {
24 const isFetching = useIsFetching()
25β
26 return <div>isFetching: {isFetching}</div>
27 }
28β
29 function Query() {
30 const [ready, setReady] = React.useState(false)
31β
32 useQuery({
33 queryKey: key,
34 queryFn: () => sleep(50).then(() => 'test'),
35 enabled: ready,
36 })
37β
38 return <button onClick={() => setReady(true)}>setReady</button>
39 }
40β
41 function Page() {
42 return (
43 <div>
44 <IsFetching />
45 <Query />
46 </div>
47 )
48 }
49β
50 const rendered = renderWithClient(queryClient, <Page />)
51β
52 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
53β
54 fireEvent.click(rendered.getByRole('button', { name: /setReady/i }))
55 await vi.advanceTimersByTimeAsync(0)
56 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
57 await vi.advanceTimersByTimeAsync(51)
58 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
59 })
60β
61 it('should not update state while rendering', async () => {
62 const queryClient = new QueryClient()
63β
64 const key1 = createKey()
65 const key2 = createKey()
66β
67 const isFetchingArray: Array<number> = []
68β
69 function IsFetching() {
70 const isFetching = useIsFetching()
71β
72 isFetchingArray.push(isFetching)
73β
74 return null
75 }
76β
77 function FirstQuery() {
78 useQuery({
79 queryKey: key1,
80 queryFn: () => sleep(100).then(() => 'data1'),
81 })
82β
83 return null
84 }
85β
86 function SecondQuery() {
87 useQuery({
88 queryKey: key2,
89 queryFn: () => sleep(100).then(() => 'data2'),
90 })
91β
92 return null
93 }
94β
95 function Page() {
96 const [renderSecond, setRenderSecond] = React.useState(false)
97β
98 React.useEffect(() => {
99 setActTimeout(() => {
100 setRenderSecond(true)
101 }, 50)
102 }, [])
103β
104 return (
105 <>
106 <IsFetching />
107 <FirstQuery />
108 {renderSecond && <SecondQuery />}
109 </>
110 )
111 }
112β
113 renderWithClient(queryClient, <Page />)
114β
115 expect(isFetchingArray[0]).toBe(0)
116 await vi.advanceTimersByTimeAsync(0)
117 expect(isFetchingArray[1]).toBe(1)
118 await vi.advanceTimersByTimeAsync(50)
119 expect(isFetchingArray[2]).toBe(1)
120 await vi.advanceTimersByTimeAsync(1)
121 expect(isFetchingArray[3]).toBe(2)
122 await vi.advanceTimersByTimeAsync(50)
123 expect(isFetchingArray[4]).toBe(1)
124 await vi.advanceTimersByTimeAsync(50)
125 expect(isFetchingArray[5]).toBe(0)
126β
127 expect(isFetchingArray).toStrictEqual([0, 1, 1, 2, 1, 0])
128 })
129β
130 it('should be able to filter', async () => {
131 const queryClient = new QueryClient()
132 const key1 = createKey()
133 const key2 = createKey()
134β
135 const isFetchingArray: Array<number> = []
136β
137 function One() {
138 useQuery({
139 queryKey: key1,
140 queryFn: () => sleep(10).then(() => 'test1'),
141 })
142β
143 return null
144 }
145β
146 function Two() {
147 useQuery({
148 queryKey: key2,
149 queryFn: () => sleep(20).then(() => 'test2'),
150 })
151β
152 return null
153 }
154β
155 function Page() {
156 const [started, setStarted] = React.useState(false)
157 const isFetching = useIsFetching({ queryKey: key1 })
158β
159 isFetchingArray.push(isFetching)
160β
161 return (
162 <div>
163 <button onClick={() => setStarted(true)}>setStarted</button>
164 <div>isFetching: {isFetching}</div>
165 {started ? (
166 <>
167 <One />
168 <Two />
169 </>
170 ) : null}
171 </div>
172 )
173 }
174β
175 const rendered = renderWithClient(queryClient, <Page />)
176β
177 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
178β
179 fireEvent.click(rendered.getByRole('button', { name: /setStarted/i }))
180 await vi.advanceTimersByTimeAsync(0)
181 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
182 await vi.advanceTimersByTimeAsync(11)
183 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
184β
185 // at no point should we have isFetching: 2
186 expect(isFetchingArray).toStrictEqual(expect.not.arrayContaining([2]))
187 })
188β
189 it('should show the correct fetching state when mounted after a query', async () => {
190 const queryClient = new QueryClient()
191 const key = createKey()
192β
193 function Page() {
194 useQuery({
195 queryKey: key,
196 queryFn: () => sleep(10).then(() => 'test'),
197 })
198β
199 const isFetching = useIsFetching()
200β
201 return (
202 <div>
203 <div>isFetching: {isFetching}</div>
204 </div>
205 )
206 }
207β
208 const rendered = renderWithClient(queryClient, <Page />)
209β
210 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
211 await vi.advanceTimersByTimeAsync(11)
212 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
213 })
214β
215 it('should use provided custom queryClient', async () => {
216 const onSuccess =
217 vi.fn<(data: unknown, query: Query<unknown, unknown, unknown>) => void>()
218β
219 const queryCache = new QueryCache({ onSuccess })
220 const queryClient = new QueryClient({ queryCache })
221 const key = createKey()
222β
223 function Page() {
224 useQuery(
225 {
226 queryKey: key,
227 queryFn: () => sleep(10).then(() => 'test'),
228 },
229 queryClient,
230 )
231β
232 const isFetching = useIsFetching({}, queryClient)
233β
234 return (
235 <div>
236 <div>isFetching: {isFetching}</div>
237 </div>
238 )
239 }
240β
241 const rendered = render(<Page />)
242β
243 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
244 await vi.advanceTimersByTimeAsync(11)
245 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
246 expect(onSuccess).toHaveBeenCalledTimes(1)
247 })
248})