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

useIsFetching.test.tsx

252 lines5.8 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 queryCache = new QueryCache()
20 const queryClient = new QueryClient({ queryCache })
21 const key = queryKey()
22​
23 function IsFetching() {
24 const isFetching = useIsFetching()
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: async () => {
34 await sleep(50)
35 return 'test'
36 },
37 enabled: ready,
38 })
39​
40 return <button onClick={() => setReady(true)}>setReady</button>
41 }
42​
43 function Page() {
44 return (
45 <div>
46 <IsFetching />
47 <Query />
48 </div>
49 )
50 }
51​
52 const { getByText, getByRole } = renderWithClient(queryClient, <Page />)
53​
54 expect(getByText('isFetching: 0')).toBeInTheDocument()
55​
56 fireEvent.click(getByRole('button', { name: /setReady/i }))
57​
58 await vi.advanceTimersByTimeAsync(0)
59 expect(getByText('isFetching: 1')).toBeInTheDocument()
60​
61 await vi.advanceTimersByTimeAsync(51)
62 expect(getByText('isFetching: 0')).toBeInTheDocument()
63 })
64​
65 it('should not update state while rendering', async () => {
66 const queryCache = new QueryCache()
67 const queryClient = new QueryClient({ queryCache })
68​
69 const key1 = queryKey()
70 const key2 = queryKey()
71​
72 const isFetchingArray: Array<number> = []
73​
74 function IsFetching() {
75 const isFetching = useIsFetching()
76 isFetchingArray.push(isFetching)
77 return null
78 }
79​
80 function FirstQuery() {
81 useQuery({
82 queryKey: key1,
83 queryFn: async () => {
84 await sleep(100)
85 return 'data'
86 },
87 })
88 return null
89 }
90​
91 function SecondQuery() {
92 useQuery({
93 queryKey: key2,
94 queryFn: async () => {
95 await sleep(100)
96 return 'data'
97 },
98 })
99 return null
100 }
101​
102 function Page() {
103 const [renderSecond, setRenderSecond] = React.useState(false)
104​
105 React.useEffect(() => {
106 setActTimeout(() => {
107 setRenderSecond(true)
108 }, 50)
109 }, [])
110​
111 return (
112 <>
113 <IsFetching />
114 <FirstQuery />
115 {renderSecond && <SecondQuery />}
116 </>
117 )
118 }
119​
120 renderWithClient(queryClient, <Page />)
121​
122 await vi.advanceTimersByTimeAsync(151)
123 expect(isFetchingArray).toEqual([0, 1, 1, 2, 1, 0])
124 })
125​
126 it('should be able to filter', async () => {
127 const queryClient = new QueryClient()
128 const key1 = queryKey()
129 const key2 = queryKey()
130​
131 const isFetchingArray: Array<number> = []
132​
133 function One() {
134 useQuery({
135 queryKey: key1,
136 queryFn: async () => {
137 await sleep(10)
138 return 'test'
139 },
140 })
141 return null
142 }
143​
144 function Two() {
145 useQuery({
146 queryKey: key2,
147 queryFn: async () => {
148 await sleep(20)
149 return 'test'
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 { getByText, getByRole } = renderWithClient(queryClient, <Page />)
176​
177 getByText('isFetching: 0')
178​
179 fireEvent.click(getByRole('button', { name: /setStarted/i }))
180​
181 await vi.advanceTimersByTimeAsync(0)
182 getByText('isFetching: 1')
183​
184 await vi.advanceTimersByTimeAsync(11)
185 getByText('isFetching: 0')
186​
187 // at no point should we have isFetching: 2
188 expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
189 })
190​
191 it('should show the correct fetching state when mounted after a query', async () => {
192 const queryClient = new QueryClient()
193 const key = queryKey()
194​
195 function Page() {
196 useQuery({
197 queryKey: key,
198 queryFn: async () => {
199 await sleep(10)
200 return 'test'
201 },
202 })
203​
204 const isFetching = useIsFetching()
205​
206 return (
207 <div>
208 <div>isFetching: {isFetching}</div>
209 </div>
210 )
211 }
212​
213 const rendered = renderWithClient(queryClient, <Page />)
214​
215 await vi.advanceTimersByTimeAsync(0)
216 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
217​
218 await vi.advanceTimersByTimeAsync(11)
219 expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
220 })
221​
222 it('should use provided custom queryClient', async () => {
223 const queryClient = new QueryClient()
224 const key = queryKey()
225​
226 function Page() {
227 useQuery(
228 {
229 queryKey: key,
230 queryFn: async () => {
231 await sleep(10)
232 return 'test'
233 },
234 },
235 queryClient,
236 )
237​
238 const isFetching = useIsFetching({}, queryClient)
239​
240 return (
241 <div>
242 <div>isFetching: {isFetching}</div>
243 </div>
244 )
245 }
246​
247 const rendered = render(<Page></Page>)
248​
249 await vi.advanceTimersByTimeAsync(0)
250 expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
251 })
252})