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

useIsFetching.test.tsx

240 lines5.9 KB

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