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

with-devtools.test.ts

672 lines18.7 KB

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