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