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

ReactQueryDevtoolsPanel.test.tsx

203 lines6.1 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@testing-library/react'
3import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
5import type { ReactQueryDevtoolsPanel as ReactQueryDevtoolsPanelComponent } from '../ReactQueryDevtoolsPanel'
6​
7const mountMock = vi.fn()
8const unmountMock = vi.fn()
9const setClientMock = vi.fn()
10const setOnCloseMock = vi.fn()
11const setErrorTypesMock = vi.fn()
12const setThemeMock = vi.fn()
13​
14vi.mock('@tanstack/query-devtools', () => ({
15 TanstackQueryDevtoolsPanel: vi.fn(function (
16 this: TanstackQueryDevtoolsPanel,
17 ) {
18 this.mount = mountMock
19 this.unmount = unmountMock
20 this.setClient = setClientMock
21 this.setOnClose = setOnCloseMock
22 this.setErrorTypes = setErrorTypesMock
23 this.setTheme = setThemeMock
24 }),
25}))
26​
27describe('ReactQueryDevtoolsPanel', () => {
28 let ReactQueryDevtoolsPanel: typeof ReactQueryDevtoolsPanelComponent
29 let queryClient: QueryClient
30​
31 beforeEach(async () => {
32 vi.clearAllMocks()
33 ;({ ReactQueryDevtoolsPanel } = await import('../ReactQueryDevtoolsPanel'))
34 queryClient = new QueryClient()
35 })
36​
37 it('should throw an error if no query client has been set', () => {
38 expect(() => render(<ReactQueryDevtoolsPanel />)).toThrow(
39 'No QueryClient set, use QueryClientProvider to set one',
40 )
41 })
42​
43 it('should not throw an error if query client is provided via context', () => {
44 expect(() =>
45 render(
46 <QueryClientProvider client={queryClient}>
47 <ReactQueryDevtoolsPanel />
48 </QueryClientProvider>,
49 ),
50 ).not.toThrow()
51 expect(mountMock).toHaveBeenCalled()
52 })
53​
54 it('should not throw an error if query client is provided via props', () => {
55 expect(() =>
56 render(<ReactQueryDevtoolsPanel client={queryClient} />),
57 ).not.toThrow()
58 expect(mountMock).toHaveBeenCalled()
59 })
60​
61 it('should forward "onClose" to the devtools instance', () => {
62 const onClose = vi.fn()
63​
64 render(<ReactQueryDevtoolsPanel client={queryClient} onClose={onClose} />)
65​
66 expect(setOnCloseMock).toHaveBeenCalledWith(onClose)
67 })
68​
69 it('should default "onClose" to a no-op function when the prop is omitted', () => {
70 render(<ReactQueryDevtoolsPanel client={queryClient} />)
71​
72 const forwarded = setOnCloseMock.mock.calls[0]?.[0]
73 expect(forwarded).toBeInstanceOf(Function)
74 expect(forwarded()).toBeUndefined()
75 })
76​
77 it('should forward "errorTypes" to the devtools instance', () => {
78 const errorTypes = [
79 { name: 'Network', initializer: () => new Error('Network') },
80 ]
81​
82 render(
83 <ReactQueryDevtoolsPanel client={queryClient} errorTypes={errorTypes} />,
84 )
85​
86 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
87 })
88​
89 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
90 render(<ReactQueryDevtoolsPanel client={queryClient} />)
91​
92 expect(setErrorTypesMock).toHaveBeenCalledWith([])
93 })
94​
95 it('should forward "theme" to the devtools instance', () => {
96 render(<ReactQueryDevtoolsPanel client={queryClient} theme="dark" />)
97​
98 expect(setThemeMock).toHaveBeenCalledWith('dark')
99 })
100​
101 it('should forward the resolved "QueryClient" via "setClient"', () => {
102 render(<ReactQueryDevtoolsPanel client={queryClient} />)
103​
104 expect(setClientMock).toHaveBeenCalledWith(queryClient)
105 })
106​
107 it('should forward "styleNonce" to the devtools constructor', () => {
108 render(<ReactQueryDevtoolsPanel client={queryClient} styleNonce="abc" />)
109​
110 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
111 expect.objectContaining({ styleNonce: 'abc' }),
112 )
113 })
114​
115 it('should forward "shadowDOMTarget" to the devtools constructor', () => {
116 const shadowDOMTarget = document
117 .createElement('div')
118 .attachShadow({ mode: 'open' })
119​
120 render(
121 <ReactQueryDevtoolsPanel
122 client={queryClient}
123 shadowDOMTarget={shadowDOMTarget}
124 />,
125 )
126​
127 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
128 expect.objectContaining({ shadowDOMTarget }),
129 )
130 })
131​
132 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
133 render(
134 <ReactQueryDevtoolsPanel
135 client={queryClient}
136 hideDisabledQueries={true}
137 />,
138 )
139​
140 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
141 expect.objectContaining({ hideDisabledQueries: true }),
142 )
143 })
144​
145 it('should create the devtools instance only once across re-renders', () => {
146 const { rerender } = render(
147 <ReactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />,
148 )
149​
150 rerender(<ReactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />)
151​
152 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
153 })
154​
155 it('should preserve the default container height when "style" omits "height"', () => {
156 const { container } = render(
157 <ReactQueryDevtoolsPanel
158 client={queryClient}
159 style={{ width: '300px' }}
160 />,
161 )
162​
163 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
164 height: '500px',
165 width: '300px',
166 })
167 })
168​
169 it('should let "style" override the default container height on the rendered element', () => {
170 const { container } = render(
171 <ReactQueryDevtoolsPanel
172 client={queryClient}
173 style={{ width: '300px', height: '300px' }}
174 />,
175 )
176​
177 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
178 height: '300px',
179 width: '300px',
180 })
181 })
182​
183 it('should call "unmount" on the devtools instance when the component unmounts', () => {
184 const { unmount } = render(<ReactQueryDevtoolsPanel client={queryClient} />)
185 unmount()
186​
187 expect(unmountMock).toHaveBeenCalled()
188 })
189​
190 it('should return null in non-development environments', async () => {
191 vi.stubEnv('NODE_ENV', 'production')
192 vi.resetModules()
193​
194 try {
195 const { ReactQueryDevtoolsPanel: ProductionDevtoolsPanel } =
196 await import('..')
197 expect(ProductionDevtoolsPanel({})).toBeNull()
198 } finally {
199 vi.unstubAllEnvs()
200 vi.resetModules()
201 }
202 })
203})