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

useIsFetching.test.tsx

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