Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useIsFetching.test.tsx

305 lines8.1 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render } from '@solidjs/testing-library'
3import {
4 Show,
5 createRenderEffect,
6 createSignal,
7 createTrackedEffect,
8} from 'solid-js'
9import { createKey, sleep } from '@tanstack/query-test-utils'
10import { QueryCache, QueryClient, useIsFetching, useQuery } from '..'
11import { renderWithClient, setActTimeout } from './utils'
12​
13describe('useIsFetching', () => {
14 let queryCache: QueryCache
15 let queryClient: QueryClient
16​
17 beforeEach(() => {
18 vi.useFakeTimers()
19 queryCache = new QueryCache()
20 queryClient = new QueryClient({ queryCache })
21 })
22​
23 afterEach(() => {
24 queryClient.clear()
25 vi.useRealTimers()
26 })
27​
28 // See https://github.com/tannerlinsley/react-query/issues/105
29 it('should update as queries start and stop fetching', async () => {
30 const key = createKey()
31​
32 function IsFetching() {
33 const isFetching = useIsFetching()
34​
35 return <div>isFetching: {isFetching()}</div>
36 }
37​
38 function Query() {
39 const [ready, setReady] = createSignal(false)
40​
41 useQuery(() => ({
42 queryKey: key,
43 queryFn: () => sleep(50).then(() => 'test'),
44 enabled: ready(),
45 }))
46​
47 return <button onClick={() => setReady(true)}>setReady</button>
48 }
49​
50 function Page() {
51 return (
52 <div>
53 <IsFetching />
54 <Query />
55 </div>
56 )
57 }
58​
59 const rendered = renderWithClient(queryClient, () => <Page />)
60​
61 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
62​
63 fireEvent.click(rendered.getByRole('button', { name: /setReady/i }))
64 // Flush the setTimeout(0) used by notifyManager's scheduler so the
65 // cache subscription fires and useIsFetching updates.
66 await vi.advanceTimersByTimeAsync(0)
67 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
68 // Advance past the 50ms query sleep, plus flush the completion notification
69 await vi.advanceTimersByTimeAsync(50)
70 await vi.advanceTimersByTimeAsync(0)
71 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
72 })
73​
74 it('should not update state while rendering', async () => {
75 const key1 = createKey()
76 const key2 = createKey()
77​
78 const isFetchingArray: Array<number> = []
79​
80 function IsFetching() {
81 const isFetching = useIsFetching()
82​
83 createRenderEffect(isFetching, (i) => {
84 isFetchingArray.push(i)
85 })
86​
87 return null
88 }
89​
90 function FirstQuery() {
91 useQuery(() => ({
92 queryKey: key1,
93 queryFn: () => sleep(150).then(() => 'data'),
94 }))
95​
96 return null
97 }
98​
99 function SecondQuery() {
100 useQuery(() => ({
101 queryKey: key2,
102 queryFn: () => sleep(200).then(() => 'data'),
103 }))
104​
105 return null
106 }
107​
108 function Page() {
109 const [renderSecond, setRenderSecond] = createSignal(false)
110​
111 createTrackedEffect(() => {
112 setActTimeout(() => {
113 setRenderSecond(true)
114 }, 100)
115 })
116​
117 return (
118 <>
119 <IsFetching />
120 <FirstQuery />
121 <Show when={renderSecond()}>
122 <SecondQuery />
123 </Show>
124 </>
125 )
126 }
127​
128 renderWithClient(queryClient, () => <Page />)
129​
130 // unlike react, Updating renderSecond wont cause a rerender for FirstQuery
131 await vi.advanceTimersByTimeAsync(300)
132 expect(isFetchingArray).toStrictEqual([0, 1, 2, 1, 0])
133 })
134​
135 it('should be able to filter', async () => {
136 const key1 = createKey()
137 const key2 = createKey()
138​
139 const isFetchingArray: Array<number> = []
140​
141 function One() {
142 useQuery(() => ({
143 queryKey: key1,
144 queryFn: () => sleep(10).then(() => 'test'),
145 }))
146​
147 return null
148 }
149​
150 function Two() {
151 useQuery(() => ({
152 queryKey: key2,
153 queryFn: () => sleep(20).then(() => 'test'),
154 }))
155​
156 return null
157 }
158​
159 function Page() {
160 const [started, setStarted] = createSignal(false)
161 const isFetching = useIsFetching(() => ({
162 queryKey: key1,
163 }))
164​
165 createRenderEffect(isFetching, (i) => {
166 isFetchingArray.push(i)
167 })
168​
169 return (
170 <div>
171 <button onClick={() => setStarted(true)}>setStarted</button>
172 <div>isFetching: {isFetching()}</div>
173 <Show when={started()}>
174 <>
175 <One />
176 <Two />
177 </>
178 </Show>
179 </div>
180 )
181 }
182​
183 const rendered = renderWithClient(queryClient, () => <Page />)
184​
185 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
186​
187 fireEvent.click(rendered.getByRole('button', { name: /setStarted/i }))
188 // Flush the setTimeout(0) used by notifyManager's scheduler so the
189 // cache subscription fires and useIsFetching updates.
190 await vi.advanceTimersByTimeAsync(0)
191 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
192 await vi.advanceTimersByTimeAsync(20)
193 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
194​
195 // at no point should we have isFetching: 2
196 expect(isFetchingArray).toStrictEqual(expect.not.arrayContaining([2]))
197 })
198​
199 it('should show the correct fetching state when mounted after a query', async () => {
200 const key = createKey()
201​
202 function Page() {
203 useQuery(() => ({
204 queryKey: key,
205 queryFn: () => sleep(10).then(() => 'test'),
206 }))
207​
208 const isFetching = useIsFetching()
209​
210 return (
211 <div>
212 <div>isFetching: {isFetching()}</div>
213 </div>
214 )
215 }
216​
217 const rendered = renderWithClient(queryClient, () => <Page />)
218​
219 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
220 await vi.advanceTimersByTimeAsync(10)
221 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
222 })
223​
224 it('should use provided custom queryClient', async () => {
225 const customClient = new QueryClient()
226 const key = createKey()
227​
228 function Page() {
229 useQuery(
230 () => ({
231 queryKey: key,
232 queryFn: () => sleep(10).then(() => 'test'),
233 }),
234 () => customClient,
235 )
236​
237 const isFetching = useIsFetching(undefined, () => customClient)
238​
239 return (
240 <div>
241 <div>isFetching: {isFetching()}</div>
242 </div>
243 )
244 }
245​
246 const rendered = render(() => <Page />)
247​
248 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
249 await vi.advanceTimersByTimeAsync(10)
250 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
251 })
252​
253 // The v6 cache aggregate subscribes once and does not follow a changed client accessor yet.
254 it.skip('should resubscribe when a custom queryClient changes', async () => {
255 const queryClient1 = new QueryClient()
256 const queryClient2 = new QueryClient()
257 const key1 = createKey()
258 const key2 = createKey()
259 const [client, setClient] = createSignal(queryClient1)
260 const queryCache1 = queryClient1.getQueryCache()
261 const originalSubscribe1 = queryCache1.subscribe.bind(queryCache1)
262 const unsubscribe1 = vi.fn<() => void>()
263​
264 vi.spyOn(queryCache1, 'subscribe').mockImplementation((listener) => {
265 const cleanup = originalSubscribe1(listener)
266​
267 return () => {
268 unsubscribe1()
269 cleanup()
270 }
271 })
272​
273 function Page() {
274 const isFetching = useIsFetching(undefined, client)
275​
276 return <div>isFetching: {isFetching()}</div>
277 }
278​
279 const rendered = render(() => <Page />)
280​
281 const firstQuery = queryClient1.query({
282 queryKey: key1,
283 queryFn: () => sleep(20).then(() => 'test1'),
284 })
285​
286 await vi.advanceTimersByTimeAsync(0)
287 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
288​
289 setClient(queryClient2)
290 await vi.advanceTimersByTimeAsync(0)
291 expect(unsubscribe1).toHaveBeenCalledTimes(1)
292 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
293​
294 const secondQuery = queryClient2.query({
295 queryKey: key2,
296 queryFn: () => sleep(20).then(() => 'test2'),
297 })
298​
299 await vi.advanceTimersByTimeAsync(0)
300 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
301 await vi.advanceTimersByTimeAsync(20)
302 await Promise.all([firstQuery, secondQuery])
303 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
304 })
305})