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 forward a "buttonPosition" change to the devtools instance after mount', () => {
153 const { rerender } = render(
154 <ReactQueryDevtools client={queryClient} buttonPosition="bottom-right" />,
155 )
156 setButtonPositionMock.mockClear()
157β
158 rerender(
159 <ReactQueryDevtools client={queryClient} buttonPosition="top-left" />,
160 )
161β
162 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
163 })
164β
165 it('should forward a "position" change to the devtools instance after mount', () => {
166 const { rerender } = render(
167 <ReactQueryDevtools client={queryClient} position="bottom" />,
168 )
169 setPositionMock.mockClear()
170β
171 rerender(<ReactQueryDevtools client={queryClient} position="top" />)
172β
173 expect(setPositionMock).toHaveBeenCalledWith('top')
174 })
175β
176 it('should forward an "initialIsOpen" change to the devtools instance after mount', () => {
177 const { rerender } = render(
178 <ReactQueryDevtools client={queryClient} initialIsOpen={false} />,
179 )
180 setInitialIsOpenMock.mockClear()
181β
182 rerender(<ReactQueryDevtools client={queryClient} initialIsOpen={true} />)
183β
184 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
185 })
186β
187 it('should forward an "errorTypes" change to the devtools instance after mount', () => {
188 const { rerender } = render(
189 <ReactQueryDevtools client={queryClient} errorTypes={[]} />,
190 )
191 setErrorTypesMock.mockClear()
192β
193 const errorTypes = [
194 { name: 'Network', initializer: () => new Error('Network') },
195 ]
196 rerender(
197 <ReactQueryDevtools client={queryClient} errorTypes={errorTypes} />,
198 )
199β
200 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
201 })
202β
203 it('should forward a "theme" change to the devtools instance after mount', () => {
204 const { rerender } = render(
205 <ReactQueryDevtools client={queryClient} theme="light" />,
206 )
207 setThemeMock.mockClear()
208β
209 rerender(<ReactQueryDevtools client={queryClient} theme="dark" />)
210β
211 expect(setThemeMock).toHaveBeenCalledWith('dark')
212 })
213β
214 it('should call "unmount" on the devtools instance when the component unmounts', () => {
215 const { unmount } = render(<ReactQueryDevtools client={queryClient} />)
216 unmount()
217β
218 expect(unmountMock).toHaveBeenCalled()
219 })
220β
221 it('should return null in non-development environments', async () => {
222 vi.stubEnv('NODE_ENV', 'production')
223 vi.resetModules()
224β
225 try {
226 const { ReactQueryDevtools: ProductionDevtools } = await import('..')
227 expect(ProductionDevtools({})).toBeNull()
228 } finally {
229 vi.unstubAllEnvs()
230 vi.resetModules()
231 }
232 })
233})