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

useIsFetching.test.tsx

268 lines5.8 KB

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