Open History to see the last commit
1import React from 'react'
2import {
3 fireEvent,
4 screen,
5 waitFor,
6 act,
7 waitForElementToBeRemoved,
8} from '@testing-library/react'
9import '@testing-library/jest-dom'
10import { useQuery } from '../..'
11import {
12 getByTextContent,
13 renderWithClient,
14 sleep,
15 createQueryClient,
16} from './utils'
17β
18describe('ReactQueryDevtools', () => {
19 it('should be able to open and close devtools', async () => {
20 const { queryClient } = createQueryClient()
21β
22 function Page() {
23 const { data = 'default' } = useQuery('check', async () => {
24 await sleep(10)
25 return 'test'
26 })
27β
28 return (
29 <div>
30 <h1>{data}</h1>
31 </div>
32 )
33 }
34β
35 renderWithClient(queryClient, <Page />, { initialIsOpen: false })
36β
37 const closeButton = screen.queryByRole('button', {
38 name: /close react query devtools/i,
39 })
40 expect(closeButton).toBeNull()
41 fireEvent.click(
42 screen.getByRole('button', { name: /open react query devtools/i })
43 )
44β
45 await waitForElementToBeRemoved(() =>
46 screen.queryByRole('button', { name: /open react query devtools/i })
47 )
48 fireEvent.click(
49 screen.getByRole('button', { name: /close react query devtools/i })
50 )
51β
52 await screen.findByRole('button', { name: /open react query devtools/i })
53 })
54β
55 it('should display the correct query states', async () => {
56 const { queryClient, queryCache } = createQueryClient()
57β
58 function Page() {
59 const { data = 'default' } = useQuery(
60 'check',
61 async () => {
62 await sleep(100)
63 return 'test'
64 },
65 { staleTime: 300 }
66 )
67β
68 return (
69 <div>
70 <h1>{data}</h1>
71 </div>
72 )
73 }
74β
75 function PageParent() {
76 const [isPageVisible, togglePageVisible] = React.useReducer(
77 visible => !visible,
78 true
79 )
80β
81 return (
82 <div>
83 <button
84 type="button"
85 aria-label="Toggle page visibility"
86 onClick={togglePageVisible}
87 >
88 Toggle Page
89 </button>
90 {isPageVisible && <Page />}
91 </div>
92 )
93 }
94β
95 renderWithClient(queryClient, <PageParent />)
96β
97 fireEvent.click(
98 screen.getByRole('button', { name: /open react query devtools/i })
99 )
100β
101 const currentQuery = queryCache.find('check')
102β
103 // When the query is fetching then expect number of
104 // fetching queries to be 1
105 expect(currentQuery?.isFetching()).toEqual(true)
106 await screen.findByText(
107 getByTextContent('fresh (0) fetching (1) stale (0) inactive (0)')
108 )
109β
110 // When we are done fetching the query doesn't go stale
111 // until 300ms after, so expect the number of fresh
112 // queries to be 1
113 await waitFor(() => {
114 expect(currentQuery?.isFetching()).toEqual(false)
115 })
116 await screen.findByText(
117 getByTextContent('fresh (1) fetching (0) stale (0) inactive (0)')
118 )
119β
120 // Then wait for the query to go stale and then
121 // expect the number of stale queries to be 1
122 await waitFor(() => {
123 expect(currentQuery?.isStale()).toEqual(false)
124 })
125 await screen.findByText(
126 getByTextContent('fresh (0) fetching (0) stale (1) inactive (0)')
127 )
128β
129 // Unmount the page component thus making the query inactive
130 // and expect number of inactive queries to be 1
131 fireEvent.click(
132 screen.getByRole('button', { name: /toggle page visibility/i })
133 )
134 await screen.findByText(
135 getByTextContent('fresh (0) fetching (0) stale (0) inactive (1)')
136 )
137 })
138β
139 it('should display the query hash and open the query details', async () => {
140 const { queryClient, queryCache } = createQueryClient()
141β
142 function Page() {
143 const { data = 'default' } = useQuery('check', async () => {
144 await sleep(10)
145 return 'test'
146 })
147β
148 return (
149 <div>
150 <h1>{data}</h1>
151 </div>
152 )
153 }
154β
155 renderWithClient(queryClient, <Page />)
156β
157 fireEvent.click(
158 screen.getByRole('button', { name: /open react query devtools/i })
159 )
160β
161 const currentQuery = queryCache.find('check')
162β
163 await screen.findByText(getByTextContent(`1${currentQuery?.queryHash}`))
164β
165 fireEvent.click(
166 screen.getByRole('button', {
167 name: `Open query details for ${currentQuery?.queryHash}`,
168 })
169 )
170β
171 await screen.findByText(/query details/i)
172 })
173β
174 it('should filter the queries via the query hash', async () => {
175 const { queryClient, queryCache } = createQueryClient()
176β
177 function Page() {
178 const fooResult = useQuery('foo', async () => {
179 await sleep(10)
180 return 'foo-result'
181 })
182β
183 const barResult = useQuery('bar', async () => {
184 await sleep(10)
185 return 'bar-result'
186 })
187β
188 const bazResult = useQuery('baz', async () => {
189 await sleep(10)
190 return 'baz-result'
191 })
192β
193 return (
194 <div>
195 <h1>
196 {barResult.data} {fooResult.data} {bazResult.data}
197 </h1>
198 </div>
199 )
200 }
201β
202 renderWithClient(queryClient, <Page />)
203β
204 fireEvent.click(
205 screen.getByRole('button', { name: /open react query devtools/i })
206 )
207β
208 const fooQueryHash = queryCache.find('foo')?.queryHash ?? 'invalid hash'
209 const barQueryHash = queryCache.find('bar')?.queryHash ?? 'invalid hash'
210 const bazQueryHash = queryCache.find('baz')?.queryHash ?? 'invalid hash'
211β
212 await screen.findByText(fooQueryHash)
213 screen.getByText(barQueryHash)
214 screen.getByText(bazQueryHash)
215β
216 const filterInput = screen.getByLabelText(/filter by queryhash/i)
217 fireEvent.change(filterInput, { target: { value: 'fo' } })
218β
219 await screen.findByText(fooQueryHash)
220 const barItem = screen.queryByText(barQueryHash)
221 const bazItem = screen.queryByText(bazQueryHash)
222 expect(barItem).toBeNull()
223 expect(bazItem).toBeNull()
224β
225 fireEvent.change(filterInput, { target: { value: '' } })
226 })
227β
228 it('should show a disabled label if all observers are disabled', async () => {
229 const { queryClient } = createQueryClient()
230β
231 function Page() {
232 const [enabled, setEnabled] = React.useState(false)
233 const { data } = useQuery(
234 'key',
235 async () => {
236 await sleep(10)
237 return 'test'
238 },
239 {
240 enabled,
241 }
242 )
243β
244 return (
245 <div>
246 <h1>{data}</h1>
247 <button onClick={() => setEnabled(true)}>Enable Query</button>
248 </div>
249 )
250 }
251β
252 renderWithClient(queryClient, <Page />, { initialIsOpen: true })
253β
254 await screen.findByText(/disabled/i)
255β
256 await act(async () => {
257 fireEvent.click(await screen.findByText(/enable query/i))
258 })
259β
260 expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
261 })
262β
263 it('should not show a disabled label for inactive queries', async () => {
264 const { queryClient } = createQueryClient()
265β
266 function Page() {
267 const { data } = useQuery('key', () => Promise.resolve('test'), {
268 enabled: false,
269 })
270β
271 return (
272 <div>
273 <h1>{data}</h1>
274 </div>
275 )
276 }
277β
278 function App() {
279 const [visible, setVisible] = React.useState(true)
280β
281 return (
282 <div>
283 {visible ? <Page /> : null}
284 <button onClick={() => setVisible(false)}>Hide Query</button>
285 </div>
286 )
287 }
288β
289 renderWithClient(queryClient, <App />, { initialIsOpen: true })
290β
291 await screen.findByText(/disabled/i)
292β
293 await act(async () => {
294 fireEvent.click(await screen.findByText(/hide query/i))
295 })
296β
297 expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
298 })
299β
300 it('should sort the queries according to the sorting filter', async () => {
301 const { queryClient, queryCache } = createQueryClient()
302β
303 function Page() {
304 const query1Result = useQuery('query-1', async () => {
305 await sleep(20)
306 return 'query-1-result'
307 })
308β
309 const query2Result = useQuery('query-2', async () => {
310 await sleep(60)
311 return 'query-2-result'
312 })
313β
314 const query3Result = useQuery(
315 'query-3',
316 async () => {
317 await sleep(40)
318 return 'query-3-result'
319 },
320 { staleTime: Infinity }
321 )
322β
323 return (
324 <div>
325 <h1>
326 {query1Result.data} {query2Result.data} {query3Result.data}
327 </h1>
328 </div>
329 )
330 }
331β
332 renderWithClient(queryClient, <Page />)
333β
334 fireEvent.click(
335 screen.getByRole('button', { name: /open react query devtools/i })
336 )
337β
338 const query1Hash = queryCache.find('query-1')?.queryHash ?? 'invalid hash'
339 const query2Hash = queryCache.find('query-2')?.queryHash ?? 'invalid hash'
340 const query3Hash = queryCache.find('query-3')?.queryHash ?? 'invalid hash'
341β
342 const sortSelect = screen.getByLabelText(/sort queries/i)
343 let queries = []
344β
345 // When sorted by query hash the queries get sorted according
346 // to just the number, with the order being -> query-1, query-2, query-3
347 fireEvent.change(sortSelect, { target: { value: 'Query Hash' } })
348β
349 /** To check the order of the queries we can use regex to find
350 * all the row items in an array and then compare the items
351 * one by one in the order we expect it
352 * @reference https://github.com/testing-library/react-testing-library/issues/313#issuecomment-625294327
353 */
354 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
355 expect(queries[0]?.textContent).toEqual(query1Hash)
356 expect(queries[1]?.textContent).toEqual(query2Hash)
357 expect(queries[2]?.textContent).toEqual(query3Hash)
358β
359 // Wait for the queries to be resolved
360 await sleep(70)
361β
362 // When sorted by the last updated date the queries are sorted by the time
363 // they were updated and since the query-2 takes longest time to complete
364 // and query-1 the shortest, so the order is -> query-2, query-3, query-1
365 fireEvent.change(sortSelect, { target: { value: 'Last Updated' } })
366β
367 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
368 expect(queries[0]?.textContent).toEqual(query2Hash)
369 expect(queries[1]?.textContent).toEqual(query3Hash)
370 expect(queries[2]?.textContent).toEqual(query1Hash)
371β
372 // When sorted by the status and then last updated date the queries
373 // query-3 takes precedence because its stale time being infinity, it
374 // always remains fresh, the rest of the queries are sorted by their last
375 // updated time, so the resulting order is -> query-3, query-2, query-1
376 fireEvent.change(sortSelect, {
377 target: { value: 'Status > Last Updated' },
378 })
379β
380 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
381 expect(queries[0]?.textContent).toEqual(query3Hash)
382 expect(queries[1]?.textContent).toEqual(query2Hash)
383 expect(queries[2]?.textContent).toEqual(query1Hash)
384β
385 // Switch the order form ascending to descending and expect the
386 // query order to be reversed from previous state
387 fireEvent.click(screen.getByRole('button', { name: /β¬ asc/i }))
388β
389 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
390 expect(queries[0]?.textContent).toEqual(query1Hash)
391 expect(queries[1]?.textContent).toEqual(query2Hash)
392 expect(queries[2]?.textContent).toEqual(query3Hash)
393 })
394})