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

ReactQueryDevtools.test.tsx

243 lines7.6 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()
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})