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