Open History to see the last commit
1import {
2 ElementRef,
3 provideExperimentalZonelessChangeDetection,
4 signal,
5} from '@angular/core'
6import { TestBed } from '@angular/core/testing'
7import {
8 QueryClient,
9 provideTanStackQuery,
10} from '@tanstack/angular-query-experimental'
11import { beforeEach, describe, expect, vi } from 'vitest'
12import { injectDevtoolsPanel } from '../inject-devtools-panel'
13β
14const mockDevtoolsPanelInstance = {
15 mount: vi.fn(),
16 unmount: vi.fn(),
17 setClient: vi.fn(),
18 setErrorTypes: vi.fn(),
19 setOnClose: vi.fn(),
20}
21β
22const mocks = vi.hoisted(() => {
23 return {
24 mockTanstackQueryDevtoolsPanel: vi.fn(() => mockDevtoolsPanelInstance),
25 }
26})
27β
28vi.mock('@tanstack/query-devtools', () => ({
29 TanstackQueryDevtoolsPanel: mocks.mockTanstackQueryDevtoolsPanel,
30}))
31β
32describe('injectDevtoolsPanel', () => {
33 let queryClient: QueryClient
34 let mockElementRef: ElementRef
35β
36 beforeEach(() => {
37 queryClient = new QueryClient()
38 mockElementRef = new ElementRef(document.createElement('div'))
39 TestBed.configureTestingModule({
40 providers: [
41 provideExperimentalZonelessChangeDetection(),
42 provideTanStackQuery(queryClient),
43 { provide: ElementRef, useValue: signal(mockElementRef) },
44 ],
45 })
46 })
47β
48 afterEach(() => {
49 vi.restoreAllMocks()
50 })
51β
52 it('should return a DevtoolsPanelRef', () => {
53 const result = TestBed.runInInjectionContext(() => {
54 return injectDevtoolsPanel(() => ({
55 hostElement: TestBed.inject(ElementRef),
56 }))
57 })
58β
59 expect(result).toEqual({
60 destroy: expect.any(Function),
61 })
62 })
63β
64 it('should initialize TanstackQueryDevtoolsPanel', () => {
65 TestBed.runInInjectionContext(() => {
66 injectDevtoolsPanel(() => ({
67 hostElement: TestBed.inject(ElementRef),
68 }))
69 })
70β
71 TestBed.flushEffects()
72β
73 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
74 })
75β
76 it('should destroy TanstackQueryDevtoolsPanel', () => {
77 const result = TestBed.runInInjectionContext(() => {
78 return injectDevtoolsPanel(() => ({
79 hostElement: TestBed.inject(ElementRef),
80 }))
81 })
82β
83 TestBed.flushEffects()
84β
85 result.destroy()
86β
87 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
88 })
89β
90 it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', () => {
91 const hostElement = signal<ElementRef>(mockElementRef)
92β
93 TestBed.runInInjectionContext(() => {
94 return injectDevtoolsPanel(() => ({
95 hostElement: hostElement(),
96 }))
97 })
98β
99 TestBed.flushEffects()
100β
101 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
102β
103 hostElement.set(null as unknown as ElementRef)
104β
105 TestBed.flushEffects()
106β
107 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
108 })
109β
110 it('should update client', () => {
111 const client = signal(new QueryClient())
112β
113 TestBed.runInInjectionContext(() => {
114 return injectDevtoolsPanel(() => ({
115 hostElement: TestBed.inject(ElementRef),
116 client: client(),
117 }))
118 })
119β
120 TestBed.flushEffects()
121β
122 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
123β
124 client.set(new QueryClient())
125β
126 TestBed.flushEffects()
127β
128 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
129 })
130β
131 it('should update error types', () => {
132 const errorTypes = signal([])
133β
134 TestBed.runInInjectionContext(() => {
135 return injectDevtoolsPanel(() => ({
136 hostElement: TestBed.inject(ElementRef),
137 errorTypes: errorTypes(),
138 }))
139 })
140β
141 TestBed.flushEffects()
142β
143 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
144β
145 errorTypes.set([])
146β
147 TestBed.flushEffects()
148β
149 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
150 })
151β
152 it('should update onclose', () => {
153 const functionA = () => {}
154 const functionB = () => {}
155β
156 const onClose = signal(functionA)
157β
158 TestBed.runInInjectionContext(() => {
159 return injectDevtoolsPanel(() => ({
160 hostElement: TestBed.inject(ElementRef),
161 onClose: onClose(),
162 }))
163 })
164β
165 TestBed.flushEffects()
166β
167 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
168β
169 onClose.set(functionB)
170β
171 TestBed.flushEffects()
172β
173 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
174 })
175})