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

with-devtools.test.ts

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