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