Open History to see the last commit
1import {
2 ApplicationRef,
3 ElementRef,
4 provideZonelessChangeDetection,
5 signal,
6} from '@angular/core'
7import { TestBed } from '@angular/core/testing'
8import { QueryClient } from '@tanstack/angular-query'
9import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
10import { injectDevtoolsPanel } from '../devtools-panel'
11import { setupTanStackQueryTestBed } from './test-utils'
12import type {
13 TanstackQueryDevtoolsPanel,
14 Theme,
15} from '@tanstack/query-devtools'
16β
17const mockDevtoolsPanelInstance = {
18 mount: vi.fn<TanstackQueryDevtoolsPanel['mount']>(),
19 unmount: vi.fn<TanstackQueryDevtoolsPanel['unmount']>(),
20 setClient: vi.fn<TanstackQueryDevtoolsPanel['setClient']>(),
21 setErrorTypes: vi.fn<TanstackQueryDevtoolsPanel['setErrorTypes']>(),
22 setOnClose: vi.fn<TanstackQueryDevtoolsPanel['setOnClose']>(),
23 setTheme: vi.fn<TanstackQueryDevtoolsPanel['setTheme']>(),
24}
25β
26const mocks = vi.hoisted(() => {
27 function MockTanstackQueryDevtoolsPanel() {
28 return mockDevtoolsPanelInstance
29 }
30β
31 return {
32 mockTanstackQueryDevtoolsPanel: vi.fn<
33 typeof MockTanstackQueryDevtoolsPanel
34 >(MockTanstackQueryDevtoolsPanel),
35 }
36})
37β
38// oxlint-disable-next-line vitest/prefer-import-in-mock -- The mock cannot implement the devtools class's private fields.
39vi.mock('@tanstack/query-devtools', () => ({
40 TanstackQueryDevtoolsPanel: mocks.mockTanstackQueryDevtoolsPanel,
41}))
42β
43describe('injectDevtoolsPanel', () => {
44 let queryClient: QueryClient
45 let mockElementRef: ElementRef
46β
47 const waitForDevtoolsToBeCreated = async () => {
48 await vi.waitFor(() => {
49 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
50 })
51 }
52β
53 beforeEach(() => {
54 vi.clearAllMocks()
55 queryClient = new QueryClient()
56 mockElementRef = new ElementRef(document.createElement('div'))
57 setupTanStackQueryTestBed(queryClient, {
58 providers: [{ provide: ElementRef, useValue: signal(mockElementRef) }],
59 })
60 })
61β
62 afterEach(() => {
63 vi.restoreAllMocks()
64 })
65β
66 it('should return a DevtoolsPanelRef', () => {
67 const result = TestBed.runInInjectionContext(() => {
68 return injectDevtoolsPanel(() => ({
69 hostElement: TestBed.inject(ElementRef),
70 }))
71 })
72β
73 expect(result).toStrictEqual({
74 destroy: expect.any(Function),
75 })
76 })
77β
78 it("should throw 'No QueryClient found' when no 'QueryClient' is provided", () => {
79 TestBed.resetTestingModule()
80 TestBed.configureTestingModule({
81 providers: [provideZonelessChangeDetection()],
82 })
83β
84 TestBed.runInInjectionContext(() => {
85 injectDevtoolsPanel(() => ({
86 hostElement: mockElementRef,
87 }))
88 })
89β
90 expect(() => TestBed.tick()).toThrow('No QueryClient found')
91 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
92 })
93β
94 it('should initialize TanstackQueryDevtoolsPanel', async () => {
95 TestBed.runInInjectionContext(() => {
96 injectDevtoolsPanel(() => ({
97 hostElement: TestBed.inject(ElementRef),
98 hideDisabledQueries: true,
99 }))
100 })
101β
102 await TestBed.inject(ApplicationRef).whenStable()
103β
104 await waitForDevtoolsToBeCreated()
105β
106 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
107 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
108 expect.objectContaining({ theme: undefined, hideDisabledQueries: true }),
109 )
110 })
111β
112 it('should update the theme and restore the saved preference when it is removed', async () => {
113 const theme = signal<Theme | undefined>('dark')
114β
115 TestBed.runInInjectionContext(() => {
116 injectDevtoolsPanel(() => ({
117 hostElement: mockElementRef,
118 theme: theme(),
119 }))
120 })
121β
122 await TestBed.inject(ApplicationRef).whenStable()
123 await waitForDevtoolsToBeCreated()
124β
125 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
126 expect.objectContaining({ theme: 'dark' }),
127 )
128 expect(mockDevtoolsPanelInstance.setTheme).not.toHaveBeenCalled()
129β
130 theme.set('light')
131 await TestBed.inject(ApplicationRef).whenStable()
132 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenLastCalledWith('light')
133β
134 theme.set(undefined)
135 await TestBed.inject(ApplicationRef).whenStable()
136 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenLastCalledWith(
137 undefined,
138 )
139 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(2)
140 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
141 })
142β
143 it('should destroy TanstackQueryDevtoolsPanel', async () => {
144 const result = TestBed.runInInjectionContext(() => {
145 return injectDevtoolsPanel(() => ({
146 hostElement: TestBed.inject(ElementRef),
147 }))
148 })
149β
150 await TestBed.inject(ApplicationRef).whenStable()
151β
152 await waitForDevtoolsToBeCreated()
153β
154 result.destroy()
155β
156 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
157 })
158β
159 it('should stop reacting after it is manually destroyed', async () => {
160 const hostElement = signal<ElementRef | undefined>(mockElementRef)
161 const result = TestBed.runInInjectionContext(() =>
162 injectDevtoolsPanel(() => ({
163 hostElement: hostElement(),
164 })),
165 )
166β
167 await TestBed.inject(ApplicationRef).whenStable()
168 await waitForDevtoolsToBeCreated()
169β
170 result.destroy()
171 hostElement.set(undefined)
172 hostElement.set(mockElementRef)
173 await TestBed.inject(ApplicationRef).whenStable()
174β
175 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
176 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
177 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
178 })
179β
180 it('should not mount when destroyed while the devtools import is pending', async () => {
181 const result = TestBed.runInInjectionContext(() =>
182 injectDevtoolsPanel(() => ({
183 hostElement: mockElementRef,
184 })),
185 )
186β
187 TestBed.tick()
188 result.destroy()
189 await TestBed.inject(ApplicationRef).whenStable()
190β
191 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
192 expect(mockDevtoolsPanelInstance.mount).not.toHaveBeenCalled()
193 })
194β
195 it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', async () => {
196 const hostElement = signal<ElementRef | null>(mockElementRef)
197β
198 TestBed.runInInjectionContext(() => {
199 return injectDevtoolsPanel(() => ({
200 hostElement: hostElement(),
201 }))
202 })
203β
204 await TestBed.inject(ApplicationRef).whenStable()
205β
206 await waitForDevtoolsToBeCreated()
207β
208 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
209β
210 hostElement.set(null)
211β
212 await TestBed.inject(ApplicationRef).whenStable()
213β
214 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
215 })
216β
217 it('should update client', async () => {
218 const client = signal(new QueryClient())
219β
220 TestBed.runInInjectionContext(() => {
221 return injectDevtoolsPanel(() => ({
222 hostElement: TestBed.inject(ElementRef),
223 client: client(),
224 }))
225 })
226β
227 await TestBed.inject(ApplicationRef).whenStable()
228β
229 await waitForDevtoolsToBeCreated()
230β
231 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
232β
233 client.set(new QueryClient())
234β
235 await TestBed.inject(ApplicationRef).whenStable()
236β
237 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
238 })
239β
240 it('should update error types', async () => {
241 const errorTypes = signal([])
242β
243 TestBed.runInInjectionContext(() => {
244 return injectDevtoolsPanel(() => ({
245 hostElement: TestBed.inject(ElementRef),
246 errorTypes: errorTypes(),
247 }))
248 })
249β
250 await TestBed.inject(ApplicationRef).whenStable()
251β
252 await waitForDevtoolsToBeCreated()
253β
254 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
255β
256 errorTypes.set([])
257β
258 await TestBed.inject(ApplicationRef).whenStable()
259β
260 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
261 })
262β
263 it('should update onClose and clear it when removed', async () => {
264 const functionA = vi.fn<() => void>()
265 const functionB = vi.fn<() => void>()
266β
267 const onClose = signal<typeof functionA | undefined>(functionA)
268β
269 TestBed.runInInjectionContext(() => {
270 return injectDevtoolsPanel(() => ({
271 hostElement: TestBed.inject(ElementRef),
272 onClose: onClose(),
273 }))
274 })
275β
276 await TestBed.inject(ApplicationRef).whenStable()
277β
278 await waitForDevtoolsToBeCreated()
279β
280 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
281β
282 onClose.set(functionB)
283β
284 await TestBed.inject(ApplicationRef).whenStable()
285β
286 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
287 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenLastCalledWith(
288 functionB,
289 )
290β
291 onClose.set(undefined)
292 await TestBed.inject(ApplicationRef).whenStable()
293β
294 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(2)
295 const resetOnClose = mockDevtoolsPanelInstance.setOnClose.mock.lastCall?.[0]
296 expect(resetOnClose).toBeTypeOf('function')
297 resetOnClose?.()
298 expect(functionA).not.toHaveBeenCalled()
299 expect(functionB).not.toHaveBeenCalled()
300 })
301})