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

devtools.test.tsx

918 lines24.7 KB

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