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

DevtoolsPanelComponent.test.tsx

129 lines4.0 KB

1import { render } from '@solidjs/testing-library'
2import { QueryClient, onlineManager } from '@tanstack/query-core'
3import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4import DevtoolsPanelComponent from '../DevtoolsPanelComponent'
5​
6// `solid-transition-group` internally imports from
7// `@solid-primitives/transition-group`, whose `exports` field points at
8// `src/index.ts` (not published) under a `@solid-primitives/source` condition
9// that Vite can't fall through, so we stub it with a transparent pass-through.
10vi.mock(import('solid-transition-group'), () => ({
11 TransitionGroup: (props) => props.children,
12}))
13​
14describe('DevtoolsPanelComponent', () => {
15 const storage: { [key: string]: string } = {}
16 let queryClient: QueryClient
17 let previousRootFontSize: string
18​
19 beforeEach(() => {
20 previousRootFontSize = document.documentElement.style.fontSize
21 vi.stubGlobal('localStorage', {
22 getItem: (key: string) =>
23 Object.prototype.hasOwnProperty.call(storage, key)
24 ? storage[key]
25 : null,
26 setItem: (key: string, value: string) => {
27 storage[key] = value
28 },
29 removeItem: (key: string) => {
30 delete storage[key]
31 },
32 clear: () => {
33 Object.keys(storage).forEach((key) => delete storage[key])
34 },
35 })
36 vi.stubGlobal(
37 'matchMedia',
38 vi.fn().mockImplementation((query: string) => ({
39 matches: false,
40 media: query,
41 onchange: null,
42 addEventListener: vi.fn<MediaQueryList['addEventListener']>(),
43 removeEventListener: vi.fn<MediaQueryList['removeEventListener']>(),
44 addListener: vi.fn<MediaQueryList['addListener']>(),
45 removeListener: vi.fn<MediaQueryList['removeListener']>(),
46 dispatchEvent: vi.fn<MediaQueryList['dispatchEvent']>(),
47 })),
48 )
49 vi.stubGlobal(
50 'ResizeObserver',
51 class {
52 observe = vi.fn<ResizeObserver['observe']>()
53 unobserve = vi.fn<ResizeObserver['unobserve']>()
54 disconnect = vi.fn<ResizeObserver['disconnect']>()
55 },
56 )
57 queryClient = new QueryClient()
58 document.documentElement.style.fontSize = '16px'
59 })
60​
61 afterEach(() => {
62 Object.keys(storage).forEach((key) => delete storage[key])
63 queryClient.clear()
64 document.documentElement.style.fontSize = previousRootFontSize
65 })
66​
67 it('should render the panel without throwing when only required props are provided', () => {
68 expect(() =>
69 render(() => (
70 <DevtoolsPanelComponent
71 client={queryClient}
72 queryFlavor="TanStack Query"
73 version="5"
74 onlineManager={onlineManager}
75 />
76 )),
77 ).not.toThrow()
78 })
79​
80 it('should render the panel without throwing when all props are provided', () => {
81 expect(() =>
82 render(() => (
83 <DevtoolsPanelComponent
84 client={queryClient}
85 queryFlavor="TanStack Query"
86 version="5"
87 onlineManager={onlineManager}
88 errorTypes={[
89 { name: 'NetworkError', initializer: () => new Error('Network') },
90 ]}
91 hideDisabledQueries={true}
92 theme="dark"
93 onClose={() => {}}
94 />
95 )),
96 ).not.toThrow()
97 })
98​
99 it('should not render the open devtools button in panel-only mode', () => {
100 const rendered = render(() => (
101 <DevtoolsPanelComponent
102 client={queryClient}
103 queryFlavor="TanStack Query"
104 version="5"
105 onlineManager={onlineManager}
106 />
107 ))
108​
109 expect(
110 rendered.queryByLabelText('Open Tanstack query devtools'),
111 ).not.toBeInTheDocument()
112 })
113​
114 it('should call "onClose" when the close button is clicked', () => {
115 const onClose = vi.fn<() => void>()
116 const rendered = render(() => (
117 <DevtoolsPanelComponent
118 client={queryClient}
119 queryFlavor="TanStack Query"
120 version="5"
121 onlineManager={onlineManager}
122 onClose={onClose}
123 />
124 ))
125​
126 rendered.getByLabelText('Close Tanstack query devtools').click()
127 expect(onClose).toHaveBeenCalledTimes(1)
128 })
129})