Open History to see the last commit
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('solid-transition-group', () => ({
11 TransitionGroup: (props: { children: unknown }) => 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('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 = ''
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(),
55 removeEventListener: vi.fn(),
56 addListener: vi.fn(),
57 removeListener: vi.fn(),
58 dispatchEvent: vi.fn(),
59 })),
60 )
61 vi.stubGlobal(
62 'ResizeObserver',
63 class {
64 observe = vi.fn()
65 unobserve = vi.fn()
66 disconnect = vi.fn()
67 },
68 )
69 queryClient = new QueryClient()
70 document.documentElement.style.fontSize = '16px'
71 })
72β
73 afterEach(() => {
74 vi.unstubAllGlobals()
75 Object.keys(storage).forEach((key) => delete storage[key])
76 queryClient.clear()
77 document.documentElement.style.fontSize = previousRootFontSize
78 })
79β
80 it('should render the panel without throwing when only required props are provided', () => {
81 expect(() =>
82 render(() => (
83 <DevtoolsPanelComponent
84 client={queryClient}
85 queryFlavor="TanStack Query"
86 version="5"
87 onlineManager={onlineManager}
88 />
89 )),
90 ).not.toThrow()
91 })
92β
93 it('should render the panel without throwing when all props are provided', () => {
94 expect(() =>
95 render(() => (
96 <DevtoolsPanelComponent
97 client={queryClient}
98 queryFlavor="TanStack Query"
99 version="5"
100 onlineManager={onlineManager}
101 errorTypes={[
102 { name: 'NetworkError', initializer: () => new Error('Network') },
103 ]}
104 hideDisabledQueries={true}
105 theme="dark"
106 onClose={() => {}}
107 />
108 )),
109 ).not.toThrow()
110 })
111β
112 it('should not render the open devtools button in panel-only mode', () => {
113 const rendered = render(() => (
114 <DevtoolsPanelComponent
115 client={queryClient}
116 queryFlavor="TanStack Query"
117 version="5"
118 onlineManager={onlineManager}
119 />
120 ))
121β
122 expect(
123 rendered.queryByLabelText('Open Tanstack query devtools'),
124 ).not.toBeInTheDocument()
125 })
126β
127 it('should call "onClose" when the close button is clicked', () => {
128 const onClose = vi.fn()
129 const rendered = render(() => (
130 <DevtoolsPanelComponent
131 client={queryClient}
132 queryFlavor="TanStack Query"
133 version="5"
134 onlineManager={onlineManager}
135 onClose={onClose}
136 />
137 ))
138β
139 rendered.getByLabelText('Close Tanstack query devtools').click()
140β
141 expect(onClose).toHaveBeenCalledTimes(1)
142 })
143})