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