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

useIsFetching.test.tsx

267 lines6.3 KB

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