Open History to see the last commit
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})