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