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