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