Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { createSignal } from 'solid-js'
3import { 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 })
20β
21 afterEach(() => {
22 vi.restoreAllMocks()
23 })
24β
25 it('should throw an error if no query client has been set', () => {
26 expect(() => render(() => <SolidQueryDevtools />)).toThrow(
27 'No QueryClient set, use QueryClientProvider to set one',
28 )
29 })
30β
31 it('should not throw an error if query client is provided via context', () => {
32 expect(() =>
33 render(() => (
34 <QueryClientProvider client={queryClient}>
35 <SolidQueryDevtools />
36 </QueryClientProvider>
37 )),
38 ).not.toThrow()
39 })
40β
41 it('should not throw an error if query client is provided via props', () => {
42 expect(() =>
43 render(() => <SolidQueryDevtools client={queryClient} />),
44 ).not.toThrow()
45 })
46β
47 it('should forward "buttonPosition" to the devtools instance', () => {
48 const setButtonPosition = vi.spyOn(
49 TanstackQueryDevtools.prototype,
50 'setButtonPosition',
51 )
52 render(() => (
53 <SolidQueryDevtools client={queryClient} buttonPosition="top-left" />
54 ))
55β
56 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
57 })
58β
59 it('should forward "position" to the devtools instance', () => {
60 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
61 render(() => <SolidQueryDevtools client={queryClient} position="left" />)
62β
63 expect(setPosition).toHaveBeenCalledWith('left')
64 })
65β
66 it('should forward "initialIsOpen" to the devtools instance', () => {
67 const setInitialIsOpen = vi.spyOn(
68 TanstackQueryDevtools.prototype,
69 'setInitialIsOpen',
70 )
71 render(() => (
72 <SolidQueryDevtools client={queryClient} initialIsOpen={true} />
73 ))
74β
75 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
76 })
77β
78 it('should default "initialIsOpen" to "false" when the prop is omitted', () => {
79 const setInitialIsOpen = vi.spyOn(
80 TanstackQueryDevtools.prototype,
81 'setInitialIsOpen',
82 )
83 render(() => <SolidQueryDevtools client={queryClient} />)
84β
85 expect(setInitialIsOpen).toHaveBeenCalledWith(false)
86 })
87β
88 it('should forward "errorTypes" to the devtools instance', () => {
89 const setErrorTypes = vi.spyOn(
90 TanstackQueryDevtools.prototype,
91 'setErrorTypes',
92 )
93 const errorTypes = [
94 { name: 'Network', initializer: () => new Error('Network') },
95 ]
96β
97 render(() => (
98 <SolidQueryDevtools client={queryClient} errorTypes={errorTypes} />
99 ))
100β
101 expect(setErrorTypes).toHaveBeenCalledWith(errorTypes)
102 })
103β
104 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
105 const setErrorTypes = vi.spyOn(
106 TanstackQueryDevtools.prototype,
107 'setErrorTypes',
108 )
109 render(() => <SolidQueryDevtools client={queryClient} />)
110β
111 expect(setErrorTypes).toHaveBeenCalledWith([])
112 })
113β
114 it('should forward "theme" to the devtools instance', () => {
115 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
116 render(() => <SolidQueryDevtools client={queryClient} theme="dark" />)
117β
118 expect(setTheme).toHaveBeenCalledWith('dark')
119 })
120β
121 it('should default "theme" to "system" when the prop is omitted', () => {
122 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
123 render(() => <SolidQueryDevtools client={queryClient} />)
124β
125 expect(setTheme).toHaveBeenCalledWith('system')
126 })
127β
128 it('should forward the resolved "QueryClient" via "setClient"', () => {
129 const setClient = vi.spyOn(TanstackQueryDevtools.prototype, 'setClient')
130 render(() => <SolidQueryDevtools client={queryClient} />)
131β
132 expect(setClient).toHaveBeenCalledWith(queryClient)
133 })
134β
135 it('should forward a "buttonPosition" change to the devtools instance after mount', () => {
136 const setButtonPosition = vi.spyOn(
137 TanstackQueryDevtools.prototype,
138 'setButtonPosition',
139 )
140 const [buttonPosition, setButtonPositionSignal] =
141 createSignal<DevtoolsButtonPosition>('bottom-right')
142β
143 render(() => (
144 <SolidQueryDevtools
145 client={queryClient}
146 buttonPosition={buttonPosition()}
147 />
148 ))
149 setButtonPosition.mockClear()
150β
151 setButtonPositionSignal('top-left')
152β
153 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
154 })
155β
156 it('should forward a "position" change to the devtools instance after mount', () => {
157 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
158 const [position, setPositionSignal] =
159 createSignal<DevtoolsPosition>('bottom')
160β
161 render(() => (
162 <SolidQueryDevtools client={queryClient} position={position()} />
163 ))
164 setPosition.mockClear()
165β
166 setPositionSignal('top')
167β
168 expect(setPosition).toHaveBeenCalledWith('top')
169 })
170β
171 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
172 const setInitialIsOpen = vi.spyOn(
173 TanstackQueryDevtools.prototype,
174 'setInitialIsOpen',
175 )
176 const [initialIsOpen, setInitialIsOpenSignal] = createSignal(false)
177β
178 render(() => (
179 <SolidQueryDevtools
180 client={queryClient}
181 initialIsOpen={initialIsOpen()}
182 />
183 ))
184 setInitialIsOpen.mockClear()
185β
186 setInitialIsOpenSignal(true)
187β
188 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
189 })
190β
191 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
192 const setErrorTypes = vi.spyOn(
193 TanstackQueryDevtools.prototype,
194 'setErrorTypes',
195 )
196 const [errorTypes, setErrorTypesSignal] = createSignal<
197 Array<DevtoolsErrorType>
198 >([])
199β
200 render(() => (
201 <SolidQueryDevtools client={queryClient} errorTypes={errorTypes()} />
202 ))
203 setErrorTypes.mockClear()
204β
205 const nextErrorTypes = [
206 { name: 'Network', initializer: () => new Error('Network') },
207 ]
208 setErrorTypesSignal(nextErrorTypes)
209β
210 expect(setErrorTypes).toHaveBeenCalledWith(nextErrorTypes)
211 })
212β
213 it('should forward a "theme" change to the devtools instance after mount', () => {
214 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
215 const [theme, setThemeSignal] = createSignal<Theme>('light')
216β
217 render(() => <SolidQueryDevtools client={queryClient} theme={theme()} />)
218 setTheme.mockClear()
219β
220 setThemeSignal('dark')
221β
222 expect(setTheme).toHaveBeenCalledWith('dark')
223 })
224β
225 it('should call "unmount" on the devtools instance when the component unmounts', () => {
226 const unmount = vi.spyOn(TanstackQueryDevtools.prototype, 'unmount')
227 const { unmount: unmountComponent } = render(() => (
228 <SolidQueryDevtools client={queryClient} />
229 ))
230 unmountComponent()
231β
232 expect(unmount).toHaveBeenCalled()
233 })
234β
235 it('should return null in non-development environments', async () => {
236 vi.doMock('solid-js/web', async (importOriginal) => {
237 const actual = await importOriginal()
238 return Object.assign({}, actual, { isDev: false })
239 })
240 vi.resetModules()
241β
242 try {
243 const { SolidQueryDevtools: ProductionDevtools } = await import('..')
244 expect(ProductionDevtools({})).toBeNull()
245 } finally {
246 vi.doUnmock('solid-js/web')
247 vi.resetModules()
248 }
249 })
250})