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