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

useIsFetching.test.tsx

253 lines5.8 KB

1import { fireEvent, render, screen, waitFor } from 'solid-testing-library'
2​
3import { createEffect, createRenderEffect, createSignal, Show } from 'solid-js'
4import { createQuery, QueryCache, QueryClientProvider, 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 render(() => (
44 <QueryClientProvider client={queryClient}>
45 <Page />
46 </QueryClientProvider>
47 ))
48​
49 await screen.findByText('isFetching: 0')
50 fireEvent.click(screen.getByRole('button', { name: /setReady/i }))
51 await screen.findByText('isFetching: 1')
52 await screen.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 isFetchings: number[] = []
63​
64 function IsFetching() {
65 const isFetching = useIsFetching()
66 createRenderEffect(() => {
67 isFetchings.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(isFetchings).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 isFetchings: 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 filters: {
156 queryKey: key1,
157 },
158 }))
159​
160 createRenderEffect(() => {
161 isFetchings.push(isFetching())
162 })
163​
164 return (
165 <div>
166 <button onClick={() => setStarted(true)}>setStarted</button>
167 <div>isFetching: {isFetching()}</div>
168 <Show when={started()}>
169 <>
170 <One />
171 <Two />
172 </>
173 </Show>
174 </div>
175 )
176 }
177​
178 render(() => (
179 <QueryClientProvider client={queryClient}>
180 <Page />
181 </QueryClientProvider>
182 ))
183​
184 await screen.findByText('isFetching: 0')
185 fireEvent.click(screen.getByRole('button', { name: /setStarted/i }))
186 await screen.findByText('isFetching: 1')
187 await screen.findByText('isFetching: 0')
188 // at no point should we have isFetching: 2
189 expect(isFetchings).toEqual(expect.not.arrayContaining([2]))
190 })
191​
192 it('should show the correct fetching state when mounted after a query', async () => {
193 const queryClient = createQueryClient()
194 const key = queryKey()
195​
196 function Page() {
197 createQuery(() => ({
198 queryKey: key,
199 queryFn: async () => {
200 await sleep(10)
201 return 'test'
202 },
203 }))
204​
205 const isFetching = useIsFetching()
206​
207 return (
208 <div>
209 <div>isFetching: {isFetching()}</div>
210 </div>
211 )
212 }
213​
214 render(() => (
215 <QueryClientProvider client={queryClient}>
216 <Page />
217 </QueryClientProvider>
218 ))
219​
220 await screen.findByText('isFetching: 1')
221 await screen.findByText('isFetching: 0')
222 })
223​
224 it('should use provided custom queryClient', async () => {
225 const queryClient = createQueryClient()
226 const key = queryKey()
227​
228 function Page() {
229 createQuery(
230 () => ({
231 queryKey: key,
232 queryFn: async () => {
233 await sleep(10)
234 return 'test'
235 },
236 }),
237 () => queryClient,
238 )
239​
240 const isFetching = useIsFetching(() => ({ queryClient }))
241​
242 return (
243 <div>
244 <div>isFetching: {isFetching}</div>
245 </div>
246 )
247 }
248​
249 render(() => <Page></Page>)
250​
251 await screen.findByText('isFetching: 1')
252 })
253})