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

devtools.test.tsx

245 lines7.5 KB

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})