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

useIsFetching.test.tsx

248 lines6.2 KB

1import { createKey, sleep } from '@tanstack/query-test-utils'
2import { fireEvent, render } from '@testing-library/react'
3import * as React from 'react'
4import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
5import { QueryCache, QueryClient, useIsFetching, useQuery } from '..'
6import { renderWithClient, setActTimeout } from './utils'
7import type { Query } from '..'
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 = createKey()
22​
23 function IsFetching() {
24 const isFetching = useIsFetching()
25​
26 return <div>isFetching: {isFetching}</div>
27 }
28​
29 function Query() {
30 const [ready, setReady] = React.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 = createKey()
65 const key2 = createKey()
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] = React.useState(false)
97​
98 React.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]).toBe(0)
116 await vi.advanceTimersByTimeAsync(0)
117 expect(isFetchingArray[1]).toBe(1)
118 await vi.advanceTimersByTimeAsync(50)
119 expect(isFetchingArray[2]).toBe(1)
120 await vi.advanceTimersByTimeAsync(1)
121 expect(isFetchingArray[3]).toBe(2)
122 await vi.advanceTimersByTimeAsync(50)
123 expect(isFetchingArray[4]).toBe(1)
124 await vi.advanceTimersByTimeAsync(50)
125 expect(isFetchingArray[5]).toBe(0)
126​
127 expect(isFetchingArray).toStrictEqual([0, 1, 1, 2, 1, 0])
128 })
129​
130 it('should be able to filter', async () => {
131 const queryClient = new QueryClient()
132 const key1 = createKey()
133 const key2 = createKey()
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] = React.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).toStrictEqual(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 = createKey()
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 =
217 vi.fn<(data: unknown, query: Query<unknown, unknown, unknown>) => void>()
218​
219 const queryCache = new QueryCache({ onSuccess })
220 const queryClient = new QueryClient({ queryCache })
221 const key = createKey()
222​
223 function Page() {
224 useQuery(
225 {
226 queryKey: key,
227 queryFn: () => sleep(10).then(() => 'test'),
228 },
229 queryClient,
230 )
231​
232 const isFetching = useIsFetching({}, queryClient)
233​
234 return (
235 <div>
236 <div>isFetching: {isFetching}</div>
237 </div>
238 )
239 }
240​
241 const rendered = render(<Page />)
242​
243 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
244 await vi.advanceTimersByTimeAsync(11)
245 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
246 expect(onSuccess).toHaveBeenCalledTimes(1)
247 })
248})