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

PreactQueryDevtoolsPanel.test.tsx

210 lines6.9 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { act, render } from '@testing-library/preact'
3import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
4import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
5import type { PreactQueryDevtoolsPanel as PreactQueryDevtoolsPanelComponent } from '../PreactQueryDevtoolsPanel'
6​
7const mountMock = vi.fn<TanstackQueryDevtoolsPanel['mount']>()
8const unmountMock = vi.fn<TanstackQueryDevtoolsPanel['unmount']>()
9const setClientMock = vi.fn<TanstackQueryDevtoolsPanel['setClient']>()
10const setOnCloseMock = vi.fn<TanstackQueryDevtoolsPanel['setOnClose']>()
11const setErrorTypesMock = vi.fn<TanstackQueryDevtoolsPanel['setErrorTypes']>()
12const setThemeMock = vi.fn<TanstackQueryDevtoolsPanel['setTheme']>()
13​
14// oxlint-disable-next-line vitest/prefer-import-in-mock -- the mocked devtools classes have '#private' fields, and this environment cannot load the real Solid module
15vi.mock('@tanstack/query-devtools', () => ({
16 TanstackQueryDevtoolsPanel: vi.fn<(this: TanstackQueryDevtoolsPanel) => void>(
17 function (this: TanstackQueryDevtoolsPanel) {
18 this.mount = mountMock
19 this.unmount = unmountMock
20 this.setClient = setClientMock
21 this.setOnClose = setOnCloseMock
22 this.setErrorTypes = setErrorTypesMock
23 this.setTheme = setThemeMock
24 },
25 ),
26}))
27​
28describe('PreactQueryDevtoolsPanel', () => {
29 let PreactQueryDevtoolsPanel: typeof PreactQueryDevtoolsPanelComponent
30 let queryClient: QueryClient
31​
32 beforeEach(async () => {
33 vi.clearAllMocks()
34 ;({ PreactQueryDevtoolsPanel } =
35 await import('../PreactQueryDevtoolsPanel'))
36 queryClient = new QueryClient()
37 })
38​
39 it('should throw an error if no query client has been set', () => {
40 expect(() => render(<PreactQueryDevtoolsPanel />)).toThrow(
41 'No QueryClient set, use QueryClientProvider to set one',
42 )
43 })
44​
45 it('should not throw an error if query client is provided via context', () => {
46 expect(() =>
47 render(
48 <QueryClientProvider client={queryClient}>
49 <PreactQueryDevtoolsPanel />
50 </QueryClientProvider>,
51 ),
52 ).not.toThrow()
53 expect(mountMock).toHaveBeenCalledWith(expect.any(HTMLDivElement))
54 })
55​
56 it('should not throw an error if query client is provided via props', () => {
57 expect(() =>
58 render(<PreactQueryDevtoolsPanel client={queryClient} />),
59 ).not.toThrow()
60 expect(mountMock).toHaveBeenCalledWith(expect.any(HTMLDivElement))
61 })
62​
63 it('should forward "onClose" to the devtools instance', () => {
64 const onClose = vi.fn<() => unknown>()
65​
66 render(<PreactQueryDevtoolsPanel client={queryClient} onClose={onClose} />)
67​
68 expect(setOnCloseMock).toHaveBeenCalledWith(onClose)
69 })
70​
71 it('should default "onClose" to a no-op function when the prop is omitted', () => {
72 render(<PreactQueryDevtoolsPanel client={queryClient} />)
73​
74 const forwarded = setOnCloseMock.mock.calls[0]![0]
75 expect(forwarded).toBeInstanceOf(Function)
76 expect(forwarded()).toBeUndefined()
77 })
78​
79 it('should forward "errorTypes" to the devtools instance', () => {
80 const errorTypes = [
81 { name: 'Network', initializer: () => new Error('Network') },
82 ]
83​
84 render(
85 <PreactQueryDevtoolsPanel client={queryClient} errorTypes={errorTypes} />,
86 )
87​
88 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
89 })
90​
91 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
92 render(<PreactQueryDevtoolsPanel client={queryClient} />)
93​
94 expect(setErrorTypesMock).toHaveBeenCalledWith([])
95 })
96​
97 it('should forward "theme" to the devtools instance', () => {
98 render(<PreactQueryDevtoolsPanel client={queryClient} theme="dark" />)
99​
100 expect(setThemeMock).toHaveBeenCalledWith('dark')
101 })
102​
103 it('should forward the resolved "QueryClient" via "setClient"', () => {
104 render(<PreactQueryDevtoolsPanel client={queryClient} />)
105​
106 expect(setClientMock).toHaveBeenCalledWith(queryClient)
107 })
108​
109 it('should forward "styleNonce" to the devtools constructor', () => {
110 render(<PreactQueryDevtoolsPanel client={queryClient} styleNonce="abc" />)
111​
112 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
113 expect.objectContaining({ styleNonce: 'abc' }),
114 )
115 })
116​
117 it('should forward "shadowDOMTarget" to the devtools constructor', () => {
118 const shadowDOMTarget = document
119 .createElement('div')
120 .attachShadow({ mode: 'open' })
121​
122 render(
123 <PreactQueryDevtoolsPanel
124 client={queryClient}
125 shadowDOMTarget={shadowDOMTarget}
126 />,
127 )
128​
129 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
130 expect.objectContaining({ shadowDOMTarget }),
131 )
132 })
133​
134 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
135 render(
136 <PreactQueryDevtoolsPanel
137 client={queryClient}
138 hideDisabledQueries={true}
139 />,
140 )
141​
142 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
143 expect.objectContaining({ hideDisabledQueries: true }),
144 )
145 })
146​
147 it('should create the devtools instance only once across re-renders', () => {
148 const { rerender } = render(
149 <PreactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />,
150 )
151​
152 rerender(<PreactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />)
153​
154 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
155 })
156​
157 it('should preserve the default container height when "style" omits "height"', () => {
158 const { container } = render(
159 <PreactQueryDevtoolsPanel
160 client={queryClient}
161 style={{ width: '300px' }}
162 />,
163 )
164​
165 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
166 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
167 height: '500px',
168 width: '300px',
169 })
170 })
171​
172 it('should let "style" override the default container height on the rendered element', () => {
173 const { container } = render(
174 <PreactQueryDevtoolsPanel
175 client={queryClient}
176 style={{ width: '300px', height: '300px' }}
177 />,
178 )
179​
180 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
181 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
182 height: '300px',
183 width: '300px',
184 })
185 })
186​
187 it('should call "unmount" on the devtools instance when the component unmounts', () => {
188 const { unmount } = render(
189 <PreactQueryDevtoolsPanel client={queryClient} />,
190 )
191 act(() => {
192 unmount()
193 })
194​
195 expect(unmountMock).toHaveBeenCalledWith()
196 })
197​
198 it('should return null in non-development environments', async () => {
199 vi.stubEnv('NODE_ENV', 'production')
200 vi.resetModules()
201​
202 try {
203 const { PreactQueryDevtoolsPanel: ProductionDevtoolsPanel } =
204 await import('..')
205 expect(ProductionDevtoolsPanel({})).toBeNull()
206 } finally {
207 vi.resetModules()
208 }
209 })
210})