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

ReactQueryDevtoolsPanel.test.tsx

205 lines6.8 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<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 "styleNonce" to the devtools constructor', () => {
109 render(<ReactQueryDevtoolsPanel client={queryClient} styleNonce="abc" />)
110​
111 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
112 expect.objectContaining({ styleNonce: 'abc' }),
113 )
114 })
115​
116 it('should forward "shadowDOMTarget" to the devtools constructor', () => {
117 const shadowDOMTarget = document
118 .createElement('div')
119 .attachShadow({ mode: 'open' })
120​
121 render(
122 <ReactQueryDevtoolsPanel
123 client={queryClient}
124 shadowDOMTarget={shadowDOMTarget}
125 />,
126 )
127​
128 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
129 expect.objectContaining({ shadowDOMTarget }),
130 )
131 })
132​
133 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
134 render(
135 <ReactQueryDevtoolsPanel
136 client={queryClient}
137 hideDisabledQueries={true}
138 />,
139 )
140​
141 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
142 expect.objectContaining({ hideDisabledQueries: true }),
143 )
144 })
145​
146 it('should create the devtools instance only once across re-renders', () => {
147 const { rerender } = render(
148 <ReactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />,
149 )
150​
151 rerender(<ReactQueryDevtoolsPanel client={queryClient} errorTypes={[]} />)
152​
153 expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
154 })
155​
156 it('should preserve the default container height when "style" omits "height"', () => {
157 const { container } = render(
158 <ReactQueryDevtoolsPanel
159 client={queryClient}
160 style={{ width: '300px' }}
161 />,
162 )
163​
164 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
165 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
166 height: '500px',
167 width: '300px',
168 })
169 })
170​
171 it('should let "style" override the default container height on the rendered element', () => {
172 const { container } = render(
173 <ReactQueryDevtoolsPanel
174 client={queryClient}
175 style={{ width: '300px', height: '300px' }}
176 />,
177 )
178​
179 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
180 expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
181 height: '300px',
182 width: '300px',
183 })
184 })
185​
186 it('should call "unmount" on the devtools instance when the component unmounts', () => {
187 const { unmount } = render(<ReactQueryDevtoolsPanel client={queryClient} />)
188 unmount()
189​
190 expect(unmountMock).toHaveBeenCalledWith()
191 })
192​
193 it('should return null in non-development environments', async () => {
194 vi.stubEnv('NODE_ENV', 'production')
195 vi.resetModules()
196​
197 try {
198 const { ReactQueryDevtoolsPanel: ProductionDevtoolsPanel } =
199 await import('..')
200 expect(ProductionDevtoolsPanel({})).toBeNull()
201 } finally {
202 vi.resetModules()
203 }
204 })
205})