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