Open History to see the last commit
1import * as React from 'react'
2import { fireEvent, screen, waitFor, act } from '@testing-library/react'
3import { ErrorBoundary } from 'react-error-boundary'
4import '@testing-library/jest-dom'
5import type { QueryClient } from '@tanstack/react-query'
6import { useQuery } from '@tanstack/react-query'
7import { defaultPanelSize, sortFns } from '../utils'
8import {
9 getByTextContent,
10 renderWithClient,
11 sleep,
12 createQueryClient,
13} from './utils'
14β
15// TODO: This should be removed with the types for react-error-boundary get updated.
16declare module 'react-error-boundary' {
17 interface ErrorBoundaryPropsWithFallback {
18 children: any
19 }
20}
21β
22Object.defineProperty(window, 'matchMedia', {
23 writable: true,
24 value: jest.fn().mockImplementation((query: string) => ({
25 matches: false,
26 media: query,
27 onchange: null,
28 addListener: jest.fn(), // deprecated
29 removeListener: jest.fn(), // deprecated
30 addEventListener: jest.fn(),
31 removeEventListener: jest.fn(),
32 dispatchEvent: jest.fn(),
33 })),
34})
35β
36describe('ReactQueryDevtools', () => {
37 beforeEach(() => {
38 localStorage.removeItem('reactQueryDevtoolsOpen')
39 localStorage.removeItem('reactQueryDevtoolsPanelPosition')
40 })
41 it('should be able to open and close devtools', async () => {
42 const { queryClient } = createQueryClient()
43 const onCloseClick = jest.fn()
44 const onToggleClick = jest.fn()
45β
46 function Page() {
47 const { data = 'default' } = useQuery(['check'], async () => {
48 await sleep(10)
49 return 'test'
50 })
51β
52 return (
53 <div>
54 <h1>{data}</h1>
55 </div>
56 )
57 }
58β
59 renderWithClient(queryClient, <Page />, {
60 initialIsOpen: false,
61 closeButtonProps: { onClick: onCloseClick },
62 toggleButtonProps: { onClick: onToggleClick },
63 })
64β
65 const verifyDevtoolsIsOpen = () => {
66 expect(
67 screen.queryByRole('generic', { name: /react query devtools panel/i }),
68 ).not.toBeNull()
69 expect(
70 screen.queryByRole('button', { name: /open react query devtools/i }),
71 ).toBeNull()
72 }
73 const verifyDevtoolsIsClosed = () => {
74 expect(
75 screen.queryByRole('generic', { name: /react query devtools panel/i }),
76 ).toBeNull()
77 expect(
78 screen.queryByRole('button', { name: /open react query devtools/i }),
79 ).not.toBeNull()
80 }
81β
82 const waitForDevtoolsToOpen = () =>
83 screen.findByRole('button', { name: /close react query devtools/i })
84 const waitForDevtoolsToClose = () =>
85 screen.findByRole('button', { name: /open react query devtools/i })
86β
87 const getOpenLogoButton = () =>
88 screen.getByRole('button', { name: /open react query devtools/i })
89 const getCloseLogoButton = () =>
90 screen.getByRole('button', { name: /close react query devtools/i })
91 const getCloseButton = () =>
92 screen.getByRole('button', { name: /^close$/i })
93β
94 verifyDevtoolsIsClosed()
95β
96 fireEvent.click(getOpenLogoButton())
97 await waitForDevtoolsToOpen()
98β
99 verifyDevtoolsIsOpen()
100β
101 fireEvent.click(getCloseLogoButton())
102 await waitForDevtoolsToClose()
103β
104 verifyDevtoolsIsClosed()
105β
106 fireEvent.click(getOpenLogoButton())
107 await waitForDevtoolsToOpen()
108β
109 verifyDevtoolsIsOpen()
110β
111 fireEvent.click(getCloseButton())
112 await waitForDevtoolsToClose()
113β
114 verifyDevtoolsIsClosed()
115 })
116β
117 it('should be able to drag devtools without error', async () => {
118 const { queryClient } = createQueryClient()
119β
120 function Page() {
121 const { data = 'default' } = useQuery(['check'], async () => {
122 await sleep(10)
123 return 'test'
124 })
125β
126 return (
127 <div>
128 <h1>{data}</h1>
129 </div>
130 )
131 }
132β
133 const result = renderWithClient(queryClient, <Page />, {
134 initialIsOpen: false,
135 })
136β
137 const draggableElement = result.container
138 .querySelector('#ReactQueryDevtoolsPanel')
139 ?.querySelector('div')
140β
141 if (!draggableElement) {
142 throw new Error('Could not find the draggable element')
143 }
144β
145 await act(async () => {
146 fireEvent.mouseDown(draggableElement)
147 })
148 })
149β
150 it('should display the correct query states', async () => {
151 const { queryClient, queryCache } = createQueryClient()
152β
153 function Page() {
154 const { data = 'default' } = useQuery(
155 ['check'],
156 async () => {
157 await sleep(100)
158 return 'test'
159 },
160 { staleTime: 300 },
161 )
162β
163 return (
164 <div>
165 <h1>{data}</h1>
166 </div>
167 )
168 }
169β
170 function PageParent() {
171 const [isPageVisible, togglePageVisible] = React.useReducer(
172 (visible) => !visible,
173 true,
174 )
175β
176 return (
177 <div>
178 <button
179 type="button"
180 aria-label="Toggle page visibility"
181 onClick={togglePageVisible}
182 >
183 Toggle Page
184 </button>
185 {isPageVisible && <Page />}
186 </div>
187 )
188 }
189β
190 renderWithClient(queryClient, <PageParent />)
191β
192 fireEvent.click(
193 screen.getByRole('button', { name: /open react query devtools/i }),
194 )
195β
196 const currentQuery = queryCache.find(['check'])
197β
198 // When the query is fetching then expect number of
199 // fetching queries to be 1
200 expect(currentQuery?.state.fetchStatus).toEqual('fetching')
201 await screen.findByText(
202 getByTextContent(
203 'fresh (0) fetching (1) paused (0) stale (0) inactive (0)',
204 ),
205 )
206β
207 // When we are done fetching the query doesn't go stale
208 // until 300ms after, so expect the number of fresh
209 // queries to be 1
210 await waitFor(() => {
211 expect(currentQuery?.state.fetchStatus).toEqual('idle')
212 })
213 await screen.findByText(
214 getByTextContent(
215 'fresh (1) fetching (0) paused (0) stale (0) inactive (0)',
216 ),
217 )
218β
219 // Then wait for the query to go stale and then
220 // expect the number of stale queries to be 1
221 await waitFor(() => {
222 expect(currentQuery?.isStale()).toEqual(false)
223 })
224 await screen.findByText(
225 getByTextContent(
226 'fresh (0) fetching (0) paused (0) stale (1) inactive (0)',
227 ),
228 )
229β
230 // Unmount the page component thus making the query inactive
231 // and expect number of inactive queries to be 1
232 fireEvent.click(
233 screen.getByRole('button', { name: /toggle page visibility/i }),
234 )
235 await screen.findByText(
236 getByTextContent(
237 'fresh (0) fetching (0) paused (0) stale (0) inactive (1)',
238 ),
239 )
240 })
241β
242 it('should display the query hash and open the query details', async () => {
243 const { queryClient, queryCache } = createQueryClient()
244β
245 function Page() {
246 const { data = 'default' } = useQuery(['check'], async () => {
247 await sleep(10)
248 return 'test'
249 })
250β
251 return (
252 <div>
253 <h1>{data}</h1>
254 </div>
255 )
256 }
257β
258 renderWithClient(queryClient, <Page />)
259β
260 fireEvent.click(
261 screen.getByRole('button', { name: /open react query devtools/i }),
262 )
263β
264 const currentQuery = queryCache.find(['check'])
265β
266 await screen.findByText(getByTextContent(`1${currentQuery?.queryHash}`))
267β
268 const queryButton = await screen.findByRole('button', {
269 name: `Open query details for ${currentQuery?.queryHash}`,
270 })
271β
272 fireEvent.click(queryButton)
273β
274 await screen.findByText(/query details/i)
275 })
276β
277 it('should filter the queries via the query hash', async () => {
278 const { queryClient, queryCache } = createQueryClient()
279β
280 function Page() {
281 const fooResult = useQuery(['foo'], async () => {
282 await sleep(10)
283 return 'foo-result'
284 })
285β
286 const barResult = useQuery(['bar'], async () => {
287 await sleep(10)
288 return 'bar-result'
289 })
290β
291 const bazResult = useQuery(['baz'], async () => {
292 await sleep(10)
293 return 'baz-result'
294 })
295β
296 return (
297 <div>
298 <h1>
299 {barResult.data} {fooResult.data} {bazResult.data}
300 </h1>
301 </div>
302 )
303 }
304β
305 renderWithClient(queryClient, <Page />)
306β
307 fireEvent.click(
308 screen.getByRole('button', { name: /open react query devtools/i }),
309 )
310β
311 const fooQueryHash = queryCache.find(['foo'])?.queryHash ?? 'invalid hash'
312 const barQueryHash = queryCache.find(['bar'])?.queryHash ?? 'invalid hash'
313 const bazQueryHash = queryCache.find(['baz'])?.queryHash ?? 'invalid hash'
314β
315 await screen.findByText(fooQueryHash)
316 screen.getByText(barQueryHash)
317 screen.getByText(bazQueryHash)
318β
319 const filterInput = screen.getByLabelText(/filter by queryhash/i)
320 fireEvent.change(filterInput, { target: { value: 'fo' } })
321β
322 await screen.findByText(fooQueryHash)
323 const barItem = screen.queryByText(barQueryHash)
324 const bazItem = screen.queryByText(bazQueryHash)
325 expect(barItem).toBeNull()
326 expect(bazItem).toBeNull()
327β
328 fireEvent.change(filterInput, { target: { value: '' } })
329 })
330β
331 it('should show a disabled label if all observers are disabled', async () => {
332 const { queryClient } = createQueryClient()
333β
334 function Page() {
335 const [enabled, setEnabled] = React.useState(false)
336 const { data } = useQuery(
337 ['key'],
338 async () => {
339 await sleep(10)
340 return 'test'
341 },
342 {
343 enabled,
344 },
345 )
346β
347 return (
348 <div>
349 <h1>{data}</h1>
350 <button onClick={() => setEnabled(true)}>Enable Query</button>
351 </div>
352 )
353 }
354β
355 renderWithClient(queryClient, <Page />, { initialIsOpen: true })
356β
357 await screen.findByText(/disabled/i)
358β
359 fireEvent.click(screen.getByRole('button', { name: /enable query/i }))
360β
361 await waitFor(() => {
362 expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
363 })
364 })
365β
366 it('should not show a disabled label for inactive queries', async () => {
367 const { queryClient } = createQueryClient()
368β
369 function Page() {
370 const { data } = useQuery(['key'], () => Promise.resolve('test'), {
371 enabled: false,
372 })
373β
374 return (
375 <div>
376 <h1>{data}</h1>
377 </div>
378 )
379 }
380β
381 function App() {
382 const [visible, setVisible] = React.useState(true)
383β
384 return (
385 <div>
386 {visible ? <Page /> : null}
387 <button onClick={() => setVisible(false)}>Hide Query</button>
388 </div>
389 )
390 }
391β
392 renderWithClient(queryClient, <App />, { initialIsOpen: true })
393β
394 await screen.findByText(/disabled/i)
395β
396 fireEvent.click(screen.getByRole('button', { name: /hide query/i }))
397β
398 await waitFor(() => {
399 expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
400 })
401 })
402β
403 it('should simulate offline mode', async () => {
404 const { queryClient } = createQueryClient()
405 let count = 0
406β
407 function App() {
408 const { data, fetchStatus } = useQuery(['key'], () => {
409 count++
410 return Promise.resolve('test')
411 })
412β
413 return (
414 <div>
415 <h1>
416 {data}, {fetchStatus}
417 </h1>
418 </div>
419 )
420 }
421β
422 renderWithClient(queryClient, <App />, {
423 initialIsOpen: true,
424 })
425β
426 await screen.findByRole('heading', { name: /test/i })
427β
428 fireEvent.click(
429 screen.getByRole('button', { name: /mock offline behavior/i }),
430 )
431β
432 const queryButton = await screen.findByRole('button', {
433 name: 'Open query details for ["key"]',
434 })
435 fireEvent.click(queryButton)
436β
437 const refetchButton = await screen.findByRole('button', {
438 name: /refetch/i,
439 })
440 fireEvent.click(refetchButton)
441β
442 await waitFor(() => {
443 expect(screen.getByText('test, paused')).toBeInTheDocument()
444 })
445β
446 fireEvent.click(
447 screen.getByRole('button', { name: /restore offline mock/i }),
448 )
449β
450 await waitFor(() => {
451 expect(screen.getByText('test, idle')).toBeInTheDocument()
452 })
453β
454 expect(count).toBe(2)
455 })
456β
457 it('should sort the queries according to the sorting filter', async () => {
458 const { queryClient, queryCache } = createQueryClient()
459β
460 function Page() {
461 const query1Result = useQuery(['query-1'], async () => {
462 await sleep(20)
463 return 'query-1-result'
464 })
465β
466 const query3Result = useQuery(
467 ['query-3'],
468 async () => {
469 await sleep(10)
470 return 'query-3-result'
471 },
472 { staleTime: Infinity, enabled: typeof query1Result.data === 'string' },
473 )
474β
475 const query2Result = useQuery(
476 ['query-2'],
477 async () => {
478 await sleep(10)
479 return 'query-2-result'
480 },
481 {
482 enabled: typeof query3Result.data === 'string',
483 },
484 )
485β
486 return (
487 <div>
488 <h1>
489 {query1Result.data} {query2Result.data} {query3Result.data}
490 </h1>
491 </div>
492 )
493 }
494β
495 renderWithClient(queryClient, <Page />)
496β
497 fireEvent.click(
498 screen.getByRole('button', { name: /open react query devtools/i }),
499 )
500β
501 const query1Hash = queryCache.find(['query-1'])?.queryHash ?? 'invalid hash'
502 const query2Hash = queryCache.find(['query-2'])?.queryHash ?? 'invalid hash'
503 const query3Hash = queryCache.find(['query-3'])?.queryHash ?? 'invalid hash'
504β
505 const sortSelect = screen.getByLabelText(/sort queries/i)
506 let queries = []
507β
508 // When sorted by query hash the queries get sorted according
509 // to just the number, with the order being -> query-1, query-2, query-3
510 fireEvent.change(sortSelect, { target: { value: 'Query Hash' } })
511β
512 /** To check the order of the queries we can use regex to find
513 * all the row items in an array and then compare the items
514 * one by one in the order we expect it
515 * @reference https://github.com/testing-library/react-testing-library/issues/313#issuecomment-625294327
516 */
517 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
518 expect(queries[0]?.textContent).toEqual(query1Hash)
519 expect(queries[1]?.textContent).toEqual(query2Hash)
520 expect(queries[2]?.textContent).toEqual(query3Hash)
521β
522 // Wait for the queries to be resolved
523 await screen.findByText(/query-1-result query-2-result query-3-result/i)
524β
525 // When sorted by the last updated date the queries are sorted by the time
526 // they were updated and since the query-2 takes longest time to complete
527 // and query-1 the shortest, so the order is -> query-2, query-3, query-1
528 fireEvent.change(sortSelect, { target: { value: 'Last Updated' } })
529β
530 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
531 expect(queries[0]?.textContent).toEqual(query2Hash)
532 expect(queries[1]?.textContent).toEqual(query3Hash)
533 expect(queries[2]?.textContent).toEqual(query1Hash)
534β
535 // When sorted by the status and then last updated date the queries
536 // query-3 takes precedence because its stale time being infinity, it
537 // always remains fresh, the rest of the queries are sorted by their last
538 // updated time, so the resulting order is -> query-3, query-2, query-1
539 fireEvent.change(sortSelect, {
540 target: { value: 'Status > Last Updated' },
541 })
542β
543 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
544 expect(queries[0]?.textContent).toEqual(query3Hash)
545 expect(queries[1]?.textContent).toEqual(query2Hash)
546 expect(queries[2]?.textContent).toEqual(query1Hash)
547β
548 // Switch the order form ascending to descending and expect the
549 // query order to be reversed from previous state
550 fireEvent.click(screen.getByRole('button', { name: /β¬ asc/i }))
551β
552 queries = await screen.findAllByText(/\["query-[1-3]"\]/)
553 expect(queries[0]?.textContent).toEqual(query1Hash)
554 expect(queries[1]?.textContent).toEqual(query2Hash)
555 expect(queries[2]?.textContent).toEqual(query3Hash)
556 })
557β
558 it('should initialize filtering and sorting values with defaults when they are not stored in localstorage', () => {
559 localStorage.removeItem('reactQueryDevtoolsBaseSort')
560 localStorage.removeItem('reactQueryDevtoolsSortFn')
561 localStorage.removeItem('reactQueryDevtoolsFilter')
562β
563 const { queryClient } = createQueryClient()
564β
565 function Page() {
566 const { data = 'default' } = useQuery(['check'], async () => {
567 await sleep(10)
568 return 'test'
569 })
570β
571 return (
572 <div>
573 <h1>{data}</h1>
574 </div>
575 )
576 }
577β
578 renderWithClient(queryClient, <Page />, {
579 initialIsOpen: true,
580 })
581β
582 const filterInput: HTMLInputElement =
583 screen.getByLabelText(/Filter by queryhash/i)
584 expect(filterInput.value).toEqual('')
585β
586 const sortCombobox: HTMLSelectElement =
587 screen.getByLabelText(/Sort queries/i)
588 expect(sortCombobox.value).toEqual(Object.keys(sortFns)[0])
589β
590 expect(screen.getByRole('button', { name: /Asc/i })).toBeInTheDocument()
591β
592 const detailsPanel = screen.queryByText(/Query Details/i)
593 expect(detailsPanel).not.toBeInTheDocument()
594 })
595β
596 it('should initialize sorting values with ones stored in localstorage', async () => {
597 localStorage.setItem('reactQueryDevtoolsBaseSort', 'true')
598 localStorage.setItem(
599 'reactQueryDevtoolsSortFn',
600 JSON.stringify(Object.keys(sortFns)[1]),
601 )
602β
603 const { queryClient } = createQueryClient()
604β
605 function Page() {
606 const { data = 'default' } = useQuery(['check'], async () => {
607 await sleep(10)
608 return 'test'
609 })
610β
611 return (
612 <div>
613 <h1>{data}</h1>
614 </div>
615 )
616 }
617β
618 renderWithClient(queryClient, <Page />, {
619 initialIsOpen: true,
620 })
621β
622 const sortCombobox: HTMLSelectElement =
623 screen.getByLabelText(/Sort queries/i)
624 expect(sortCombobox.value).toEqual(Object.keys(sortFns)[1])
625β
626 expect(screen.getByRole('button', { name: /Desc/i })).toBeInTheDocument()
627 })
628β
629 it('should initialize filter value with one stored in localstorage', () => {
630 localStorage.setItem('reactQueryDevtoolsFilter', JSON.stringify('posts'))
631β
632 const { queryClient } = createQueryClient()
633β
634 function Page() {
635 const { data = 'default' } = useQuery(['check'], async () => {
636 await sleep(10)
637 return 'test'
638 })
639β
640 return (
641 <div>
642 <h1>{data}</h1>
643 </div>
644 )
645 }
646β
647 renderWithClient(queryClient, <Page />, {
648 initialIsOpen: true,
649 })
650β
651 const filterInput: HTMLInputElement =
652 screen.getByLabelText(/Filter by queryhash/i)
653 expect(filterInput.value).toEqual('posts')
654 })
655β
656 it('style should have a nonce', async () => {
657 const { queryClient } = createQueryClient()
658β
659 function Page() {
660 return <div></div>
661 }
662β
663 const { container } = renderWithClient(queryClient, <Page />, {
664 styleNonce: 'test-nonce',
665 initialIsOpen: false,
666 })
667 const styleTag = container.querySelector('style')
668 expect(styleTag).toHaveAttribute('nonce', 'test-nonce')
669β
670 await screen.findByRole('button', { name: /react query devtools/i })
671 })
672β
673 describe('with custom context', () => {
674 it('should render without error when the custom context aligns', async () => {
675 const context = React.createContext<QueryClient | undefined>(undefined)
676 const { queryClient } = createQueryClient()
677β
678 function Page() {
679 const { data = 'default' } = useQuery(['check'], async () => 'test', {
680 context,
681 })
682β
683 return (
684 <div>
685 <h1>{data}</h1>
686 </div>
687 )
688 }
689β
690 renderWithClient(queryClient, <Page />, {
691 initialIsOpen: false,
692 context,
693 })
694β
695 await screen.findByRole('button', { name: /open react query devtools/i })
696 })
697β
698 it('should render with error when the custom context is not passed to useQuery', async () => {
699 const consoleErrorMock = jest.spyOn(console, 'error')
700 consoleErrorMock.mockImplementation(() => undefined)
701β
702 const context = React.createContext<QueryClient | undefined>(undefined)
703 const { queryClient } = createQueryClient()
704β
705 function Page() {
706 const { data = 'default' } = useQuery(['check'], async () => 'test', {
707 useErrorBoundary: true,
708 })
709β
710 return (
711 <div>
712 <h1>{data}</h1>
713 </div>
714 )
715 }
716β
717 const rendered = renderWithClient(
718 queryClient,
719 <ErrorBoundary fallbackRender={() => <div>error boundary</div>}>
720 <Page />
721 </ErrorBoundary>,
722 {
723 initialIsOpen: false,
724 context,
725 },
726 )
727β
728 await waitFor(() => rendered.getByText('error boundary'))
729β
730 consoleErrorMock.mockRestore()
731 })
732β
733 it('should render with error when the custom context is not passed to ReactQueryDevtools', async () => {
734 const consoleErrorMock = jest.spyOn(console, 'error')
735 consoleErrorMock.mockImplementation(() => undefined)
736β
737 const context = React.createContext<QueryClient | undefined>(undefined)
738 const { queryClient } = createQueryClient()
739β
740 function Page() {
741 const { data = 'default' } = useQuery(['check'], async () => 'test', {
742 useErrorBoundary: true,
743 context,
744 })
745β
746 return (
747 <div>
748 <h1>{data}</h1>
749 </div>
750 )
751 }
752β
753 const rendered = renderWithClient(
754 queryClient,
755 <ErrorBoundary fallbackRender={() => <div>error boundary</div>}>
756 <Page />
757 </ErrorBoundary>,
758 {
759 initialIsOpen: false,
760 },
761 )
762β
763 await waitFor(() => rendered.getByText('error boundary'))
764β
765 consoleErrorMock.mockRestore()
766 })
767 })
768β
769 it('should render a menu to select panel position', async () => {
770 const { queryClient } = createQueryClient()
771β
772 function Page() {
773 const { data = 'default' } = useQuery(['check'], async () => 'test')
774β
775 return (
776 <div>
777 <h1>{data}</h1>
778 </div>
779 )
780 }
781β
782 renderWithClient(queryClient, <Page />, {
783 initialIsOpen: true,
784 })
785β
786 const positionSelect = (await screen.findByLabelText(
787 'Panel position',
788 )) as HTMLSelectElement
789β
790 expect(positionSelect.value).toBe('bottom')
791 })
792β
793 it(`should render the panel to the left if panelPosition is set to 'left'`, async () => {
794 const { queryClient } = createQueryClient()
795β
796 function Page() {
797 const { data = 'default' } = useQuery(['check'], async () => 'test')
798β
799 return (
800 <div>
801 <h1>{data}</h1>
802 </div>
803 )
804 }
805β
806 renderWithClient(queryClient, <Page />, {
807 initialIsOpen: true,
808 panelPosition: 'left',
809 })
810β
811 const positionSelect = (await screen.findByLabelText(
812 'Panel position',
813 )) as HTMLSelectElement
814β
815 expect(positionSelect.value).toBe('left')
816β
817 const panel = (await screen.getByLabelText(
818 'React Query Devtools Panel',
819 )) as HTMLDivElement
820β
821 expect(panel.style.left).toBe('0px')
822 expect(panel.style.width).toBe('500px')
823 expect(panel.style.height).toBe('100vh')
824 })
825β
826 it('should change the panel position if user select different option from the menu', async () => {
827 const { queryClient } = createQueryClient()
828β
829 function Page() {
830 const { data = 'default' } = useQuery(['check'], async () => 'test')
831β
832 return (
833 <div>
834 <h1>{data}</h1>
835 </div>
836 )
837 }
838β
839 renderWithClient(queryClient, <Page />, {
840 initialIsOpen: true,
841 })
842β
843 const positionSelect = (await screen.findByLabelText(
844 'Panel position',
845 )) as HTMLSelectElement
846β
847 expect(positionSelect.value).toBe('bottom')
848β
849 const panel = (await screen.getByLabelText(
850 'React Query Devtools Panel',
851 )) as HTMLDivElement
852β
853 expect(panel.style.bottom).toBe('0px')
854 expect(panel.style.height).toBe('500px')
855 expect(panel.style.width).toBe('100%')
856β
857 await act(async () => {
858 fireEvent.change(positionSelect, { target: { value: 'right' } })
859 })
860β
861 expect(positionSelect.value).toBe('right')
862β
863 expect(panel.style.right).toBe('0px')
864 expect(panel.style.width).toBe('500px')
865 expect(panel.style.height).toBe('100vh')
866 })
867β
868 it('should restore parent element padding after closing', async () => {
869 const { queryClient } = createQueryClient()
870β
871 function Page() {
872 const { data = 'default' } = useQuery(['check'], async () => 'test')
873β
874 return (
875 <div>
876 <h1>{data}</h1>
877 </div>
878 )
879 }
880β
881 const parentElementTestid = 'parentElement'
882 const parentPaddings = {
883 paddingTop: '428px',
884 paddingBottom: '39px',
885 paddingLeft: '-373px',
886 paddingRight: '20%',
887 }
888β
889 function Parent({ children }: { children: React.ReactElement }) {
890 return (
891 <div data-testid={parentElementTestid} style={parentPaddings}>
892 {children}
893 </div>
894 )
895 }
896β
897 renderWithClient(
898 queryClient,
899 <Page />,
900 {
901 initialIsOpen: true,
902 panelPosition: 'bottom',
903 },
904 { wrapper: Parent },
905 )
906β
907 const parentElement = screen.getByTestId(parentElementTestid)
908 expect(parentElement).toHaveStyle({
909 paddingTop: '0px',
910 paddingLeft: '0px',
911 paddingRight: '0px',
912 paddingBottom: defaultPanelSize,
913 })
914β
915 fireEvent.click(screen.getByRole('button', { name: /^close$/i }))
916 expect(parentElement).toHaveStyle(parentPaddings)
917 })
918})