Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
4import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
5import SolidQueryDevtoolsPanel from '../devtoolsPanel'
6β
7describe('SolidQueryDevtoolsPanel', () => {
8 afterEach(() => {
9 vi.restoreAllMocks()
10 })
11β
12 it('should throw an error if no query client has been set', () => {
13 expect(() => render(() => <SolidQueryDevtoolsPanel />)).toThrow(
14 'No QueryClient set, use QueryClientProvider to set one',
15 )
16 })
17β
18 it('should not throw an error if query client is provided via context', () => {
19 const queryClient = new QueryClient()
20β
21 expect(() =>
22 render(() => (
23 <QueryClientProvider client={queryClient}>
24 <SolidQueryDevtoolsPanel />
25 </QueryClientProvider>
26 )),
27 ).not.toThrow()
28 })
29β
30 it('should not throw an error if query client is provided via props', () => {
31 const queryClient = new QueryClient()
32β
33 expect(() =>
34 render(() => <SolidQueryDevtoolsPanel client={queryClient} />),
35 ).not.toThrow()
36 })
37β
38 it('should forward "onClose" to the devtools instance', () => {
39 const setOnClose = vi.spyOn(
40 TanstackQueryDevtoolsPanel.prototype,
41 'setOnClose',
42 )
43 const queryClient = new QueryClient()
44 const onClose = vi.fn()
45β
46 render(() => (
47 <SolidQueryDevtoolsPanel client={queryClient} onClose={onClose} />
48 ))
49β
50 expect(setOnClose).toHaveBeenCalledWith(onClose)
51 })
52β
53 it('should default "onClose" to a no-op function when the prop is omitted', () => {
54 const setOnClose = vi.spyOn(
55 TanstackQueryDevtoolsPanel.prototype,
56 'setOnClose',
57 )
58 const queryClient = new QueryClient()
59β
60 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
61β
62 expect(setOnClose).toHaveBeenCalledWith(expect.any(Function))
63 })
64β
65 it('should forward "errorTypes" to the devtools instance', () => {
66 const setErrorTypes = vi.spyOn(
67 TanstackQueryDevtoolsPanel.prototype,
68 'setErrorTypes',
69 )
70 const queryClient = new QueryClient()
71 const errorTypes = [
72 { name: 'Network', initializer: () => new Error('Network') },
73 ]
74β
75 render(() => (
76 <SolidQueryDevtoolsPanel client={queryClient} errorTypes={errorTypes} />
77 ))
78β
79 expect(setErrorTypes).toHaveBeenCalledWith(errorTypes)
80 })
81β
82 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
83 const setErrorTypes = vi.spyOn(
84 TanstackQueryDevtoolsPanel.prototype,
85 'setErrorTypes',
86 )
87 const queryClient = new QueryClient()
88β
89 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
90β
91 expect(setErrorTypes).toHaveBeenCalledWith([])
92 })
93β
94 it('should forward "theme" to the devtools instance', () => {
95 const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme')
96 const queryClient = new QueryClient()
97β
98 render(() => <SolidQueryDevtoolsPanel client={queryClient} theme="dark" />)
99β
100 expect(setTheme).toHaveBeenCalledWith('dark')
101 })
102β
103 it('should default "theme" to "system" when the prop is omitted', () => {
104 const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme')
105 const queryClient = new QueryClient()
106β
107 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
108β
109 expect(setTheme).toHaveBeenCalledWith('system')
110 })
111β
112 it('should forward the resolved "QueryClient" via "setClient"', () => {
113 const setClient = vi.spyOn(
114 TanstackQueryDevtoolsPanel.prototype,
115 'setClient',
116 )
117 const queryClient = new QueryClient()
118β
119 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
120β
121 expect(setClient).toHaveBeenCalledWith(queryClient)
122 })
123β
124 it('should preserve the default container height when "style" omits "height"', () => {
125 const queryClient = new QueryClient()
126β
127 const { container } = render(() => (
128 <SolidQueryDevtoolsPanel
129 client={queryClient}
130 style={{ width: '300px' }}
131 />
132 ))
133β
134 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
135 height: '500px',
136 width: '300px',
137 })
138 })
139β
140 it('should let "style" override the default container height on the rendered element', () => {
141 const queryClient = new QueryClient()
142β
143 const { container } = render(() => (
144 <SolidQueryDevtoolsPanel
145 client={queryClient}
146 style={{ width: '300px', height: '300px' }}
147 />
148 ))
149β
150 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
151 height: '300px',
152 width: '300px',
153 })
154 })
155β
156 it('should call "unmount" on the devtools instance when the component unmounts', () => {
157 const unmount = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'unmount')
158 const queryClient = new QueryClient()
159β
160 const { unmount: unmountComponent } = render(() => (
161 <SolidQueryDevtoolsPanel client={queryClient} />
162 ))
163 unmountComponent()
164β
165 expect(unmount).toHaveBeenCalled()
166 })
167β
168 it('should return null in non-development environments', async () => {
169 vi.doMock('solid-js/web', async (importOriginal) => {
170 const actual = await importOriginal()
171 return Object.assign({}, actual, { isDev: false })
172 })
173 vi.resetModules()
174β
175 try {
176 const { SolidQueryDevtoolsPanel: ProductionDevtoolsPanel } =
177 await import('..')
178 expect(ProductionDevtoolsPanel({})).toBeNull()
179 } finally {
180 vi.doUnmock('solid-js/web')
181 vi.resetModules()
182 }
183 })
184})