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

useIsFetching.test.tsx

298 lines7.2 KB

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