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

devtools.test.tsx

439 lines12.4 KB

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})