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