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

PreactQueryDevtoolsPanel.test.tsx

204 lines6.7 KB

1import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
2import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
3import { act, render } from '@testing-library/preact'
4import { beforeEach, describe, expect, it, vi } from 'vitest'
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 "shadowDOMTarget" to the devtools constructor', () => {
110 const shadowDOMTarget = document
111 .createElement('div')
112 .attachShadow({ mode: 'open' })
113​
114 render(
115 <PreactQueryDevtoolsPanel
116 client={queryClient}
117 shadowDOMTarget={shadowDOMTarget}
118 />,
119 )
120​
121 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
122 expect.objectContaining({ shadowDOMTarget }),
123 )
124 })
125​
126 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
127 render(
128 <PreactQueryDevtoolsPanel
129 client={queryClient}
130 hideDisabledQueries={true}
131 />,
132 )
133​
134 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
135 expect.objectContaining({ hideDisabledQueries: true }),
136 )
137 })
138​
139 it('should create the devtools instance only once across re-renders', () => {
140 const { rerender } = render(
141 <PreactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />,
142 )
143​
144 rerender(<PreactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />)
145​
146 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
147 })
148​
149 it('should preserve the default container height when "style" omits "height"', () => {
150 const { container } = render(
151 <PreactQueryDevtoolsPanel
152 client={queryClient}
153 style={{ width: '300px' }}
154 />,
155 )
156​
157 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
158 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
159 height: '100%',
160 display: 'flex',
161 'flex-direction': 'column',
162 width: '300px',
163 })
164 })
165​
166 it('should let "style" override the default container height on the rendered element', () => {
167 const { container } = render(
168 <PreactQueryDevtoolsPanel
169 client={queryClient}
170 style={{ width: '300px', height: '300px' }}
171 />,
172 )
173​
174 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
175 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
176 height: '300px',
177 width: '300px',
178 })
179 })
180​
181 it('should call "unmount" on the devtools instance when the component unmounts', () => {
182 const { unmount } = render(
183 <PreactQueryDevtoolsPanel client={queryClient} />,
184 )
185 act(() => {
186 unmount()
187 })
188​
189 expect(unmountMock).toHaveBeenCalledWith()
190 })
191​
192 it('should return null in non-development environments', async () => {
193 vi.stubEnv('NODE_ENV', 'production')
194 vi.resetModules()
195​
196 try {
197 const { PreactQueryDevtoolsPanel: ProductionDevtoolsPanel } =
198 await import('..')
199 expect(ProductionDevtoolsPanel({})).toBeNull()
200 } finally {
201 vi.resetModules()
202 }
203 })
204})