Open History to see the last commit
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})