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

PreactQueryDevtoolsPanel.test.tsx

242 lines7.4 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@testing-library/preact'
3import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
4import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
5​
6const mountMock = vi.fn()
7const unmountMock = vi.fn()
8const setClientMock = vi.fn()
9const setOnCloseMock = vi.fn()
10const setErrorTypesMock = vi.fn()
11const setThemeMock = vi.fn()
12​
13vi.mock('@tanstack/query-devtools', () => ({
14 TanstackQueryDevtoolsPanel: vi.fn(function (
15 this: TanstackQueryDevtoolsPanel,
16 ) {
17 this.mount = mountMock
18 this.unmount = unmountMock
19 this.setClient = setClientMock
20 this.setOnClose = setOnCloseMock
21 this.setErrorTypes = setErrorTypesMock
22 this.setTheme = setThemeMock
23 }),
24}))
25​
26describe('PreactQueryDevtoolsPanel', () => {
27 beforeEach(() => {
28 vi.clearAllMocks()
29 })
30​
31 it('should throw an error if no query client has been set', async () => {
32 const { PreactQueryDevtoolsPanel } =
33 await import('../PreactQueryDevtoolsPanel')
34​
35 expect(() => render(<PreactQueryDevtoolsPanel />)).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', async () => {
41 const { PreactQueryDevtoolsPanel } =
42 await import('../PreactQueryDevtoolsPanel')
43 const queryClient = new QueryClient()
44​
45 expect(() =>
46 render(
47 <QueryClientProvider client={queryClient}>
48 <PreactQueryDevtoolsPanel />
49 </QueryClientProvider>,
50 ),
51 ).not.toThrow()
52 expect(mountMock).toHaveBeenCalled()
53 })
54​
55 it('should not throw an error if query client is provided via props', async () => {
56 const { PreactQueryDevtoolsPanel } =
57 await import('../PreactQueryDevtoolsPanel')
58 const queryClient = new QueryClient()
59​
60 expect(() =>
61 render(<PreactQueryDevtoolsPanel client={queryClient} />),
62 ).not.toThrow()
63 expect(mountMock).toHaveBeenCalled()
64 })
65​
66 it('should forward "onClose" to the devtools instance', async () => {
67 const { PreactQueryDevtoolsPanel } =
68 await import('../PreactQueryDevtoolsPanel')
69 const queryClient = new QueryClient()
70 const onClose = vi.fn()
71​
72 render(<PreactQueryDevtoolsPanel client={queryClient} onClose={onClose} />)
73​
74 expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function))
75 })
76​
77 it('should default "onClose" to a no-op function when the prop is omitted', async () => {
78 const { PreactQueryDevtoolsPanel } =
79 await import('../PreactQueryDevtoolsPanel')
80 const queryClient = new QueryClient()
81​
82 render(<PreactQueryDevtoolsPanel client={queryClient} />)
83​
84 expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function))
85 })
86​
87 it('should forward "errorTypes" to the devtools instance', async () => {
88 const { PreactQueryDevtoolsPanel } =
89 await import('../PreactQueryDevtoolsPanel')
90 const queryClient = new QueryClient()
91 const errorTypes = [
92 { name: 'Network', initializer: () => new Error('Network') },
93 ]
94​
95 render(
96 <PreactQueryDevtoolsPanel client={queryClient} errorTypes={errorTypes} />,
97 )
98​
99 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
100 })
101​
102 it('should default "errorTypes" to an empty array when the prop is omitted', async () => {
103 const { PreactQueryDevtoolsPanel } =
104 await import('../PreactQueryDevtoolsPanel')
105 const queryClient = new QueryClient()
106​
107 render(<PreactQueryDevtoolsPanel client={queryClient} />)
108​
109 expect(setErrorTypesMock).toHaveBeenCalledWith([])
110 })
111​
112 it('should forward "theme" to the devtools instance', async () => {
113 const { PreactQueryDevtoolsPanel } =
114 await import('../PreactQueryDevtoolsPanel')
115 const queryClient = new QueryClient()
116​
117 render(<PreactQueryDevtoolsPanel client={queryClient} theme="dark" />)
118​
119 expect(setThemeMock).toHaveBeenCalledWith('dark')
120 })
121​
122 it('should forward the resolved "QueryClient" via "setClient"', async () => {
123 const { PreactQueryDevtoolsPanel } =
124 await import('../PreactQueryDevtoolsPanel')
125 const queryClient = new QueryClient()
126​
127 render(<PreactQueryDevtoolsPanel client={queryClient} />)
128​
129 expect(setClientMock).toHaveBeenCalledWith(queryClient)
130 })
131​
132 it('should forward "styleNonce" to the devtools constructor', async () => {
133 const { PreactQueryDevtoolsPanel } =
134 await import('../PreactQueryDevtoolsPanel')
135 const queryClient = new QueryClient()
136​
137 render(<PreactQueryDevtoolsPanel client={queryClient} styleNonce="abc" />)
138​
139 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
140 expect.objectContaining({ styleNonce: 'abc' }),
141 )
142 })
143​
144 it('should forward "shadowDOMTarget" to the devtools constructor', async () => {
145 const { PreactQueryDevtoolsPanel } =
146 await import('../PreactQueryDevtoolsPanel')
147 const queryClient = new QueryClient()
148 const shadowDOMTarget = document
149 .createElement('div')
150 .attachShadow({ mode: 'open' })
151​
152 render(
153 <PreactQueryDevtoolsPanel
154 client={queryClient}
155 shadowDOMTarget={shadowDOMTarget}
156 />,
157 )
158​
159 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
160 expect.objectContaining({ shadowDOMTarget }),
161 )
162 })
163​
164 it('should forward "hideDisabledQueries" to the devtools constructor', async () => {
165 const { PreactQueryDevtoolsPanel } =
166 await import('../PreactQueryDevtoolsPanel')
167 const queryClient = new QueryClient()
168​
169 render(
170 <PreactQueryDevtoolsPanel
171 client={queryClient}
172 hideDisabledQueries={true}
173 />,
174 )
175​
176 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
177 expect.objectContaining({ hideDisabledQueries: true }),
178 )
179 })
180​
181 it('should preserve the default container height when "style" omits "height"', async () => {
182 const { PreactQueryDevtoolsPanel } =
183 await import('../PreactQueryDevtoolsPanel')
184 const queryClient = new QueryClient()
185​
186 const { container } = render(
187 <PreactQueryDevtoolsPanel
188 client={queryClient}
189 style={{ width: '300px' }}
190 />,
191 )
192​
193 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
194 height: '500px',
195 width: '300px',
196 })
197 })
198​
199 it('should let "style" override the default container height on the rendered element', async () => {
200 const { PreactQueryDevtoolsPanel } =
201 await import('../PreactQueryDevtoolsPanel')
202 const queryClient = new QueryClient()
203​
204 const { container } = render(
205 <PreactQueryDevtoolsPanel
206 client={queryClient}
207 style={{ width: '300px', height: '300px' }}
208 />,
209 )
210​
211 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
212 height: '300px',
213 width: '300px',
214 })
215 })
216​
217 it('should call "unmount" on the devtools instance when the component unmounts', async () => {
218 const { PreactQueryDevtoolsPanel } =
219 await import('../PreactQueryDevtoolsPanel')
220 const queryClient = new QueryClient()
221​
222 const { unmount } = render(
223 <PreactQueryDevtoolsPanel client={queryClient} />,
224 )
225 unmount()
226​
227 expect(unmountMock).toHaveBeenCalled()
228 })
229​
230 it('should return null in non-development environments', async () => {
231 vi.stubEnv('NODE_ENV', 'production')
232 vi.resetModules()
233​
234 try {
235 const { PreactQueryDevtoolsPanel } = await import('..')
236 expect(PreactQueryDevtoolsPanel({})).toBeNull()
237 } finally {
238 vi.unstubAllEnvs()
239 vi.resetModules()
240 }
241 })
242})