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

devtoolsPanel.test.tsx

170 lines5.1 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
4import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
5import SolidQueryDevtoolsPanel from '../devtoolsPanel'
6​
7describe('SolidQueryDevtoolsPanel', () => {
8 let queryClient: QueryClient
9​
10 beforeEach(() => {
11 queryClient = new QueryClient()
12 })
13​
14 afterEach(() => {
15 vi.restoreAllMocks()
16 })
17​
18 it('should throw an error if no query client has been set', () => {
19 expect(() => render(() => <SolidQueryDevtoolsPanel />)).toThrow(
20 'No QueryClient set, use QueryClientProvider to set one',
21 )
22 })
23​
24 it('should not throw an error if query client is provided via context', () => {
25 expect(() =>
26 render(() => (
27 <QueryClientProvider client={queryClient}>
28 <SolidQueryDevtoolsPanel />
29 </QueryClientProvider>
30 )),
31 ).not.toThrow()
32 })
33​
34 it('should not throw an error if query client is provided via props', () => {
35 expect(() =>
36 render(() => <SolidQueryDevtoolsPanel client={queryClient} />),
37 ).not.toThrow()
38 })
39​
40 it('should forward "onClose" to the devtools instance', () => {
41 const setOnClose = vi.spyOn(
42 TanstackQueryDevtoolsPanel.prototype,
43 'setOnClose',
44 )
45 const onClose = vi.fn()
46​
47 render(() => (
48 <SolidQueryDevtoolsPanel client={queryClient} onClose={onClose} />
49 ))
50​
51 expect(setOnClose).toHaveBeenCalledWith(onClose)
52 })
53​
54 it('should default "onClose" to a no-op function when the prop is omitted', () => {
55 const setOnClose = vi.spyOn(
56 TanstackQueryDevtoolsPanel.prototype,
57 'setOnClose',
58 )
59 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
60​
61 const forwarded = setOnClose.mock.calls[0]![0]
62 expect(forwarded).toBeInstanceOf(Function)
63 expect(forwarded()).toBeUndefined()
64 })
65​
66 it('should forward "errorTypes" to the devtools instance', () => {
67 const setErrorTypes = vi.spyOn(
68 TanstackQueryDevtoolsPanel.prototype,
69 'setErrorTypes',
70 )
71 const errorTypes = [
72 { name: 'Network', initializer: () => new Error('Network') },
73 ]
74​
75 render(() => (
76 <SolidQueryDevtoolsPanel client={queryClient} errorTypes={errorTypes} />
77 ))
78​
79 expect(setErrorTypes).toHaveBeenCalledWith(errorTypes)
80 })
81​
82 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
83 const setErrorTypes = vi.spyOn(
84 TanstackQueryDevtoolsPanel.prototype,
85 'setErrorTypes',
86 )
87 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
88​
89 expect(setErrorTypes).toHaveBeenCalledWith([])
90 })
91​
92 it('should forward "theme" to the devtools instance', () => {
93 const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme')
94 render(() => <SolidQueryDevtoolsPanel client={queryClient} theme="dark" />)
95​
96 expect(setTheme).toHaveBeenCalledWith('dark')
97 })
98​
99 it('should default "theme" to "system" when the prop is omitted', () => {
100 const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme')
101 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
102​
103 expect(setTheme).toHaveBeenCalledWith('system')
104 })
105​
106 it('should forward the resolved "QueryClient" via "setClient"', () => {
107 const setClient = vi.spyOn(
108 TanstackQueryDevtoolsPanel.prototype,
109 'setClient',
110 )
111 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
112​
113 expect(setClient).toHaveBeenCalledWith(queryClient)
114 })
115​
116 it('should preserve the default container height when "style" omits "height"', () => {
117 const { container } = render(() => (
118 <SolidQueryDevtoolsPanel
119 client={queryClient}
120 style={{ width: '300px' }}
121 />
122 ))
123​
124 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
125 height: '500px',
126 width: '300px',
127 })
128 })
129​
130 it('should let "style" override the default container height on the rendered element', () => {
131 const { container } = render(() => (
132 <SolidQueryDevtoolsPanel
133 client={queryClient}
134 style={{ width: '300px', height: '300px' }}
135 />
136 ))
137​
138 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
139 height: '300px',
140 width: '300px',
141 })
142 })
143​
144 it('should call "unmount" on the devtools instance when the component unmounts', () => {
145 const unmount = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'unmount')
146 const { unmount: unmountComponent } = render(() => (
147 <SolidQueryDevtoolsPanel client={queryClient} />
148 ))
149 unmountComponent()
150​
151 expect(unmount).toHaveBeenCalled()
152 })
153​
154 it('should return null in non-development environments', async () => {
155 vi.doMock('solid-js/web', async (importOriginal) => {
156 const actual = await importOriginal()
157 return Object.assign({}, actual, { isDev: false })
158 })
159 vi.resetModules()
160​
161 try {
162 const { SolidQueryDevtoolsPanel: ProductionDevtoolsPanel } =
163 await import('..')
164 expect(ProductionDevtoolsPanel({})).toBeNull()
165 } finally {
166 vi.doUnmock('solid-js/web')
167 vi.resetModules()
168 }
169 })
170})