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

devtoolsPanel.test.tsx

195 lines6.0 KB

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