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