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

devtoolsPanel.test.tsx

168 lines5.1 KB

1import { render } from '@solidjs/testing-library'
2import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
3import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
4import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
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<() => void>()
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 forward an undefined "theme" to the devtools instance when the prop is omitted', () => {
100 const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme')
101 render(() => <SolidQueryDevtoolsPanel client={queryClient} />)
102​
103 expect(setTheme).toHaveBeenCalledWith(undefined)
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: '100%',
126 display: 'flex',
127 'flex-direction': 'column',
128 width: '300px',
129 })
130 })
131​
132 it('should let "style" override the default container height on the rendered element', () => {
133 const { container } = render(() => (
134 <SolidQueryDevtoolsPanel
135 client={queryClient}
136 style={{ width: '300px', height: '300px' }}
137 />
138 ))
139​
140 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
141 height: '300px',
142 width: '300px',
143 })
144 })
145​
146 it('should call "unmount" on the devtools instance when the component unmounts', () => {
147 const unmount = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'unmount')
148 const { unmount: unmountComponent } = render(() => (
149 <SolidQueryDevtoolsPanel client={queryClient} />
150 ))
151 unmountComponent()
152​
153 expect(unmount).toHaveBeenCalledWith()
154 })
155​
156 it('should return null in non-development environments', async () => {
157 vi.stubEnv('NODE_ENV', 'production')
158 vi.resetModules()
159​
160 try {
161 const { SolidQueryDevtoolsPanel: ProductionDevtoolsPanel } =
162 await import('..')
163 expect(ProductionDevtoolsPanel({})).toBeNull()
164 } finally {
165 vi.resetModules()
166 }
167 })
168})