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

useIsFetching.test.tsx

253 lines6.4 KB

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