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

useIsFetching.test.tsx

247 lines6.1 KB

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