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

useIsFetching.test.tsx

274 lines6.6 KB

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