Open History to see the last commit
1import { TanstackQueryDevtools } 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 { 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 "shadowDOMTarget" to the devtools constructor', () => {
122 const shadowDOMTarget = document
123 .createElement('div')
124 .attachShadow({ mode: 'open' })
125β
126 render(
127 <ReactQueryDevtools
128 client={queryClient}
129 shadowDOMTarget={shadowDOMTarget}
130 />,
131 )
132β
133 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
134 expect.objectContaining({ shadowDOMTarget }),
135 )
136 })
137β
138 it('should forward "hideDisabledQueries" to the devtools constructor', () => {
139 render(
140 <ReactQueryDevtools client={queryClient} hideDisabledQueries={true} />,
141 )
142β
143 expect(TanstackQueryDevtools).toHaveBeenCalledWith(
144 expect.objectContaining({ hideDisabledQueries: true }),
145 )
146 })
147β
148 it('should create the devtools instance only once across re-renders', () => {
149 const { rerender } = render(
150 <ReactQueryDevtools client={queryClient} position="bottom" />,
151 )
152β
153 rerender(<ReactQueryDevtools client={queryClient} position="top" />)
154β
155 expect(TanstackQueryDevtools).toHaveBeenCalledTimes(1)
156 })
157β
158 it('should forward a "buttonPosition" change to the devtools instance after mount', () => {
159 const { rerender } = render(
160 <ReactQueryDevtools client={queryClient} buttonPosition="bottom-right" />,
161 )
162 setButtonPositionMock.mockClear()
163β
164 rerender(
165 <ReactQueryDevtools client={queryClient} buttonPosition="top-left" />,
166 )
167β
168 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
169 })
170β
171 it('should forward a "position" change to the devtools instance after mount', () => {
172 const { rerender } = render(
173 <ReactQueryDevtools client={queryClient} position="bottom" />,
174 )
175 setPositionMock.mockClear()
176β
177 rerender(<ReactQueryDevtools client={queryClient} position="top" />)
178β
179 expect(setPositionMock).toHaveBeenCalledWith('top')
180 })
181β
182 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
183 const { rerender } = render(
184 <ReactQueryDevtools client={queryClient} initialIsOpen={false} />,
185 )
186 setInitialIsOpenMock.mockClear()
187β
188 rerender(<ReactQueryDevtools client={queryClient} initialIsOpen={true} />)
189β
190 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
191 })
192β
193 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
194 const { rerender } = render(
195 <ReactQueryDevtools client={queryClient} errorTypes={[]} />,
196 )
197 setErrorTypesMock.mockClear()
198β
199 const errorTypes = [
200 { name: 'Network', initializer: () => new Error('Network') },
201 ]
202 rerender(
203 <ReactQueryDevtools client={queryClient} errorTypes={errorTypes} />,
204 )
205β
206 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
207 })
208β
209 it('should forward a "theme" change to the devtools instance after mount', () => {
210 const { rerender } = render(
211 <ReactQueryDevtools client={queryClient} theme="light" />,
212 )
213 setThemeMock.mockClear()
214β
215 rerender(<ReactQueryDevtools client={queryClient} theme="dark" />)
216β
217 expect(setThemeMock).toHaveBeenCalledWith('dark')
218 })
219β
220 it('should call "unmount" on the devtools instance when the component unmounts', () => {
221 const { unmount } = render(<ReactQueryDevtools client={queryClient} />)
222 unmount()
223β
224 expect(unmountMock).toHaveBeenCalledWith()
225 })
226β
227 it('should return null in non-development environments', async () => {
228 vi.stubEnv('NODE_ENV', 'production')
229 vi.resetModules()
230β
231 try {
232 const { ReactQueryDevtools: ProductionDevtools } = await import('..')
233 expect(ProductionDevtools({})).toBeNull()
234 } finally {
235 vi.resetModules()
236 }
237 })
238})