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

devtools.test.tsx

285 lines8.5 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { createSignal, flush } from 'solid-js'
3import { cleanup, render } from '@solidjs/testing-library'
4import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
5import { TanstackQueryDevtools } from '@tanstack/query-devtools'
6import SolidQueryDevtools from '../devtools'
7import type {
8 DevtoolsButtonPosition,
9 DevtoolsErrorType,
10 DevtoolsPosition,
11 Theme,
12} from '@tanstack/query-devtools'
13​
14describe('SolidQueryDevtools', () => {
15 let queryClient: QueryClient
16​
17 beforeEach(() => {
18 queryClient = new QueryClient()
19 // Mounting the real devtools lazily imports the devtools UI, which is Solid
20 // 1 code that cannot resolve under this Solid 2 test setup. These tests only
21 // assert what the wrapper forwards to the instance, and the container
22 // element is rendered by the wrapper itself, so stub the mount out.
23 vi.spyOn(TanstackQueryDevtools.prototype, 'mount').mockImplementation(
24 () => {},
25 )
26 // ...and with nothing really mounted, the real unmount() would throw.
27 vi.spyOn(TanstackQueryDevtools.prototype, 'unmount').mockImplementation(
28 () => {},
29 )
30 })
31​
32 afterEach(() => {
33 // Dispose rendered roots before restoring mocks: disposal calls unmount()
34 // on the instance, which must still be the stub from beforeEach.
35 cleanup()
36 vi.restoreAllMocks()
37 })
38​
39 it('should throw an error if no query client has been set', () => {
40 expect(() => render(() => <SolidQueryDevtools />)).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 <SolidQueryDevtools />
50 </QueryClientProvider>
51 )),
52 ).not.toThrow()
53 })
54​
55 it('should not throw an error if query client is provided via props', () => {
56 expect(() =>
57 render(() => <SolidQueryDevtools client={queryClient} />),
58 ).not.toThrow()
59 })
60​
61 it('should forward "buttonPosition" to the devtools instance', () => {
62 const setButtonPosition = vi.spyOn(
63 TanstackQueryDevtools.prototype,
64 'setButtonPosition',
65 )
66 render(() => (
67 <SolidQueryDevtools client={queryClient} buttonPosition="top-left" />
68 ))
69​
70 flush()
71 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
72 })
73​
74 it('should forward "position" to the devtools instance', () => {
75 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
76 render(() => <SolidQueryDevtools client={queryClient} position="left" />)
77​
78 flush()
79 expect(setPosition).toHaveBeenCalledWith('left')
80 })
81​
82 it('should forward "initialIsOpen" to the devtools instance', () => {
83 const setInitialIsOpen = vi.spyOn(
84 TanstackQueryDevtools.prototype,
85 'setInitialIsOpen',
86 )
87 render(() => (
88 <SolidQueryDevtools client={queryClient} initialIsOpen={true} />
89 ))
90​
91 flush()
92 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
93 })
94​
95 it('should default "initialIsOpen" to "false" when the prop is omitted', () => {
96 const setInitialIsOpen = vi.spyOn(
97 TanstackQueryDevtools.prototype,
98 'setInitialIsOpen',
99 )
100 render(() => <SolidQueryDevtools client={queryClient} />)
101​
102 flush()
103 expect(setInitialIsOpen).toHaveBeenCalledWith(false)
104 })
105​
106 it('should forward "errorTypes" to the devtools instance', () => {
107 const setErrorTypes = vi.spyOn(
108 TanstackQueryDevtools.prototype,
109 'setErrorTypes',
110 )
111 const errorTypes = [
112 { name: 'Network', initializer: () => new Error('Network') },
113 ]
114​
115 render(() => (
116 <SolidQueryDevtools client={queryClient} errorTypes={errorTypes} />
117 ))
118​
119 flush()
120 expect(setErrorTypes).toHaveBeenCalledWith(errorTypes)
121 })
122​
123 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
124 const setErrorTypes = vi.spyOn(
125 TanstackQueryDevtools.prototype,
126 'setErrorTypes',
127 )
128 render(() => <SolidQueryDevtools client={queryClient} />)
129​
130 flush()
131 expect(setErrorTypes).toHaveBeenCalledWith([])
132 })
133​
134 it('should forward "theme" to the devtools instance', () => {
135 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
136 render(() => <SolidQueryDevtools client={queryClient} theme="dark" />)
137​
138 flush()
139 expect(setTheme).toHaveBeenCalledWith('dark')
140 })
141​
142 it('should default "theme" to "system" when the prop is omitted', () => {
143 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
144 render(() => <SolidQueryDevtools client={queryClient} />)
145​
146 flush()
147 expect(setTheme).toHaveBeenCalledWith('system')
148 })
149​
150 it('should forward the resolved "QueryClient" via "setClient"', () => {
151 const setClient = vi.spyOn(TanstackQueryDevtools.prototype, 'setClient')
152 render(() => <SolidQueryDevtools client={queryClient} />)
153​
154 flush()
155 expect(setClient).toHaveBeenCalledWith(queryClient)
156 })
157​
158 it('should forward a "buttonPosition" change to the devtools instance after mount', () => {
159 const setButtonPosition = vi.spyOn(
160 TanstackQueryDevtools.prototype,
161 'setButtonPosition',
162 )
163 const [buttonPosition, setButtonPositionSignal] =
164 createSignal<DevtoolsButtonPosition>('bottom-right')
165​
166 render(() => (
167 <SolidQueryDevtools
168 client={queryClient}
169 buttonPosition={buttonPosition()}
170 />
171 ))
172 flush()
173 setButtonPosition.mockClear()
174​
175 setButtonPositionSignal('top-left')
176 flush()
177​
178 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
179 })
180​
181 it('should forward a "position" change to the devtools instance after mount', () => {
182 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
183 const [position, setPositionSignal] =
184 createSignal<DevtoolsPosition>('bottom')
185​
186 render(() => (
187 <SolidQueryDevtools client={queryClient} position={position()} />
188 ))
189 flush()
190 setPosition.mockClear()
191​
192 setPositionSignal('top')
193 flush()
194​
195 expect(setPosition).toHaveBeenCalledWith('top')
196 })
197​
198 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
199 const setInitialIsOpen = vi.spyOn(
200 TanstackQueryDevtools.prototype,
201 'setInitialIsOpen',
202 )
203 const [initialIsOpen, setInitialIsOpenSignal] = createSignal(false)
204​
205 render(() => (
206 <SolidQueryDevtools
207 client={queryClient}
208 initialIsOpen={initialIsOpen()}
209 />
210 ))
211 flush()
212 setInitialIsOpen.mockClear()
213​
214 setInitialIsOpenSignal(true)
215 flush()
216​
217 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
218 })
219​
220 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
221 const setErrorTypes = vi.spyOn(
222 TanstackQueryDevtools.prototype,
223 'setErrorTypes',
224 )
225 const [errorTypes, setErrorTypesSignal] = createSignal<
226 Array<DevtoolsErrorType>
227 >([])
228​
229 render(() => (
230 <SolidQueryDevtools client={queryClient} errorTypes={errorTypes()} />
231 ))
232 flush()
233 setErrorTypes.mockClear()
234​
235 const nextErrorTypes = [
236 { name: 'Network', initializer: () => new Error('Network') },
237 ]
238 setErrorTypesSignal(nextErrorTypes)
239 flush()
240​
241 expect(setErrorTypes).toHaveBeenCalledWith(nextErrorTypes)
242 })
243​
244 it('should forward a "theme" change to the devtools instance after mount', () => {
245 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
246 const [theme, setThemeSignal] = createSignal<Theme>('light')
247​
248 render(() => <SolidQueryDevtools client={queryClient} theme={theme()} />)
249 flush()
250 setTheme.mockClear()
251​
252 setThemeSignal('dark')
253 flush()
254​
255 expect(setTheme).toHaveBeenCalledWith('dark')
256 })
257​
258 it('should call "unmount" on the devtools instance when the component unmounts', async () => {
259 const unmount = vi.spyOn(TanstackQueryDevtools.prototype, 'unmount')
260 const { unmount: unmountComponent } = render(() => (
261 <SolidQueryDevtools client={queryClient} />
262 ))
263 flush()
264 await Promise.resolve()
265 unmountComponent()
266​
267 expect(unmount).toHaveBeenCalled()
268 })
269​
270 it('should return null in non-development environments', async () => {
271 vi.doMock('solid-js/web', async (importOriginal) => {
272 const actual = await importOriginal()
273 return Object.assign({}, actual, { isDev: false })
274 })
275 vi.resetModules()
276​
277 try {
278 const { SolidQueryDevtools: ProductionDevtools } = await import('..')
279 expect(ProductionDevtools({})).toBeNull()
280 } finally {
281 vi.doUnmock('solid-js/web')
282 vi.resetModules()
283 }
284 })
285})