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

with-devtools.test.ts

674 lines17.5 KB

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