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

with-devtools.test.ts

607 lines15.6 KB

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