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

with-devtools.test.ts

633 lines17.2 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(0)
261​
262 const newErrorTypes = [
263 {
264 name: '',
265 initializer: () => new Error(),
266 },
267 ]
268​
269 errorTypes.set(newErrorTypes)
270 await TestBed.inject(ApplicationRef).whenStable()
271​
272 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenCalledTimes(1)
273 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenCalledWith(
274 newErrorTypes,
275 )
276 })
277​
278 it('should update client', async () => {
279 const client = signal(new QueryClient())
280​
281 TestBed.configureTestingModule({
282 providers: [
283 provideZonelessChangeDetection(),
284 provideTanStackQuery(
285 () => new QueryClient(),
286 withDevtools(() => ({
287 loadDevtools: true,
288 client,
289 })),
290 ),
291 ],
292 })
293​
294 TestBed.inject(ENVIRONMENT_INITIALIZER)
295 await TestBed.inject(ApplicationRef).whenStable()
296​
297 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledTimes(0)
298​
299 const newClient = new QueryClient()
300 client.set(newClient)
301 await TestBed.inject(ApplicationRef).whenStable()
302​
303 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledTimes(1)
304 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledWith(newClient)
305 })
306​
307 it('should update position', async () => {
308 const position = signal<DevtoolsPosition>('top')
309​
310 TestBed.configureTestingModule({
311 providers: [
312 provideZonelessChangeDetection(),
313 provideTanStackQuery(
314 () => new QueryClient(),
315 withDevtools(() => ({
316 loadDevtools: true,
317 position,
318 })),
319 ),
320 ],
321 })
322​
323 TestBed.inject(ENVIRONMENT_INITIALIZER)
324 await TestBed.inject(ApplicationRef).whenStable()
325​
326 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledTimes(0)
327​
328 position.set('left')
329 await TestBed.inject(ApplicationRef).whenStable()
330​
331 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledTimes(1)
332 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledWith('left')
333 })
334​
335 it('should update button position', async () => {
336 const buttonPosition = signal<DevtoolsButtonPosition>('bottom-left')
337​
338 TestBed.configureTestingModule({
339 providers: [
340 provideZonelessChangeDetection(),
341 provideTanStackQuery(
342 () => new QueryClient(),
343 withDevtools(() => ({
344 loadDevtools: true,
345 buttonPosition,
346 })),
347 ),
348 ],
349 })
350​
351 TestBed.inject(ENVIRONMENT_INITIALIZER)
352 await TestBed.inject(ApplicationRef).whenStable()
353​
354 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledTimes(0)
355​
356 buttonPosition.set('bottom-right')
357 await TestBed.inject(ApplicationRef).whenStable()
358​
359 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledTimes(1)
360 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledWith(
361 'bottom-right',
362 )
363 })
364​
365 it('should update initialIsOpen', async () => {
366 const initialIsOpen = signal(false)
367​
368 TestBed.configureTestingModule({
369 providers: [
370 provideZonelessChangeDetection(),
371 provideTanStackQuery(
372 () => new QueryClient(),
373 withDevtools(() => ({
374 loadDevtools: true,
375 initialIsOpen,
376 })),
377 ),
378 ],
379 })
380​
381 TestBed.inject(ENVIRONMENT_INITIALIZER)
382 await TestBed.inject(ApplicationRef).whenStable()
383​
384 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledTimes(0)
385​
386 initialIsOpen.set(true)
387 await TestBed.inject(ApplicationRef).whenStable()
388​
389 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledTimes(1)
390 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledWith(true)
391 })
392​
393 it('should update theme', async () => {
394 const theme = signal<Theme>('system')
395​
396 TestBed.configureTestingModule({
397 providers: [
398 provideZonelessChangeDetection(),
399 provideTanStackQuery(
400 () => new QueryClient(),
401 withDevtools(() => ({
402 loadDevtools: true,
403 theme,
404 })),
405 ),
406 ],
407 })
408​
409 TestBed.inject(ENVIRONMENT_INITIALIZER)
410 await TestBed.inject(ApplicationRef).whenStable()
411​
412 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledTimes(0)
413​
414 theme.set('dark')
415 await TestBed.inject(ApplicationRef).whenStable()
416​
417 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledTimes(1)
418 expect(mockDevtoolsInstance.setTheme).toHaveBeenCalledWith('dark')
419 })
420​
421 it('should pass construction-only options to the devtools', async () => {
422 const shadowDOMTarget = document
423 .createElement('div')
424 .attachShadow({ mode: 'open' })
425​
426 TestBed.configureTestingModule({
427 providers: [
428 provideZonelessChangeDetection(),
429 provideTanStackQuery(
430 () => new QueryClient(),
431 withDevtools(() => ({
432 loadDevtools: true,
433 styleNonce: 'nonce',
434 shadowDOMTarget,
435 hideDisabledQueries: true,
436 })),
437 ),
438 ],
439 })
440​
441 TestBed.inject(ENVIRONMENT_INITIALIZER)
442 await TestBed.inject(ApplicationRef).whenStable()
443​
444 expect(mockTanstackQueryDevtools).toHaveBeenCalledWith(
445 expect.objectContaining({
446 styleNonce: 'nonce',
447 shadowDOMTarget,
448 hideDisabledQueries: true,
449 }),
450 )
451 })
452​
453 it('should destroy devtools', async () => {
454 const loadDevtools = signal(true)
455​
456 TestBed.configureTestingModule({
457 providers: [
458 provideZonelessChangeDetection(),
459 provideTanStackQuery(
460 () => new QueryClient(),
461 withDevtools(() => ({
462 loadDevtools,
463 })),
464 ),
465 ],
466 })
467​
468 TestBed.inject(ENVIRONMENT_INITIALIZER)
469 await TestBed.inject(ApplicationRef).whenStable()
470​
471 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
472 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(0)
473​
474 loadDevtools.set(false)
475 await TestBed.inject(ApplicationRef).whenStable()
476​
477 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
478 })
479​
480 it('should unmount devtools when injector is destroyed', async () => {
481 TestBed.configureTestingModule({
482 providers: [
483 provideZonelessChangeDetection(),
484 provideTanStackQuery(
485 () => new QueryClient(),
486 withDevtools(() => ({
487 loadDevtools: true,
488 })),
489 ),
490 ],
491 })
492​
493 TestBed.inject(ENVIRONMENT_INITIALIZER)
494 await TestBed.inject(ApplicationRef).whenStable()
495​
496 expect(mockTanstackQueryDevtools).toHaveBeenCalled()
497 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
498 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(0)
499​
500 // Destroy the injector
501 TestBed.resetTestingModule()
502​
503 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
504 })
505​
506 it('should remount devtools when toggled from false to true', async () => {
507 const loadDevtools = signal(false)
508​
509 TestBed.configureTestingModule({
510 providers: [
511 provideZonelessChangeDetection(),
512 provideTanStackQuery(
513 () => new QueryClient(),
514 withDevtools(() => ({
515 loadDevtools,
516 })),
517 ),
518 ],
519 })
520​
521 TestBed.inject(ENVIRONMENT_INITIALIZER)
522 await TestBed.inject(ApplicationRef).whenStable()
523​
524 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
525 expect(mockDevtoolsInstance.mount).not.toHaveBeenCalled()
526​
527 loadDevtools.set(true)
528 await TestBed.inject(ApplicationRef).whenStable()
529​
530 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
531 expect(mockDevtoolsInstance.unmount).not.toHaveBeenCalled()
532​
533 loadDevtools.set(false)
534 await TestBed.inject(ApplicationRef).whenStable()
535​
536 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
537 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
538​
539 loadDevtools.set(true)
540 await TestBed.inject(ApplicationRef).whenStable()
541​
542 // Should remount (mount called twice now)
543 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(2)
544 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
545 })
546​
547 describe('injection context', () => {
548 it('should inject dependencies directly in the callback', async () => {
549 const firstService = { value: 'first' }
550 const secondService = { value: 'second' }
551 const firstServiceToken = new InjectionToken('FirstService')
552 const secondServiceToken = new InjectionToken('SecondService')
553 const withDevtoolsFn = vi.fn(() => {
554 const first = inject(firstServiceToken)
555 const second = inject(secondServiceToken)
556​
557 return {
558 loadDevtools: true,
559 initialIsOpen: first === firstService && second === secondService,
560 }
561 })
562​
563 TestBed.configureTestingModule({
564 providers: [
565 provideZonelessChangeDetection(),
566 { provide: firstServiceToken, useValue: firstService },
567 { provide: secondServiceToken, useValue: secondService },
568 provideTanStackQuery(
569 () => new QueryClient(),
570 withDevtools(withDevtoolsFn),
571 ),
572 ],
573 })
574​
575 TestBed.inject(ENVIRONMENT_INITIALIZER)
576 await TestBed.inject(ApplicationRef).whenStable()
577​
578 expect(withDevtoolsFn).toHaveBeenCalled()
579 expect(mockTanstackQueryDevtools).toHaveBeenCalledWith(
580 expect.objectContaining({ initialIsOpen: true }),
581 )
582 })
583​
584 it('should reactively update when an injected service changes', async () => {
585 class ReactiveService {
586 enabled = signal(false)
587 position = signal<DevtoolsPosition>('bottom')
588 }
589​
590 const withDevtoolsFn = vi.fn(() => {
591 const service = inject(ReactiveService)
592 return {
593 loadDevtools: service.enabled,
594 position: service.position,
595 }
596 })
597​
598 TestBed.configureTestingModule({
599 providers: [
600 provideZonelessChangeDetection(),
601 ReactiveService,
602 provideTanStackQuery(
603 () => new QueryClient(),
604 withDevtools(withDevtoolsFn),
605 ),
606 ],
607 })
608​
609 TestBed.inject(ENVIRONMENT_INITIALIZER)
610 await TestBed.inject(ApplicationRef).whenStable()
611​
612 const service = TestBed.inject(ReactiveService)
613​
614 expect(mockTanstackQueryDevtools).toHaveBeenCalledTimes(1)
615 expect(mockDevtoolsInstance.mount).not.toHaveBeenCalled()
616​
617 service.enabled.set(true)
618 await TestBed.inject(ApplicationRef).whenStable()
619​
620 expect(mockTanstackQueryDevtools).toHaveBeenCalledWith(
621 expect.objectContaining({
622 position: 'bottom',
623 }),
624 )
625​
626 service.position.set('top')
627 await TestBed.inject(ApplicationRef).whenStable()
628​
629 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledWith('top')
630 expect(withDevtoolsFn).toHaveBeenCalledTimes(1)
631 })
632 })
633})