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

ReactQueryDevtoolsPanel.test.tsx

199 lines6.6 KB

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