Open History to see the last commit
1import {
2 ApplicationRef,
3 ENVIRONMENT_INITIALIZER,
4 EnvironmentInjector,
5 InjectionToken,
6 PLATFORM_ID,
7 createEnvironmentInjector,
8 inject,
9 isDevMode,
10 provideZonelessChangeDetection,
11 signal,
12} from '@angular/core'
13import { TestBed } from '@angular/core/testing'
14import { QueryClient, provideTanStackQuery } from '@tanstack/angular-query'
15import { getQueryFeatureProviders } from '@tanstack/angular-query/internal'
16import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
17import { withDevtools } from '../index'
18import type { WritableSignal } from '@angular/core'
19import type {
20 DevtoolsButtonPosition,
21 DevtoolsErrorType,
22 DevtoolsPosition,
23 TanstackQueryDevtools,
24 Theme,
25} from '@tanstack/query-devtools'
26import type { DevtoolsOptions } from '../types'
27β
28const { mockDevtoolsInstance, mockTanstackQueryDevtools } = vi.hoisted(() => {
29 const instance = {
30 mount: vi.fn<TanstackQueryDevtools['mount']>(),
31 unmount: vi.fn<TanstackQueryDevtools['unmount']>(),
32 setClient: vi.fn<TanstackQueryDevtools['setClient']>(),
33 setPosition: vi.fn<TanstackQueryDevtools['setPosition']>(),
34 setErrorTypes: vi.fn<TanstackQueryDevtools['setErrorTypes']>(),
35 setButtonPosition: vi.fn<TanstackQueryDevtools['setButtonPosition']>(),
36 setInitialIsOpen: vi.fn<TanstackQueryDevtools['setInitialIsOpen']>(),
37 setTheme: vi.fn<TanstackQueryDevtools['setTheme']>(),
38 }
39β
40 return {
41 mockDevtoolsInstance: instance,
42 mockTanstackQueryDevtools: vi.fn<() => typeof instance>(
43 function MockTanstackQueryDevtools() {
44 return instance
45 },
46 ),
47 }
48})
49β
50// oxlint-disable-next-line vitest/prefer-import-in-mock -- The mock cannot implement the devtools class's private fields.
51vi.mock('@tanstack/query-devtools', () => ({
52 TanstackQueryDevtools: mockTanstackQueryDevtools,
53}))
54β
55vi.mock(import('@angular/core'), async () => {
56 const actual = await vi.importActual('@angular/core')
57 return {
58 ...actual,
59 isDevMode: vi.fn<() => boolean>(),
60 }
61})
62β
63const mockIsDevMode = vi.mocked(isDevMode)
64β
65describe('withDevtools feature', () => {
66 beforeEach(() => {
67 vi.clearAllMocks()
68 })
69β
70 afterEach(() => {
71 vi.restoreAllMocks()
72 TestBed.resetTestingModule()
73 })
74β
75 it.each([
76 {
77 description: 'should load devtools in development mode',
78 isDevMode: true,
79 expectedCalled: true,
80 },
81 {
82 description: 'should not load devtools in production mode',
83 isDevMode: false,
84 expectedCalled: false,
85 },
86 {
87 description: `should load devtools in development mode when 'loadDevtools' is set to 'auto'`,
88 isDevMode: true,
89 loadDevtools: 'auto',
90 expectedCalled: true,
91 },
92 {
93 description: `should not load devtools in production mode when 'loadDevtools' is set to 'auto'`,
94 isDevMode: false,
95 loadDevtools: 'auto',
96 expectedCalled: false,
97 },
98 {
99 description:
100 "should load devtools in development mode when 'loadDevtools' is set to true",
101 isDevMode: true,
102 loadDevtools: true,
103 expectedCalled: true,
104 },
105 {
106 description:
107 "should load devtools in production mode when 'loadDevtools' is set to true",
108 isDevMode: false,
109 loadDevtools: true,
110 expectedCalled: true,
111 },
112 {
113 description:
114 "should not load devtools in development mode when 'loadDevtools' is set to false",
115 isDevMode: true,
116 loadDevtools: false,
117 expectedCalled: false,
118 },
119 {
120 description:
121 "should not load devtools in production mode when 'loadDevtools' is set to false",
122 isDevMode: false,
123 loadDevtools: false,
124 expectedCalled: false,
125 },
126 ])(
127 '$description',
128 async ({ isDevMode: isDevModeValue, loadDevtools, expectedCalled }) => {
129 mockIsDevMode.mockReturnValue(isDevModeValue)
130β
131 const providers = [
132 provideZonelessChangeDetection(),
133 provideTanStackQuery(
134 () => new QueryClient(),
135 loadDevtools !== undefined
136 ? withDevtools(
137 () =>
138 ({
139 loadDevtools,
140 }) as DevtoolsOptions,
141 )
142 : withDevtools(),
143 ),
144 ]
145β
146 TestBed.configureTestingModule({
147 providers,
148 })
149β
150 TestBed.inject(ENVIRONMENT_INITIALIZER)
151 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
152β
153 await TestBed.inject(ApplicationRef).whenStable()
154β
155 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(
156 expectedCalled ? 1 : 0,
157 )
158 },
159 )
160β
161 it("should throw 'No QueryClient found' when 'loadDevtools' is 'true' and no 'QueryClient' is provided", () => {
162 TestBed.configureTestingModule({
163 providers: [
164 provideZonelessChangeDetection(),
165 getQueryFeatureProviders(
166 withDevtools(() => ({
167 loadDevtools: true,
168 })),
169 ),
170 ],
171 })
172β
173 expect(() => TestBed.inject(ENVIRONMENT_INITIALIZER)).toThrow(
174 'No QueryClient found',
175 )
176 expect(mockTanstackQueryDevtools).not.toHaveBeenCalled()
177 })
178β
179 it('should not continue loading devtools after injector is destroyed', async () => {
180 TestBed.configureTestingModule({
181 providers: [
182 provideZonelessChangeDetection(),
183 provideTanStackQuery(
184 () => new QueryClient(),
185 withDevtools(() => ({
186 loadDevtools: true,
187 })),
188 ),
189 ],
190 })
191β
192 const app = TestBed.inject(ApplicationRef)
193 TestBed.inject(ENVIRONMENT_INITIALIZER)
194 const stable = app.whenStable()
195 // Destroys injector
196 TestBed.resetTestingModule()
197 await stable
198β
199 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
200 expect(mockDevtoolsInstance.mount).not.toHaveBeenCalled()
201 })
202β
203 it('should not create devtools again when already provided', async () => {
204 TestBed.configureTestingModule({
205 providers: [
206 provideZonelessChangeDetection(),
207 provideTanStackQuery(
208 () => new QueryClient(),
209 withDevtools(() => ({
210 loadDevtools: true,
211 })),
212 ),
213 ],
214 })
215β
216 TestBed.inject(ENVIRONMENT_INITIALIZER)
217 await TestBed.inject(ApplicationRef).whenStable()
218β
219 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
220β
221 const injector = TestBed.inject(EnvironmentInjector)
222β
223 createEnvironmentInjector(
224 [
225 getQueryFeatureProviders(
226 withDevtools(() => ({
227 loadDevtools: true,
228 })),
229 ),
230 ],
231 injector,
232 )
233β
234 TestBed.inject(ENVIRONMENT_INITIALIZER)
235 await TestBed.inject(ApplicationRef).whenStable()
236β
237 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
238 })
239β
240 it('should not load devtools if platform is not browser', async () => {
241 TestBed.configureTestingModule({
242 providers: [
243 {
244 provide: PLATFORM_ID,
245 useValue: 'server',
246 },
247 provideZonelessChangeDetection(),
248 provideTanStackQuery(
249 () => new QueryClient(),
250 withDevtools(() => ({
251 loadDevtools: true,
252 })),
253 ),
254 ],
255 })
256β
257 TestBed.inject(ENVIRONMENT_INITIALIZER)
258 await TestBed.inject(ApplicationRef).whenStable()
259β
260 expect(mockTanstackQueryDevtools).not.toHaveBeenCalled()
261 })
262β
263 it('should update error types', async () => {
264 const errorTypes = signal([] as Array<DevtoolsErrorType>)
265β
266 TestBed.configureTestingModule({
267 providers: [
268 provideZonelessChangeDetection(),
269 provideTanStackQuery(
270 () => new QueryClient(),
271 withDevtools(() => ({
272 loadDevtools: true,
273 errorTypes,
274 })),
275 ),
276 ],
277 })
278β
279 TestBed.inject(ENVIRONMENT_INITIALIZER)
280 await TestBed.inject(ApplicationRef).whenStable()
281β
282 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenCalledTimes(1)
283 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenLastCalledWith(
284 errorTypes(),
285 )
286β
287 const newErrorTypes = [
288 {
289 name: '',
290 initializer: () => new Error(),
291 },
292 ]
293β
294 errorTypes.set(newErrorTypes)
295 await TestBed.inject(ApplicationRef).whenStable()
296β
297 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenCalledTimes(2)
298 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenLastCalledWith(
299 newErrorTypes,
300 )
301 })
302β
303 it('should update client', async () => {
304 const client = signal(new QueryClient())
305β
306 TestBed.configureTestingModule({
307 providers: [
308 provideZonelessChangeDetection(),
309 provideTanStackQuery(
310 () => new QueryClient(),
311 withDevtools(() => ({
312 loadDevtools: true,
313 client,
314 })),
315 ),
316 ],
317 })
318β
319 TestBed.inject(ENVIRONMENT_INITIALIZER)
320 await TestBed.inject(ApplicationRef).whenStable()
321β
322 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledTimes(1)
323 expect(mockDevtoolsInstance.setClient).toHaveBeenLastCalledWith(client())
324β
325 const newClient = new QueryClient()
326 client.set(newClient)
327 await TestBed.inject(ApplicationRef).whenStable()
328β
329 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledTimes(2)
330 expect(mockDevtoolsInstance.setClient).toHaveBeenLastCalledWith(newClient)
331 })
332β
333 it('should update position', async () => {
334 const position = signal<DevtoolsPosition>('top')
335β
336 TestBed.configureTestingModule({
337 providers: [
338 provideZonelessChangeDetection(),
339 provideTanStackQuery(
340 () => new QueryClient(),
341 withDevtools(() => ({
342 loadDevtools: true,
343 position,
344 })),
345 ),
346 ],
347 })
348β
349 TestBed.inject(ENVIRONMENT_INITIALIZER)
350 await TestBed.inject(ApplicationRef).whenStable()
351β
352 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledTimes(1)
353 expect(mockDevtoolsInstance.setPosition).toHaveBeenLastCalledWith('top')
354β
355 position.set('left')
356 await TestBed.inject(ApplicationRef).whenStable()
357β
358 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledTimes(2)
359 expect(mockDevtoolsInstance.setPosition).toHaveBeenLastCalledWith('left')
360 })
361β
362 it('should update button position', async () => {
363 const buttonPosition = signal<DevtoolsButtonPosition>('bottom-left')
364β
365 TestBed.configureTestingModule({
366 providers: [
367 provideZonelessChangeDetection(),
368 provideTanStackQuery(
369 () => new QueryClient(),
370 withDevtools(() => ({
371 loadDevtools: true,
372 buttonPosition,
373 })),
374 ),
375 ],
376 })
377β
378 TestBed.inject(ENVIRONMENT_INITIALIZER)
379 await TestBed.inject(ApplicationRef).whenStable()
380β
381 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledTimes(1)
382 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenLastCalledWith(
383 'bottom-left',
384 )
385β
386 buttonPosition.set('bottom-right')
387 await TestBed.inject(ApplicationRef).whenStable()
388β
389 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledTimes(2)
390 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenLastCalledWith(
391 'bottom-right',
392 )
393 })
394β
395 it('should update initialIsOpen', async () => {
396 const initialIsOpen = signal(false)
397β
398 TestBed.configureTestingModule({
399 providers: [
400 provideZonelessChangeDetection(),
401 provideTanStackQuery(
402 () => new QueryClient(),
403 withDevtools(() => ({
404 loadDevtools: true,
405 initialIsOpen,
406 })),
407 ),
408 ],
409 })
410β
411 TestBed.inject(ENVIRONMENT_INITIALIZER)
412 await TestBed.inject(ApplicationRef).whenStable()
413β
414 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledTimes(1)
415 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenLastCalledWith(
416 false,
417 )
418β
419 initialIsOpen.set(true)
420 await TestBed.inject(ApplicationRef).whenStable()
421β
422 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledTimes(2)
423 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenLastCalledWith(true)
424 })
425β
426 it('should update theme', async () => {
427 const theme = signal<Theme>('system')
428β
429 TestBed.configureTestingModule({
430 providers: [
431 provideZonelessChangeDetection(),
432 provideTanStackQuery(
433 () => new QueryClient(),
434 withDevtools(() => ({
435 loadDevtools: true,
436 theme,
437 })),
438 ),
439 ],
440 })
441β
442 TestBed.inject(ENVIRONMENT_INITIALIZER)
443 await TestBed.inject(ApplicationRef).whenStable()
444β
445 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledTimes(1)
446 expect(mockDevtoolsInstance.setTheme).toHaveBeenLastCalledWith('system')
447β
448 theme.set('dark')
449 await TestBed.inject(ApplicationRef).whenStable()
450β
451 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledTimes(2)
452 expect(mockDevtoolsInstance.setTheme).toHaveBeenLastCalledWith('dark')
453 })
454β
455 it('should update theme to undefined when it is removed', async () => {
456 const theme = signal<Theme | undefined>('dark')
457β
458 TestBed.configureTestingModule({
459 providers: [
460 provideZonelessChangeDetection(),
461 provideTanStackQuery(
462 () => new QueryClient(),
463 withDevtools(() => ({
464 loadDevtools: true,
465 theme,
466 })),
467 ),
468 ],
469 })
470β
471 TestBed.inject(ENVIRONMENT_INITIALIZER)
472 await TestBed.inject(ApplicationRef).whenStable()
473β
474 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledTimes(1)
475 expect(mockDevtoolsInstance.setTheme).toHaveBeenLastCalledWith('dark')
476β
477 theme.set(undefined)
478 await TestBed.inject(ApplicationRef).whenStable()
479β
480 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledTimes(2)
481 expect(mockDevtoolsInstance.setTheme).toHaveBeenLastCalledWith(undefined)
482 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
483 })
484β
485 it('should pass construction-only options to the devtools', async () => {
486 const shadowDOMTarget = document
487 .createElement('div')
488 .attachShadow({ mode: 'open' })
489β
490 TestBed.configureTestingModule({
491 providers: [
492 provideZonelessChangeDetection(),
493 provideTanStackQuery(
494 () => new QueryClient(),
495 withDevtools(() => ({
496 loadDevtools: true,
497 shadowDOMTarget,
498 hideDisabledQueries: true,
499 })),
500 ),
501 ],
502 })
503β
504 TestBed.inject(ENVIRONMENT_INITIALIZER)
505 await TestBed.inject(ApplicationRef).whenStable()
506β
507 expect(mockTanstackQueryDevtools).toHaveBeenCalledWith(
508 expect.objectContaining({
509 shadowDOMTarget,
510 hideDisabledQueries: true,
511 }),
512 )
513 })
514β
515 it('should destroy devtools', async () => {
516 const loadDevtools = signal(true)
517β
518 TestBed.configureTestingModule({
519 providers: [
520 provideZonelessChangeDetection(),
521 provideTanStackQuery(
522 () => new QueryClient(),
523 withDevtools(() => ({
524 loadDevtools,
525 })),
526 ),
527 ],
528 })
529β
530 TestBed.inject(ENVIRONMENT_INITIALIZER)
531 await TestBed.inject(ApplicationRef).whenStable()
532β
533 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
534 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(0)
535β
536 loadDevtools.set(false)
537 await TestBed.inject(ApplicationRef).whenStable()
538β
539 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
540 })
541β
542 it('should unmount devtools when injector is destroyed', async () => {
543 TestBed.configureTestingModule({
544 providers: [
545 provideZonelessChangeDetection(),
546 provideTanStackQuery(
547 () => new QueryClient(),
548 withDevtools(() => ({
549 loadDevtools: true,
550 })),
551 ),
552 ],
553 })
554β
555 TestBed.inject(ENVIRONMENT_INITIALIZER)
556 await TestBed.inject(ApplicationRef).whenStable()
557β
558 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
559 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
560 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(0)
561β
562 // Destroy the injector
563 TestBed.resetTestingModule()
564β
565 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
566 })
567β
568 it('should remount devtools when toggled from false to true', async () => {
569 const loadDevtools = signal(false)
570β
571 TestBed.configureTestingModule({
572 providers: [
573 provideZonelessChangeDetection(),
574 provideTanStackQuery(
575 () => new QueryClient(),
576 withDevtools(() => ({
577 loadDevtools,
578 })),
579 ),
580 ],
581 })
582β
583 TestBed.inject(ENVIRONMENT_INITIALIZER)
584 await TestBed.inject(ApplicationRef).whenStable()
585β
586 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
587 expect(mockDevtoolsInstance.mount).not.toHaveBeenCalled()
588β
589 loadDevtools.set(true)
590 await TestBed.inject(ApplicationRef).whenStable()
591β
592 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
593 expect(mockDevtoolsInstance.unmount).not.toHaveBeenCalled()
594β
595 loadDevtools.set(false)
596 await TestBed.inject(ApplicationRef).whenStable()
597β
598 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
599 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
600β
601 loadDevtools.set(true)
602 await TestBed.inject(ApplicationRef).whenStable()
603β
604 // Should remount (mount called twice now)
605 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(2)
606 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
607 })
608β
609 it('should keep devtools mounted when loadDevtools resolves to the same state', async () => {
610 mockIsDevMode.mockReturnValue(true)
611 const loadDevtools = signal<'auto' | boolean>(true)
612β
613 TestBed.configureTestingModule({
614 providers: [
615 provideZonelessChangeDetection(),
616 provideTanStackQuery(
617 () => new QueryClient(),
618 withDevtools(() => ({ loadDevtools })),
619 ),
620 ],
621 })
622 await TestBed.inject(ApplicationRef).whenStable()
623β
624 const container = document.querySelector('.tsqd-parent-container')
625 expect(container).not.toBeNull()
626 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
627β
628 loadDevtools.set('auto')
629 await TestBed.inject(ApplicationRef).whenStable()
630β
631 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
632 expect(mockDevtoolsInstance.unmount).not.toHaveBeenCalled()
633 expect(document.querySelector('.tsqd-parent-container')).toBe(container)
634 })
635β
636 describe('injection context', () => {
637 it('should inject dependencies directly in the callback', async () => {
638 const firstService = { value: 'first' }
639 const secondService = { value: 'second' }
640 const firstServiceToken = new InjectionToken('FirstService')
641 const secondServiceToken = new InjectionToken('SecondService')
642 const withDevtoolsFn = vi.fn<
643 () => { loadDevtools: boolean; initialIsOpen: boolean }
644 >(() => {
645 const first = inject(firstServiceToken)
646 const second = inject(secondServiceToken)
647β
648 return {
649 loadDevtools: true,
650 initialIsOpen: first === firstService && second === secondService,
651 }
652 })
653β
654 TestBed.configureTestingModule({
655 providers: [
656 provideZonelessChangeDetection(),
657 { provide: firstServiceToken, useValue: firstService },
658 { provide: secondServiceToken, useValue: secondService },
659 provideTanStackQuery(
660 () => new QueryClient(),
661 withDevtools(withDevtoolsFn),
662 ),
663 ],
664 })
665β
666 TestBed.inject(ENVIRONMENT_INITIALIZER)
667 await TestBed.inject(ApplicationRef).whenStable()
668β
669 expect(withDevtoolsFn).toHaveBeenCalledWith()
670 expect(mockTanstackQueryDevtools).toHaveBeenCalledWith(
671 expect.objectContaining({ initialIsOpen: true }),
672 )
673 })
674β
675 it('should reactively update when an injected service changes', async () => {
676 class ReactiveService {
677 enabled = signal(false)
678 position = signal<DevtoolsPosition>('bottom')
679 }
680β
681 const withDevtoolsFn = vi.fn<
682 () => {
683 loadDevtools: WritableSignal<boolean>
684 position: WritableSignal<DevtoolsPosition>
685 }
686 >(() => {
687 const service = inject(ReactiveService)
688 return {
689 loadDevtools: service.enabled,
690 position: service.position,
691 }
692 })
693β
694 TestBed.configureTestingModule({
695 providers: [
696 provideZonelessChangeDetection(),
697 ReactiveService,
698 provideTanStackQuery(
699 () => new QueryClient(),
700 withDevtools(withDevtoolsFn),
701 ),
702 ],
703 })
704β
705 TestBed.inject(ENVIRONMENT_INITIALIZER)
706 await TestBed.inject(ApplicationRef).whenStable()
707β
708 const service = TestBed.inject(ReactiveService)
709β
710 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
711 expect(mockDevtoolsInstance.mount).not.toHaveBeenCalled()
712β
713 service.enabled.set(true)
714 await TestBed.inject(ApplicationRef).whenStable()
715β
716 expect(mockTanstackQueryDevtools).toHaveBeenCalledWith(
717 expect.objectContaining({
718 position: 'bottom',
719 }),
720 )
721β
722 service.position.set('top')
723 await TestBed.inject(ApplicationRef).whenStable()
724β
725 expect(mockDevtoolsInstance.setPosition).toHaveBeenLastCalledWith('top')
726 expect(withDevtoolsFn).toHaveBeenCalledTimes(1)
727 })
728 })
729})