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

DevtoolsPanelComponent.test.tsx

141 lines4.4 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { QueryClient, onlineManager } from '@tanstack/query-core'
3import { render } from '@solidjs/testing-library'
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​
14// `goober` compiles every `css\`...\`` template literal at mount time
15// (template parsing + class hashing + style serialization), which
16// dominates mount cost and produces no value for label/role-based
17// assertions, so we replace it with a no-op factory.
18vi.mock(import('goober'), () => {
19 let counter = 0
20 const css = Object.assign(() => `tsqd-${++counter}`, {
21 bind: () => css,
22 })
23 return { css, glob: () => {}, setup: () => {} }
24})
25​
26describe('DevtoolsPanelComponent', () => {
27 const storage: { [key: string]: string } = {}
28 let queryClient: QueryClient
29 let previousRootFontSize: string
30​
31 beforeEach(() => {
32 previousRootFontSize = document.documentElement.style.fontSize
33 vi.stubGlobal('localStorage', {
34 getItem: (key: string) =>
35 Object.prototype.hasOwnProperty.call(storage, key)
36 ? storage[key]
37 : null,
38 setItem: (key: string, value: string) => {
39 storage[key] = value
40 },
41 removeItem: (key: string) => {
42 delete storage[key]
43 },
44 clear: () => {
45 Object.keys(storage).forEach((key) => delete storage[key])
46 },
47 })
48 vi.stubGlobal(
49 'matchMedia',
50 vi.fn().mockImplementation((query: string) => ({
51 matches: false,
52 media: query,
53 onchange: null,
54 addEventListener: vi.fn<MediaQueryList['addEventListener']>(),
55 removeEventListener: vi.fn<MediaQueryList['removeEventListener']>(),
56 addListener: vi.fn<MediaQueryList['addListener']>(),
57 removeListener: vi.fn<MediaQueryList['removeListener']>(),
58 dispatchEvent: vi.fn<MediaQueryList['dispatchEvent']>(),
59 })),
60 )
61 vi.stubGlobal(
62 'ResizeObserver',
63 class {
64 observe = vi.fn<ResizeObserver['observe']>()
65 unobserve = vi.fn<ResizeObserver['unobserve']>()
66 disconnect = vi.fn<ResizeObserver['disconnect']>()
67 },
68 )
69 queryClient = new QueryClient()
70 document.documentElement.style.fontSize = '16px'
71 })
72​
73 afterEach(() => {
74 Object.keys(storage).forEach((key) => delete storage[key])
75 queryClient.clear()
76 document.documentElement.style.fontSize = previousRootFontSize
77 })
78​
79 it('should render the panel without throwing when only required props are provided', () => {
80 expect(() =>
81 render(() => (
82 <DevtoolsPanelComponent
83 client={queryClient}
84 queryFlavor="TanStack Query"
85 version="5"
86 onlineManager={onlineManager}
87 />
88 )),
89 ).not.toThrow()
90 })
91​
92 it('should render the panel without throwing when all props are provided', () => {
93 expect(() =>
94 render(() => (
95 <DevtoolsPanelComponent
96 client={queryClient}
97 queryFlavor="TanStack Query"
98 version="5"
99 onlineManager={onlineManager}
100 errorTypes={[
101 { name: 'NetworkError', initializer: () => new Error('Network') },
102 ]}
103 hideDisabledQueries={true}
104 theme="dark"
105 onClose={() => {}}
106 />
107 )),
108 ).not.toThrow()
109 })
110​
111 it('should not render the open devtools button in panel-only mode', () => {
112 const rendered = render(() => (
113 <DevtoolsPanelComponent
114 client={queryClient}
115 queryFlavor="TanStack Query"
116 version="5"
117 onlineManager={onlineManager}
118 />
119 ))
120​
121 expect(
122 rendered.queryByLabelText('Open Tanstack query devtools'),
123 ).not.toBeInTheDocument()
124 })
125​
126 it('should call "onClose" when the close button is clicked', () => {
127 const onClose = vi.fn<() => void>()
128 const rendered = render(() => (
129 <DevtoolsPanelComponent
130 client={queryClient}
131 queryFlavor="TanStack Query"
132 version="5"
133 onlineManager={onlineManager}
134 onClose={onClose}
135 />
136 ))
137​
138 rendered.getByLabelText('Close Tanstack query devtools').click()
139 expect(onClose).toHaveBeenCalledTimes(1)
140 })
141})