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 expect(isFetchingArray).toEqual([0, 1, 2, 1, 0])
123 })
124β
125 it('should be able to filter', async () => {
126 const key1 = queryKey()
127 const key2 = queryKey()
128β
129 const isFetchingArray: Array<number> = []
130β
131 function One() {
132 useQuery(() => ({
133 queryKey: key1,
134 queryFn: () => sleep(10).then(() => 'test'),
135 }))
136β
137 return null
138 }
139β
140 function Two() {
141 useQuery(() => ({
142 queryKey: key2,
143 queryFn: () => sleep(20).then(() => 'test'),
144 }))
145β
146 return null
147 }
148β
149 function Page() {
150 const [started, setStarted] = createSignal(false)
151 const isFetching = useIsFetching(() => ({
152 queryKey: key1,
153 }))
154β
155 createRenderEffect(() => {
156 isFetchingArray.push(isFetching())
157 })
158β
159 return (
160 <div>
161 <button onClick={() => setStarted(true)}>setStarted</button>
162 <div>isFetching: {isFetching()}</div>
163 <Show when={started()}>
164 <>
165 <One />
166 <Two />
167 </>
168 </Show>
169 </div>
170 )
171 }
172β
173 const rendered = renderWithClient(queryClient, () => <Page />)
174β
175 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
176β
177 fireEvent.click(rendered.getByRole('button', { name: /setStarted/i }))
178 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
179 await vi.advanceTimersByTimeAsync(20)
180 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
181β
182 // at no point should we have isFetching: 2
183 expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
184 })
185β
186 it('should show the correct fetching state when mounted after a query', async () => {
187 const key = queryKey()
188β
189 function Page() {
190 useQuery(() => ({
191 queryKey: key,
192 queryFn: () => sleep(10).then(() => 'test'),
193 }))
194β
195 const isFetching = useIsFetching()
196β
197 return (
198 <div>
199 <div>isFetching: {isFetching()}</div>
200 </div>
201 )
202 }
203β
204 const rendered = renderWithClient(queryClient, () => <Page />)
205β
206 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
207 await vi.advanceTimersByTimeAsync(10)
208 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
209 })
210β
211 it('should use provided custom queryClient', async () => {
212 const customClient = new QueryClient()
213 const key = queryKey()
214β
215 function Page() {
216 useQuery(
217 () => ({
218 queryKey: key,
219 queryFn: () => sleep(10).then(() => 'test'),
220 }),
221 () => customClient,
222 )
223β
224 const isFetching = useIsFetching(undefined, () => customClient)
225β
226 return (
227 <div>
228 <div>isFetching: {isFetching()}</div>
229 </div>
230 )
231 }
232β
233 const rendered = render(() => <Page />)
234β
235 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
236 await vi.advanceTimersByTimeAsync(10)
237 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
238 })
239β
240 it('should resubscribe when a custom queryClient changes', async () => {
241 const queryClient1 = new QueryClient()
242 const queryClient2 = new QueryClient()
243 const key1 = queryKey()
244 const key2 = queryKey()
245 const [client, setClient] = createSignal(queryClient1)
246 const queryCache1 = queryClient1.getQueryCache()
247 const originalSubscribe1 = queryCache1.subscribe.bind(queryCache1)
248 const unsubscribe1 = vi.fn()
249β
250 vi.spyOn(queryCache1, 'subscribe').mockImplementation((listener) => {
251 const cleanup = originalSubscribe1(listener)
252β
253 return () => {
254 unsubscribe1()
255 cleanup()
256 }
257 })
258β
259 function Page() {
260 const isFetching = useIsFetching(undefined, client)
261β
262 return <div>isFetching: {isFetching()}</div>
263 }
264β
265 const rendered = render(() => <Page />)
266β
267 const firstQuery = queryClient1.query({
268 queryKey: key1,
269 queryFn: () => sleep(20).then(() => 'test1'),
270 })
271β
272 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
273β
274 setClient(queryClient2)
275 expect(unsubscribe1).toHaveBeenCalledTimes(1)
276 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
277β
278 const secondQuery = queryClient2.query({
279 queryKey: key2,
280 queryFn: () => sleep(20).then(() => 'test2'),
281 })
282β
283 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
284 await vi.advanceTimersByTimeAsync(20)
285 await Promise.all([firstQuery, secondQuery])
286 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
287 })
288})