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