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