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

PreactQueryDevtools.test.tsx

243 lines8.0 KB

1import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
2import { TanstackQueryDevtools } from '@tanstack/query-devtools'
3import { act, render } from '@testing-library/preact'
4import { beforeEach, describe, expect, it, vi } from 'vitest'
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 "shadowDOMTarget" to the devtools constructor', () => {
122 const shadowDOMTarget = document
123 .createElement('div')
124 .attachShadow({ mode: 'open' })
125​
126 render(
127 <PreactQueryDevtools
128 client={queryClient}
129 shadowDOMTarget={shadowDOMTarget}
130 />,
131 )
132​
133 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
134 expect.objectContaining({ shadowDOMTarget }),
135 )
136 })
137​
138 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
139 render(
140 <PreactQueryDevtools client={queryClient} hideDisabledQueries={true} />,
141 )
142​
143 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
144 expect.objectContaining({ hideDisabledQueries: true }),
145 )
146 })
147​
148 it('should create the devtools instance only once across re-renders', () => {
149 const { rerender } = render(
150 <PreactQueryDevtools client={queryClient} position="bottom" />,
151 )
152​
153 rerender(<PreactQueryDevtools client={queryClient} position="top" />)
154​
155 expect(TanstackQueryDevtools).toHaveBeenCalledTimes(1)
156 })
157​
158 it('should forward a "buttonPosition" change to the devtools instance after mount', () => {
159 const { rerender } = render(
160 <PreactQueryDevtools
161 client={queryClient}
162 buttonPosition="bottom-right"
163 />,
164 )
165 setButtonPositionMock.mockClear()
166​
167 rerender(
168 <PreactQueryDevtools client={queryClient} buttonPosition="top-left" />,
169 )
170​
171 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
172 })
173​
174 it('should forward a "position" change to the devtools instance after mount', () => {
175 const { rerender } = render(
176 <PreactQueryDevtools client={queryClient} position="bottom" />,
177 )
178 setPositionMock.mockClear()
179​
180 rerender(<PreactQueryDevtools client={queryClient} position="top" />)
181​
182 expect(setPositionMock).toHaveBeenCalledWith('top')
183 })
184​
185 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
186 const { rerender } = render(
187 <PreactQueryDevtools client={queryClient} initialIsOpen={false} />,
188 )
189 setInitialIsOpenMock.mockClear()
190​
191 rerender(<PreactQueryDevtools client={queryClient} initialIsOpen={true} />)
192​
193 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
194 })
195​
196 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
197 const { rerender } = render(
198 <PreactQueryDevtools client={queryClient} errorTypes={[]} />,
199 )
200 setErrorTypesMock.mockClear()
201​
202 const errorTypes = [
203 { name: 'Network', initializer: () => new Error('Network') },
204 ]
205 rerender(
206 <PreactQueryDevtools client={queryClient} errorTypes={errorTypes} />,
207 )
208​
209 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
210 })
211​
212 it('should forward a "theme" change to the devtools instance after mount', () => {
213 const { rerender } = render(
214 <PreactQueryDevtools client={queryClient} theme="light" />,
215 )
216 setThemeMock.mockClear()
217​
218 rerender(<PreactQueryDevtools client={queryClient} theme="dark" />)
219​
220 expect(setThemeMock).toHaveBeenCalledWith('dark')
221 })
222​
223 it('should call "unmount" on the devtools instance when the component unmounts', () => {
224 const { unmount } = render(<PreactQueryDevtools client={queryClient} />)
225 act(() => {
226 unmount()
227 })
228​
229 expect(unmountMock).toHaveBeenCalledWith()
230 })
231​
232 it('should return null in non-development environments', async () => {
233 vi.stubEnv('NODE_ENV', 'production')
234 vi.resetModules()
235​
236 try {
237 const { PreactQueryDevtools: ProductionDevtools } = await import('..')
238 expect(ProductionDevtools({})).toBeNull()
239 } finally {
240 vi.resetModules()
241 }
242 })
243})