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