Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useIsFetching.test.tsx

274 lines6.5 KB

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