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