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