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

inject-devtools-panel.test.ts

322 lines9.6 KB

1import {
2 ElementRef,
3 EnvironmentInjector,
4 createEnvironmentInjector,
5 provideZonelessChangeDetection,
6 signal,
7} from '@angular/core'
8import { TestBed } from '@angular/core/testing'
9import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
10import { QueryClient } from '@tanstack/query-core'
11import { provideTanStackQuery } from '../providers'
12import { injectDevtoolsPanel } from '../devtools-panel'
13import type {
14 TanstackQueryDevtoolsPanel,
15 Theme,
16} from '@tanstack/query-devtools'
17​
18const mockDevtoolsPanelInstance = {
19 mount: vi.fn<TanstackQueryDevtoolsPanel['mount']>(),
20 unmount: vi.fn<TanstackQueryDevtoolsPanel['unmount']>(),
21 setClient: vi.fn<TanstackQueryDevtoolsPanel['setClient']>(),
22 setErrorTypes: vi.fn<TanstackQueryDevtoolsPanel['setErrorTypes']>(),
23 setOnClose: vi.fn<TanstackQueryDevtoolsPanel['setOnClose']>(),
24 setTheme: vi.fn<TanstackQueryDevtoolsPanel['setTheme']>(),
26​
27const mocks = vi.hoisted(() => {
28 function MockTanstackQueryDevtoolsPanel() {
29 return mockDevtoolsPanelInstance
30 }
31​
32 return {
33 mockTanstackQueryDevtoolsPanel: vi.fn<
34 typeof MockTanstackQueryDevtoolsPanel
35 >(MockTanstackQueryDevtoolsPanel),
36 devtoolsImportError: null as Error | null,
37 }
38})
39​
40// 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
41vi.mock('@tanstack/query-devtools', () => ({
42 get TanstackQueryDevtoolsPanel() {
43 if (mocks.devtoolsImportError) throw mocks.devtoolsImportError
44 return mocks.mockTanstackQueryDevtoolsPanel
45 },
46}))
47​
48describe('injectDevtoolsPanel', () => {
49 let queryClient: QueryClient
50 let mockElementRef: ElementRef
51​
52 const waitForDevtoolsToBeCreated = async () => {
53 await vi.waitFor(() => {
54 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
55 })
56 }
57​
58 beforeEach(() => {
59 vi.clearAllMocks()
60 mocks.devtoolsImportError = null
61 queryClient = new QueryClient()
62 mockElementRef = new ElementRef(document.createElement('div'))
63 TestBed.configureTestingModule({
64 providers: [
65 provideZonelessChangeDetection(),
66 provideTanStackQuery(queryClient),
67 { provide: ElementRef, useValue: signal(mockElementRef) },
68 ],
69 })
70 })
71​
72 afterEach(() => {
73 vi.restoreAllMocks()
74 })
75​
76 it('should return a DevtoolsPanelRef', () => {
77 const result = TestBed.runInInjectionContext(() => {
78 return injectDevtoolsPanel(() => ({
79 hostElement: TestBed.inject(ElementRef),
80 }))
81 })
82​
83 expect(result).toStrictEqual({
84 destroy: expect.any(Function),
85 })
86 })
87​
88 it('should initialize TanstackQueryDevtoolsPanel', async () => {
89 TestBed.runInInjectionContext(() => {
90 injectDevtoolsPanel(() => ({
91 hostElement: TestBed.inject(ElementRef),
92 }))
93 })
94​
95 TestBed.tick()
96 await waitForDevtoolsToBeCreated()
97 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
98 })
99​
100 it('should destroy TanstackQueryDevtoolsPanel', async () => {
101 const result = TestBed.runInInjectionContext(() => {
102 return injectDevtoolsPanel(() => ({
103 hostElement: TestBed.inject(ElementRef),
104 }))
105 })
106​
107 TestBed.tick()
108 await waitForDevtoolsToBeCreated()
109​
110 result.destroy()
111​
112 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
113 })
114​
115 it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', async () => {
116 const hostElement = signal<ElementRef>(mockElementRef)
117​
118 TestBed.runInInjectionContext(() => {
119 return injectDevtoolsPanel(() => ({
120 hostElement: hostElement(),
121 }))
122 })
123​
124 TestBed.tick()
125 await waitForDevtoolsToBeCreated()
126 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
127​
128 hostElement.set(null as unknown as ElementRef)
129 TestBed.tick()
130 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
131 })
132​
133 it('should update client', async () => {
134 const client = signal(new QueryClient())
135​
136 TestBed.runInInjectionContext(() => {
137 return injectDevtoolsPanel(() => ({
138 hostElement: TestBed.inject(ElementRef),
139 client: client(),
140 }))
141 })
142​
143 TestBed.tick()
144 await waitForDevtoolsToBeCreated()
145 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
146​
147 client.set(new QueryClient())
148 TestBed.tick()
149 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
150 })
151​
152 it('should update error types', async () => {
153 const errorTypes = signal([])
154​
155 TestBed.runInInjectionContext(() => {
156 return injectDevtoolsPanel(() => ({
157 hostElement: TestBed.inject(ElementRef),
158 errorTypes: errorTypes(),
159 }))
160 })
161​
162 TestBed.tick()
163 await waitForDevtoolsToBeCreated()
164 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
165​
166 errorTypes.set([])
167 TestBed.tick()
168 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
169 })
170​
171 it('should update onclose', async () => {
172 const functionA = () => {}
173 const functionB = () => {}
174​
175 const onClose = signal(functionA)
176​
177 TestBed.runInInjectionContext(() => {
178 return injectDevtoolsPanel(() => ({
179 hostElement: TestBed.inject(ElementRef),
180 onClose: onClose(),
181 }))
182 })
183​
184 TestBed.tick()
185 await waitForDevtoolsToBeCreated()
186 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
187​
188 onClose.set(functionB)
189 TestBed.tick()
190 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
191 })
192​
193 it('should pass hideDisabledQueries and theme to TanstackQueryDevtoolsPanel', async () => {
194 TestBed.runInInjectionContext(() => {
195 injectDevtoolsPanel(() => ({
196 hostElement: TestBed.inject(ElementRef),
197 hideDisabledQueries: true,
198 theme: 'dark',
199 }))
200 })
201​
202 TestBed.tick()
203 await waitForDevtoolsToBeCreated()
204 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
205 expect.objectContaining({ hideDisabledQueries: true, theme: 'dark' }),
206 )
207 })
208​
209 it('should update theme', async () => {
210 const theme = signal<Theme | undefined>('light')
211​
212 TestBed.runInInjectionContext(() => {
213 return injectDevtoolsPanel(() => ({
214 hostElement: TestBed.inject(ElementRef),
215 theme: theme(),
216 }))
217 })
218​
219 TestBed.tick()
220 await waitForDevtoolsToBeCreated()
221 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(0)
222​
223 theme.set('dark')
224 TestBed.tick()
225 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(1)
226 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledWith('dark')
227​
228 theme.set(undefined)
229 TestBed.tick()
230 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(2)
231 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenLastCalledWith(
232 undefined,
233 )
234 })
235​
236 it('should create TanstackQueryDevtoolsPanel once with the latest options when they change while loading', async () => {
237 const consoleErrorMock = vi
238 .spyOn(console, 'error')
239 .mockReturnValue(undefined)
240 const theme = signal<Theme>('light')
241​
242 TestBed.runInInjectionContext(() => {
243 return injectDevtoolsPanel(() => ({
244 hostElement: TestBed.inject(ElementRef),
245 theme: theme(),
246 }))
247 })
248​
249 TestBed.tick()
250 theme.set('dark')
251 TestBed.tick()
252 await vi.dynamicImportSettled()
253 TestBed.tick()
254 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
255 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
256 expect.objectContaining({ theme: 'dark' }),
257 )
258 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
259 expect(consoleErrorMock).not.toHaveBeenCalled()
260 })
261​
262 it('should not create TanstackQueryDevtoolsPanel when hostElement is removed while loading', async () => {
263 const hostElement = signal<ElementRef | undefined>(mockElementRef)
264​
265 TestBed.runInInjectionContext(() => {
266 return injectDevtoolsPanel(() => ({
267 hostElement: hostElement(),
268 }))
269 })
270​
271 TestBed.tick()
272 hostElement.set(undefined)
273 TestBed.tick()
274 await vi.dynamicImportSettled()
275 TestBed.tick()
276 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
277 expect(mockDevtoolsPanelInstance.mount).not.toHaveBeenCalled()
278 })
279​
280 it('should not create TanstackQueryDevtoolsPanel when the injector is destroyed while loading', async () => {
281 const injector = createEnvironmentInjector(
282 [],
283 TestBed.inject(EnvironmentInjector),
284 )
285​
286 injectDevtoolsPanel(() => ({ hostElement: mockElementRef }), { injector })
287​
288 TestBed.tick()
289 injector.destroy()
290 await vi.dynamicImportSettled()
291 TestBed.tick()
292 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
293 expect(mockDevtoolsPanelInstance.mount).not.toHaveBeenCalled()
294 })
295​
296 it('should load TanstackQueryDevtoolsPanel only once when loading fails, even if options change', async () => {
297 const consoleErrorMock = vi
298 .spyOn(console, 'error')
299 .mockReturnValue(undefined)
300 mocks.devtoolsImportError = new Error('Failed to load devtools')
301 const theme = signal<Theme>('light')
302​
303 TestBed.runInInjectionContext(() => {
304 return injectDevtoolsPanel(() => ({
305 hostElement: TestBed.inject(ElementRef),
306 theme: theme(),
307 }))
308 })
309​
310 TestBed.tick()
311 await vi.dynamicImportSettled()
312 theme.set('dark')
313 TestBed.tick()
314 await vi.dynamicImportSettled()
315 expect(consoleErrorMock).toHaveBeenCalledTimes(1)
316 expect(consoleErrorMock).toHaveBeenCalledWith(
317 'Install @tanstack/query-devtools or reinstall without --omit=optional.',
318 mocks.devtoolsImportError,
319 )
320 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
321 })
322})