Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
4import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5import SolidQueryDevtools from '../devtools'
6β
7describe('SolidQueryDevtools', () => {
8 afterEach(() => {
9 vi.restoreAllMocks()
10 })
11β
12 it('should throw an error if no query client has been set', () => {
13 expect(() => render(() => <SolidQueryDevtools />)).toThrow(
14 'No QueryClient set, use QueryClientProvider to set one',
15 )
16 })
17β
18 it('should not throw an error if query client is provided via context', () => {
19 const queryClient = new QueryClient()
20β
21 expect(() =>
22 render(() => (
23 <QueryClientProvider client={queryClient}>
24 <SolidQueryDevtools />
25 </QueryClientProvider>
26 )),
27 ).not.toThrow()
28 })
29β
30 it('should not throw an error if query client is provided via props', () => {
31 const queryClient = new QueryClient()
32β
33 expect(() =>
34 render(() => <SolidQueryDevtools client={queryClient} />),
35 ).not.toThrow()
36 })
37β
38 it('should forward "buttonPosition" to the devtools instance', () => {
39 const setButtonPosition = vi.spyOn(
40 TanstackQueryDevtools.prototype,
41 'setButtonPosition',
42 )
43 const queryClient = new QueryClient()
44β
45 render(() => (
46 <SolidQueryDevtools client={queryClient} buttonPosition="top-left" />
47 ))
48β
49 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
50 })
51β
52 it('should forward "position" to the devtools instance', () => {
53 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
54 const queryClient = new QueryClient()
55β
56 render(() => <SolidQueryDevtools client={queryClient} position="left" />)
57β
58 expect(setPosition).toHaveBeenCalledWith('left')
59 })
60β
61 it('should forward "initialIsOpen" to the devtools instance', () => {
62 const setInitialIsOpen = vi.spyOn(
63 TanstackQueryDevtools.prototype,
64 'setInitialIsOpen',
65 )
66 const queryClient = new QueryClient()
67β
68 render(() => (
69 <SolidQueryDevtools client={queryClient} initialIsOpen={true} />
70 ))
71β
72 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
73 })
74β
75 it('should default "initialIsOpen" to "false" when the prop is omitted', () => {
76 const setInitialIsOpen = vi.spyOn(
77 TanstackQueryDevtools.prototype,
78 'setInitialIsOpen',
79 )
80 const queryClient = new QueryClient()
81β
82 render(() => <SolidQueryDevtools client={queryClient} />)
83β
84 expect(setInitialIsOpen).toHaveBeenCalledWith(false)
85 })
86β
87 it('should forward "errorTypes" to the devtools instance', () => {
88 const setErrorTypes = vi.spyOn(
89 TanstackQueryDevtools.prototype,
90 'setErrorTypes',
91 )
92 const queryClient = new QueryClient()
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 const queryClient = new QueryClient()
110β
111 render(() => <SolidQueryDevtools client={queryClient} />)
112β
113 expect(setErrorTypes).toHaveBeenCalledWith([])
114 })
115β
116 it('should forward "theme" to the devtools instance', () => {
117 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
118 const queryClient = new QueryClient()
119β
120 render(() => <SolidQueryDevtools client={queryClient} theme="dark" />)
121β
122 expect(setTheme).toHaveBeenCalledWith('dark')
123 })
124β
125 it('should default "theme" to "system" when the prop is omitted', () => {
126 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
127 const queryClient = new QueryClient()
128β
129 render(() => <SolidQueryDevtools client={queryClient} />)
130β
131 expect(setTheme).toHaveBeenCalledWith('system')
132 })
133β
134 it('should forward the resolved "QueryClient" via "setClient"', () => {
135 const setClient = vi.spyOn(TanstackQueryDevtools.prototype, 'setClient')
136 const queryClient = new QueryClient()
137β
138 render(() => <SolidQueryDevtools client={queryClient} />)
139β
140 expect(setClient).toHaveBeenCalledWith(queryClient)
141 })
142β
143 it('should call "unmount" on the devtools instance when the component unmounts', () => {
144 const unmount = vi.spyOn(TanstackQueryDevtools.prototype, 'unmount')
145 const queryClient = new QueryClient()
146β
147 const { unmount: unmountComponent } = render(() => (
148 <SolidQueryDevtools client={queryClient} />
149 ))
150 unmountComponent()
151β
152 expect(unmount).toHaveBeenCalled()
153 })
154β
155 it('should return null in non-development environments', async () => {
156 vi.doMock('solid-js/web', async (importOriginal) => {
157 const actual = await importOriginal()
158 return Object.assign({}, actual, { isDev: false })
159 })
160 vi.resetModules()
161β
162 try {
163 const { SolidQueryDevtools: ProductionDevtools } = await import('..')
164 expect(ProductionDevtools({})).toBeNull()
165 } finally {
166 vi.doUnmock('solid-js/web')
167 vi.resetModules()
168 }
169 })
170})