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 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
153 })
154β
155 it('should forward a "position" change to the devtools instance after mount', () => {
156 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
157 const [position, setPositionSignal] =
158 createSignal<DevtoolsPosition>('bottom')
159β
160 render(() => (
161 <SolidQueryDevtools client={queryClient} position={position()} />
162 ))
163 setPosition.mockClear()
164β
165 setPositionSignal('top')
166 expect(setPosition).toHaveBeenCalledWith('top')
167 })
168β
169 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
170 const setInitialIsOpen = vi.spyOn(
171 TanstackQueryDevtools.prototype,
172 'setInitialIsOpen',
173 )
174 const [initialIsOpen, setInitialIsOpenSignal] = createSignal(false)
175β
176 render(() => (
177 <SolidQueryDevtools
178 client={queryClient}
179 initialIsOpen={initialIsOpen()}
180 />
181 ))
182 setInitialIsOpen.mockClear()
183β
184 setInitialIsOpenSignal(true)
185 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
186 })
187β
188 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
189 const setErrorTypes = vi.spyOn(
190 TanstackQueryDevtools.prototype,
191 'setErrorTypes',
192 )
193 const [errorTypes, setErrorTypesSignal] = createSignal<
194 Array<DevtoolsErrorType>
195 >([])
196β
197 render(() => (
198 <SolidQueryDevtools client={queryClient} errorTypes={errorTypes()} />
199 ))
200 setErrorTypes.mockClear()
201β
202 const nextErrorTypes = [
203 { name: 'Network', initializer: () => new Error('Network') },
204 ]
205 setErrorTypesSignal(nextErrorTypes)
206 expect(setErrorTypes).toHaveBeenCalledWith(nextErrorTypes)
207 })
208β
209 it('should forward a "theme" change to the devtools instance after mount', () => {
210 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
211 const [theme, setThemeSignal] = createSignal<Theme>('light')
212β
213 render(() => <SolidQueryDevtools client={queryClient} theme={theme()} />)
214 setTheme.mockClear()
215β
216 setThemeSignal('dark')
217 expect(setTheme).toHaveBeenCalledWith('dark')
218 })
219β
220 it('should call "unmount" on the devtools instance when the component unmounts', () => {
221 const unmount = vi.spyOn(TanstackQueryDevtools.prototype, 'unmount')
222 const { unmount: unmountComponent } = render(() => (
223 <SolidQueryDevtools client={queryClient} />
224 ))
225 unmountComponent()
226β
227 expect(unmount).toHaveBeenCalled()
228 })
229β
230 it('should return null in non-development environments', async () => {
231 vi.doMock('solid-js/web', async (importOriginal) => {
232 const actual = await importOriginal()
233 return Object.assign({}, actual, { isDev: false })
234 })
235 vi.resetModules()
236β
237 try {
238 const { SolidQueryDevtools: ProductionDevtools } = await import('..')
239 expect(ProductionDevtools({})).toBeNull()
240 } finally {
241 vi.doUnmock('solid-js/web')
242 vi.resetModules()
243 }
244 })
245})