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