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