Open History to see the last commit
1import { QueryClient, onlineManager } from '@tanstack/query-core'
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3import { TanstackQueryDevtoolsPanel } from '..'
4β
5// `solid-transition-group` resolves to its unpublished source under Vite.
6vi.mock(import('solid-transition-group'), () => ({
7 TransitionGroup: (props) => props.children,
8}))
9β
10describe('TanstackQueryDevtoolsPanel', () => {
11 let devtools: TanstackQueryDevtoolsPanel
12β
13 beforeEach(() => {
14 devtools = new TanstackQueryDevtoolsPanel({
15 client: new QueryClient(),
16 queryFlavor: 'TanStack Query',
17 version: '5',
18 onlineManager,
19 })
20 })
21β
22 describe('mount', () => {
23 it('should mount devtools to the provided element', () => {
24 const el = document.createElement('div')
25β
26 expect(() => devtools.mount(el)).not.toThrow()
27β
28 devtools.unmount()
29 })
30β
31 it('should throw if mount is called twice without unmount', () => {
32 const el = document.createElement('div')
33 devtools.mount(el)
34β
35 expect(() => devtools.mount(el)).toThrow('Devtools is already mounted')
36β
37 devtools.unmount()
38 })
39 })
40β
41 describe('unmount', () => {
42 it('should unmount devtools and allow remounting', () => {
43 const el = document.createElement('div')
44 devtools.mount(el)
45β
46 expect(() => devtools.unmount()).not.toThrow()
47 expect(() => devtools.mount(el)).not.toThrow()
48β
49 devtools.unmount()
50 })
51β
52 it('should throw if unmount is called before mount', () => {
53 expect(() => devtools.unmount()).toThrow('Devtools is not mounted')
54 })
55β
56 it('should throw if unmount is called twice', () => {
57 const el = document.createElement('div')
58 devtools.mount(el)
59 devtools.unmount()
60β
61 expect(() => devtools.unmount()).toThrow('Devtools is not mounted')
62 })
63 })
64β
65 describe('setters', () => {
66 describe('before mount', () => {
67 it('should not throw when "setButtonPosition" is called', () => {
68 expect(() => devtools.setButtonPosition('top-left')).not.toThrow()
69 })
70β
71 it('should not throw when "setPosition" is called', () => {
72 expect(() => devtools.setPosition('left')).not.toThrow()
73 })
74β
75 it('should not throw when "setInitialIsOpen" is called', () => {
76 expect(() => devtools.setInitialIsOpen(true)).not.toThrow()
77 })
78β
79 it('should not throw when "setErrorTypes" is called', () => {
80 expect(() =>
81 devtools.setErrorTypes([
82 { name: 'NetworkError', initializer: () => new Error('Network') },
83 ]),
84 ).not.toThrow()
85 })
86β
87 it('should not throw when "setClient" is called', () => {
88 expect(() => devtools.setClient(new QueryClient())).not.toThrow()
89 })
90β
91 it('should not throw when "setOnClose" is called', () => {
92 expect(() => devtools.setOnClose(() => {})).not.toThrow()
93 })
94β
95 it('should not throw when "setTheme" is called', () => {
96 expect(() => devtools.setTheme('dark')).not.toThrow()
97 expect(() => devtools.setTheme('light')).not.toThrow()
98 expect(() => devtools.setTheme('system')).not.toThrow()
99 expect(() => devtools.setTheme(undefined)).not.toThrow()
100 })
101 })
102β
103 describe('after mount', () => {
104 it('should not throw when setters are called on a mounted instance', () => {
105 const el = document.createElement('div')
106 devtools.mount(el)
107β
108 try {
109 expect(() => devtools.setButtonPosition('top-left')).not.toThrow()
110 expect(() => devtools.setPosition('left')).not.toThrow()
111 expect(() => devtools.setInitialIsOpen(true)).not.toThrow()
112 expect(() =>
113 devtools.setErrorTypes([
114 { name: 'NetworkError', initializer: () => new Error('Network') },
115 ]),
116 ).not.toThrow()
117 expect(() => devtools.setClient(new QueryClient())).not.toThrow()
118 expect(() => devtools.setOnClose(() => {})).not.toThrow()
119 expect(() => devtools.setTheme('dark')).not.toThrow()
120 } finally {
121 devtools.unmount()
122 }
123 })
124 })
125 })
126β
127 describe('styles', () => {
128 it('should add the styles to the shadow root the panel renders in', async () => {
129 vi.stubGlobal(
130 'matchMedia',
131 vi.fn().mockImplementation((query: string) => ({
132 matches: false,
133 media: query,
134 addEventListener: vi.fn<MediaQueryList['addEventListener']>(),
135 removeEventListener: vi.fn<MediaQueryList['removeEventListener']>(),
136 })),
137 )
138 vi.stubGlobal(
139 'ResizeObserver',
140 class {
141 observe() {}
142 unobserve() {}
143 disconnect() {}
144 },
145 )
146 const host = document.createElement('div')
147 document.body.appendChild(host)
148 const shadowRoot = host.attachShadow({ mode: 'open' })
149 const el = document.createElement('div')
150 shadowRoot.appendChild(el)
151 devtools.mount(el)
152β
153 try {
154 await vi.waitFor(
155 () =>
156 expect(shadowRoot.querySelector('.tsqd-main-panel')).not.toBeNull(),
157 5000,
158 )
159 expect(shadowRoot.adoptedStyleSheets).toHaveLength(1)
160 } finally {
161 devtools.unmount()
162 host.remove()
163 }
164 })
165 })
166β
167 describe('trustedTypesPolicy', () => {
168 it('should create the devtools markup with the policy', async () => {
169 vi.stubGlobal(
170 'matchMedia',
171 vi.fn().mockImplementation((query: string) => ({
172 matches: false,
173 media: query,
174 addEventListener: vi.fn<MediaQueryList['addEventListener']>(),
175 removeEventListener: vi.fn<MediaQueryList['removeEventListener']>(),
176 })),
177 )
178 vi.stubGlobal(
179 'ResizeObserver',
180 class {
181 observe() {}
182 unobserve() {}
183 disconnect() {}
184 },
185 )
186 // Solid creates each template once, so use a fresh copy of the devtools.
187 vi.resetModules()
188 const { TanstackQueryDevtoolsPanel: FreshTanstackQueryDevtoolsPanel } =
189 await import('..')
190 const createHTML = vi.fn<(html: string) => string>((html) => html)
191 const policyDevtools = new FreshTanstackQueryDevtoolsPanel({
192 client: new QueryClient(),
193 queryFlavor: 'TanStack Query',
194 version: '5',
195 onlineManager,
196 trustedTypesPolicy: { createHTML },
197 })
198 const el = document.createElement('div')
199 document.body.appendChild(el)
200 policyDevtools.mount(el)
201β
202 try {
203 await vi.waitFor(
204 () => expect(el.querySelector('.tsqd-main-panel')).not.toBeNull(),
205 5000,
206 )
207 expect(createHTML).toHaveBeenCalledWith(expect.stringContaining('<'))
208 } finally {
209 policyDevtools.unmount()
210 el.remove()
211 }
212 })
213 })
214})