Open History to see the last commit
1import { fireEvent, render, screen, waitFor } from 'solid-testing-library'
2β
3import {
4 createContext,
5 createEffect,
6 createRenderEffect,
7 createSignal,
8 ErrorBoundary,
9 Show,
10} from 'solid-js'
11import type { QueryClient } from '..'
12import { createQuery, QueryCache, QueryClientProvider, useIsFetching } from '..'
13import { createQueryClient, queryKey, setActTimeout, sleep } from './utils'
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})