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

useIsFetching.test.tsx

246 lines6.1 KB

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