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