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

PreactQueryDevtools.test.tsx

248 lines7.7 KB

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()
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 create the devtools instance only once across re-renders', () => {
153 const { rerender } = render(
154 <PreactQueryDevtools client={queryClient} position="bottom" />,
155 )
156​
157 rerender(<PreactQueryDevtools client={queryClient} position="top" />)
158​
159 expect(TanstackQueryDevtools).toHaveBeenCalledTimes(1)
160 })
161​
162 it('should forward a "buttonPosition" change to the devtools instance after mount', () => {
163 const { rerender } = render(
164 <PreactQueryDevtools
165 client={queryClient}
166 buttonPosition="bottom-right"
167 />,
168 )
169 setButtonPositionMock.mockClear()
170​
171 rerender(
172 <PreactQueryDevtools client={queryClient} buttonPosition="top-left" />,
173 )
174​
175 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
176 })
177​
178 it('should forward a "position" change to the devtools instance after mount', () => {
179 const { rerender } = render(
180 <PreactQueryDevtools client={queryClient} position="bottom" />,
181 )
182 setPositionMock.mockClear()
183​
184 rerender(<PreactQueryDevtools client={queryClient} position="top" />)
185​
186 expect(setPositionMock).toHaveBeenCalledWith('top')
187 })
188​
189 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
190 const { rerender } = render(
191 <PreactQueryDevtools client={queryClient} initialIsOpen={false} />,
192 )
193 setInitialIsOpenMock.mockClear()
194​
195 rerender(<PreactQueryDevtools client={queryClient} initialIsOpen={true} />)
196​
197 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
198 })
199​
200 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
201 const { rerender } = render(
202 <PreactQueryDevtools client={queryClient} errorTypes={[]} />,
203 )
204 setErrorTypesMock.mockClear()
205​
206 const errorTypes = [
207 { name: 'Network', initializer: () => new Error('Network') },
208 ]
209 rerender(
210 <PreactQueryDevtools client={queryClient} errorTypes={errorTypes} />,
211 )
212​
213 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
214 })
215​
216 it('should forward a "theme" change to the devtools instance after mount', () => {
217 const { rerender } = render(
218 <PreactQueryDevtools client={queryClient} theme="light" />,
219 )
220 setThemeMock.mockClear()
221​
222 rerender(<PreactQueryDevtools client={queryClient} theme="dark" />)
223​
224 expect(setThemeMock).toHaveBeenCalledWith('dark')
225 })
226​
227 it('should call "unmount" on the devtools instance when the component unmounts', () => {
228 const { unmount } = render(<PreactQueryDevtools client={queryClient} />)
229 act(() => {
230 unmount()
231 })
232​
233 expect(unmountMock).toHaveBeenCalled()
234 })
235​
236 it('should return null in non-development environments', async () => {
237 vi.stubEnv('NODE_ENV', 'production')
238 vi.resetModules()
239​
240 try {
241 const { PreactQueryDevtools: ProductionDevtools } = await import('..')
242 expect(ProductionDevtools({})).toBeNull()
243 } finally {
244 vi.unstubAllEnvs()
245 vi.resetModules()
246 }
247 })
248})