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

useIsFetching.test.tsx

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