Open History to see the last commit
1import { fireEvent, waitFor } from '@testing-library/react'
2import React from 'react'
3β
4import {
5 mockConsoleError,
6 queryKey,
7 renderWithClient,
8 setActTimeout,
9 sleep,
10} from './utils'
11import { useQuery, useIsFetching, QueryClient, QueryCache } from '../..'
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 = new QueryClient({ queryCache })
18 const key = queryKey()
19β
20 function Page() {
21 const [ready, setReady] = React.useState(false)
22β
23 const isFetching = useIsFetching()
24β
25 useQuery(
26 key,
27 async () => {
28 await sleep(1000)
29 return 'test'
30 },
31 {
32 enabled: ready,
33 }
34 )
35β
36 return (
37 <div>
38 <div>isFetching: {isFetching}</div>
39 <button onClick={() => setReady(true)}>setReady</button>
40 </div>
41 )
42 }
43β
44 const rendered = renderWithClient(queryClient, <Page />)
45β
46 await waitFor(() => rendered.getByText('isFetching: 0'))
47 fireEvent.click(rendered.getByText('setReady'))
48 await waitFor(() => rendered.getByText('isFetching: 1'))
49 await waitFor(() => rendered.getByText('isFetching: 0'))
50 })
51β
52 it('should not update state while rendering', async () => {
53 const consoleMock = mockConsoleError()
54 const queryCache = new QueryCache()
55 const queryClient = new QueryClient({ queryCache })
56β
57 const key1 = queryKey()
58 const key2 = queryKey()
59β
60 const isFetchings: number[] = []
61β
62 function IsFetching() {
63 const isFetching = useIsFetching()
64 isFetchings.push(isFetching)
65 return null
66 }
67β
68 function FirstQuery() {
69 useQuery(key1, async () => {
70 await sleep(100)
71 return 'data'
72 })
73 return null
74 }
75β
76 function SecondQuery() {
77 useQuery(key2, async () => {
78 await sleep(100)
79 return 'data'
80 })
81 return null
82 }
83β
84 function Page() {
85 const [renderSecond, setRenderSecond] = React.useState(false)
86β
87 React.useEffect(() => {
88 setActTimeout(() => {
89 setRenderSecond(true)
90 }, 50)
91 }, [])
92β
93 return (
94 <>
95 <FirstQuery />
96 {renderSecond && <SecondQuery />}
97 <IsFetching />
98 </>
99 )
100 }
101β
102 renderWithClient(queryClient, <Page />)
103 await waitFor(() => expect(isFetchings).toEqual([0, 1, 1, 2, 1, 0]))
104 expect(consoleMock).not.toHaveBeenCalled()
105 expect(consoleMock.mock.calls[0]?.[0] ?? '').not.toMatch('setState')
106β
107 consoleMock.mockRestore()
108 })
109β
110 it('should be able to filter', async () => {
111 const queryCache = new QueryCache()
112 const queryClient = new QueryClient({ queryCache })
113 const key1 = queryKey()
114 const key2 = queryKey()
115β
116 const isFetchings: number[] = []
117β
118 function One() {
119 useQuery(key1, async () => {
120 await sleep(10)
121 return 'test'
122 })
123 return null
124 }
125β
126 function Two() {
127 useQuery(key2, async () => {
128 await sleep(20)
129 return 'test'
130 })
131 return null
132 }
133β
134 function Page() {
135 const [started, setStarted] = React.useState(false)
136 const isFetching = useIsFetching(key1)
137 isFetchings.push(isFetching)
138β
139 React.useEffect(() => {
140 setActTimeout(() => {
141 setStarted(true)
142 }, 5)
143 }, [])
144β
145 if (!started) {
146 return null
147 }
148β
149 return (
150 <div>
151 <One />
152 <Two />
153 </div>
154 )
155 }
156β
157 renderWithClient(queryClient, <Page />)
158β
159 await sleep(100)
160 expect(isFetchings).toEqual([0, 0, 1, 0])
161 })
162β
163 it('should show the correct fetching state when mounted after a query', async () => {
164 const queryClient = new QueryClient()
165 const key = queryKey()
166β
167 function Page() {
168 useQuery(key, async () => {
169 await sleep(10)
170 return 'test'
171 })
172β
173 const isFetching = useIsFetching()
174β
175 return (
176 <div>
177 <div>isFetching: {isFetching}</div>
178 </div>
179 )
180 }
181β
182 const rendered = renderWithClient(queryClient, <Page />)
183β
184 await rendered.findByText('isFetching: 1')
185 await rendered.findByText('isFetching: 0')
186 })
187})