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

ReactQueryDevtools.test.tsx

246 lines8.1 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@testing-library/react'
3import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5import type { ReactQueryDevtools as ReactQueryDevtoolsComponent } from '../ReactQueryDevtools'
6​
7const mountMock = vi.fn<TanstackQueryDevtools['mount']>()
8const unmountMock = vi.fn<TanstackQueryDevtools['unmount']>()
9const setClientMock = vi.fn<TanstackQueryDevtools['setClient']>()
10const setButtonPositionMock =
11 vi.fn<TanstackQueryDevtools['setButtonPosition']>()
12const setPositionMock = vi.fn<TanstackQueryDevtools['setPosition']>()
13const setInitialIsOpenMock = vi.fn<TanstackQueryDevtools['setInitialIsOpen']>()
14const setErrorTypesMock = vi.fn<TanstackQueryDevtools['setErrorTypes']>()
15const setThemeMock = vi.fn<TanstackQueryDevtools['setTheme']>()
16​
17// 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
18vi.mock('@tanstack/query-devtools', () => ({
19 TanstackQueryDevtools: vi.fn<(this: TanstackQueryDevtools) => void>(function (
20 this: TanstackQueryDevtools,
21 ) {
22 this.mount = mountMock
23 this.unmount = unmountMock
24 this.setClient = setClientMock
25 this.setButtonPosition = setButtonPositionMock
26 this.setPosition = setPositionMock
27 this.setInitialIsOpen = setInitialIsOpenMock
28 this.setErrorTypes = setErrorTypesMock
29 this.setTheme = setThemeMock
30 }),
31}))
32​
33describe('ReactQueryDevtools', () => {
34 let ReactQueryDevtools: typeof ReactQueryDevtoolsComponent
35 let queryClient: QueryClient
36​
37 beforeEach(async () => {
38 vi.clearAllMocks()
39 ;({ ReactQueryDevtools } = await import('../ReactQueryDevtools'))
40 queryClient = new QueryClient()
41 })
42​
43 it('should throw an error if no query client has been set', () => {
44 expect(() => render(<ReactQueryDevtools />)).toThrow(
45 'No QueryClient set, use QueryClientProvider to set one',
46 )
47 })
48​
49 it('should not throw an error if query client is provided via context', () => {
50 expect(() =>
51 render(
52 <QueryClientProvider client={queryClient}>
53 <ReactQueryDevtools />
54 </QueryClientProvider>,
55 ),
56 ).not.toThrow()
57 expect(mountMock).toHaveBeenCalledWith(expect.any(HTMLDivElement))
58 })
59​
60 it('should not throw an error if query client is provided via props', () => {
61 expect(() =>
62 render(<ReactQueryDevtools client={queryClient} />),
63 ).not.toThrow()
64 expect(mountMock).toHaveBeenCalledWith(expect.any(HTMLDivElement))
65 })
66​
67 it('should forward "buttonPosition" to the devtools instance', () => {
68 render(
69 <ReactQueryDevtools client={queryClient} buttonPosition="top-left" />,
70 )
71​
72 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
73 })
74​
75 it('should forward "position" to the devtools instance', () => {
76 render(<ReactQueryDevtools client={queryClient} position="left" />)
77​
78 expect(setPositionMock).toHaveBeenCalledWith('left')
79 })
80​
81 it('should forward "initialIsOpen" to the devtools instance', () => {
82 render(<ReactQueryDevtools client={queryClient} initialIsOpen={true} />)
83​
84 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
85 })
86​
87 it('should default "initialIsOpen" to "false" when the prop is omitted', () => {
88 render(<ReactQueryDevtools client={queryClient} />)
89​
90 expect(setInitialIsOpenMock).toHaveBeenCalledWith(false)
91 })
92​
93 it('should forward "errorTypes" to the devtools instance', () => {
94 const errorTypes = [
95 { name: 'Network', initializer: () => new Error('Network') },
96 ]
97​
98 render(<ReactQueryDevtools client={queryClient} errorTypes={errorTypes} />)
99​
100 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
101 })
102​
103 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
104 render(<ReactQueryDevtools client={queryClient} />)
105​
106 expect(setErrorTypesMock).toHaveBeenCalledWith([])
107 })
108​
109 it('should forward "theme" to the devtools instance', () => {
110 render(<ReactQueryDevtools client={queryClient} theme="dark" />)
111​
112 expect(setThemeMock).toHaveBeenCalledWith('dark')
113 })
114​
115 it('should forward the resolved "QueryClient" via "setClient"', () => {
116 render(<ReactQueryDevtools client={queryClient} />)
117​
118 expect(setClientMock).toHaveBeenCalledWith(queryClient)
119 })
120​
121 it('should forward "styleNonce" to the devtools constructor', () => {
122 render(<ReactQueryDevtools client={queryClient} styleNonce="abc" />)
123​
124 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
125 expect.objectContaining({ styleNonce: 'abc' }),
126 )
127 })
128​
129 it('should forward "shadowDOMTarget" to the devtools constructor', () => {
130 const shadowDOMTarget = document
131 .createElement('div')
132 .attachShadow({ mode: 'open' })
133​
134 render(
135 <ReactQueryDevtools
136 client={queryClient}
137 shadowDOMTarget={shadowDOMTarget}
138 />,
139 )
140​
141 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
142 expect.objectContaining({ shadowDOMTarget }),
143 )
144 })
145​
146 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
147 render(
148 <ReactQueryDevtools client={queryClient} hideDisabledQueries={true} />,
149 )
150​
151 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
152 expect.objectContaining({ hideDisabledQueries: true }),
153 )
154 })
155​
156 it('should create the devtools instance only once across re-renders', () => {
157 const { rerender } = render(
158 <ReactQueryDevtools client={queryClient} position="bottom" />,
159 )
160​
161 rerender(<ReactQueryDevtools client={queryClient} position="top" />)
162​
163 expect(TanstackQueryDevtools).toHaveBeenCalledTimes(1)
164 })
165​
166 it('should forward a "buttonPosition" change to the devtools instance after mount', () => {
167 const { rerender } = render(
168 <ReactQueryDevtools client={queryClient} buttonPosition="bottom-right" />,
169 )
170 setButtonPositionMock.mockClear()
171​
172 rerender(
173 <ReactQueryDevtools client={queryClient} buttonPosition="top-left" />,
174 )
175​
176 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
177 })
178​
179 it('should forward a "position" change to the devtools instance after mount', () => {
180 const { rerender } = render(
181 <ReactQueryDevtools client={queryClient} position="bottom" />,
182 )
183 setPositionMock.mockClear()
184​
185 rerender(<ReactQueryDevtools client={queryClient} position="top" />)
186​
187 expect(setPositionMock).toHaveBeenCalledWith('top')
188 })
189​
190 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
191 const { rerender } = render(
192 <ReactQueryDevtools client={queryClient} initialIsOpen={false} />,
193 )
194 setInitialIsOpenMock.mockClear()
195​
196 rerender(<ReactQueryDevtools client={queryClient} initialIsOpen={true} />)
197​
198 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
199 })
200​
201 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
202 const { rerender } = render(
203 <ReactQueryDevtools client={queryClient} errorTypes={[]} />,
204 )
205 setErrorTypesMock.mockClear()
206​
207 const errorTypes = [
208 { name: 'Network', initializer: () => new Error('Network') },
209 ]
210 rerender(
211 <ReactQueryDevtools client={queryClient} errorTypes={errorTypes} />,
212 )
213​
214 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
215 })
216​
217 it('should forward a "theme" change to the devtools instance after mount', () => {
218 const { rerender } = render(
219 <ReactQueryDevtools client={queryClient} theme="light" />,
220 )
221 setThemeMock.mockClear()
222​
223 rerender(<ReactQueryDevtools client={queryClient} theme="dark" />)
224​
225 expect(setThemeMock).toHaveBeenCalledWith('dark')
226 })
227​
228 it('should call "unmount" on the devtools instance when the component unmounts', () => {
229 const { unmount } = render(<ReactQueryDevtools client={queryClient} />)
230 unmount()
231​
232 expect(unmountMock).toHaveBeenCalledWith()
233 })
234​
235 it('should return null in non-development environments', async () => {
236 vi.stubEnv('NODE_ENV', 'production')
237 vi.resetModules()
238​
239 try {
240 const { ReactQueryDevtools: ProductionDevtools } = await import('..')
241 expect(ProductionDevtools({})).toBeNull()
242 } finally {
243 vi.resetModules()
244 }
245 })
246})