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