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'
11import type {
12 TanstackQueryDevtoolsPanel,
13 Theme,
14} from '@tanstack/query-devtools'
15β
16const mockDevtoolsPanelInstance = {
17 mount: vi.fn<TanstackQueryDevtoolsPanel['mount']>(),
18 unmount: vi.fn<TanstackQueryDevtoolsPanel['unmount']>(),
19 setClient: vi.fn<TanstackQueryDevtoolsPanel['setClient']>(),
20 setErrorTypes: vi.fn<TanstackQueryDevtoolsPanel['setErrorTypes']>(),
21 setOnClose: vi.fn<TanstackQueryDevtoolsPanel['setOnClose']>(),
22 setTheme: vi.fn<TanstackQueryDevtoolsPanel['setTheme']>(),
23}
24β
25const mocks = vi.hoisted(() => {
26 function MockTanstackQueryDevtoolsPanel() {
27 return mockDevtoolsPanelInstance
28 }
29β
30 return {
31 mockTanstackQueryDevtoolsPanel: vi.fn<
32 typeof MockTanstackQueryDevtoolsPanel
33 >(MockTanstackQueryDevtoolsPanel),
34 }
35})
36β
37// oxlint-disable-next-line vitest/prefer-import-in-mock -- the mocked devtools classes have '#private' fields, and this environment cannot load the real Solid module
38vi.mock('@tanstack/query-devtools', () => ({
39 TanstackQueryDevtoolsPanel: mocks.mockTanstackQueryDevtoolsPanel,
40}))
41β
42describe('injectDevtoolsPanel', () => {
43 let queryClient: QueryClient
44 let mockElementRef: ElementRef
45β
46 const waitForDevtoolsToBeCreated = async () => {
47 await vi.waitFor(() => {
48 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
49 })
50 }
51β
52 beforeEach(() => {
53 vi.clearAllMocks()
54 queryClient = new QueryClient()
55 mockElementRef = new ElementRef(document.createElement('div'))
56 TestBed.configureTestingModule({
57 providers: [
58 provideZonelessChangeDetection(),
59 provideTanStackQuery(queryClient),
60 { provide: ElementRef, useValue: signal(mockElementRef) },
61 ],
62 })
63 })
64β
65 afterEach(() => {
66 vi.restoreAllMocks()
67 })
68β
69 it('should return a DevtoolsPanelRef', () => {
70 const result = TestBed.runInInjectionContext(() => {
71 return injectDevtoolsPanel(() => ({
72 hostElement: TestBed.inject(ElementRef),
73 }))
74 })
75β
76 expect(result).toStrictEqual({
77 destroy: expect.any(Function),
78 })
79 })
80β
81 it('should initialize TanstackQueryDevtoolsPanel', async () => {
82 TestBed.runInInjectionContext(() => {
83 injectDevtoolsPanel(() => ({
84 hostElement: TestBed.inject(ElementRef),
85 }))
86 })
87β
88 TestBed.tick()
89 await waitForDevtoolsToBeCreated()
90 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
91 })
92β
93 it('should destroy TanstackQueryDevtoolsPanel', async () => {
94 const result = TestBed.runInInjectionContext(() => {
95 return injectDevtoolsPanel(() => ({
96 hostElement: TestBed.inject(ElementRef),
97 }))
98 })
99β
100 TestBed.tick()
101 await waitForDevtoolsToBeCreated()
102β
103 result.destroy()
104β
105 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
106 })
107β
108 it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', async () => {
109 const hostElement = signal<ElementRef>(mockElementRef)
110β
111 TestBed.runInInjectionContext(() => {
112 return injectDevtoolsPanel(() => ({
113 hostElement: hostElement(),
114 }))
115 })
116β
117 TestBed.tick()
118 await waitForDevtoolsToBeCreated()
119 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
120β
121 hostElement.set(null as unknown as ElementRef)
122 TestBed.tick()
123 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
124 })
125β
126 it('should update client', async () => {
127 const client = signal(new QueryClient())
128β
129 TestBed.runInInjectionContext(() => {
130 return injectDevtoolsPanel(() => ({
131 hostElement: TestBed.inject(ElementRef),
132 client: client(),
133 }))
134 })
135β
136 TestBed.tick()
137 await waitForDevtoolsToBeCreated()
138 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
139β
140 client.set(new QueryClient())
141 TestBed.tick()
142 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
143 })
144β
145 it('should update error types', async () => {
146 const errorTypes = signal([])
147β
148 TestBed.runInInjectionContext(() => {
149 return injectDevtoolsPanel(() => ({
150 hostElement: TestBed.inject(ElementRef),
151 errorTypes: errorTypes(),
152 }))
153 })
154β
155 TestBed.tick()
156 await waitForDevtoolsToBeCreated()
157 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
158β
159 errorTypes.set([])
160 TestBed.tick()
161 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
162 })
163β
164 it('should update onclose', async () => {
165 const functionA = () => {}
166 const functionB = () => {}
167β
168 const onClose = signal(functionA)
169β
170 TestBed.runInInjectionContext(() => {
171 return injectDevtoolsPanel(() => ({
172 hostElement: TestBed.inject(ElementRef),
173 onClose: onClose(),
174 }))
175 })
176β
177 TestBed.tick()
178 await waitForDevtoolsToBeCreated()
179 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
180β
181 onClose.set(functionB)
182 TestBed.tick()
183 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
184 })
185β
186 it('should pass hideDisabledQueries and theme to TanstackQueryDevtoolsPanel', async () => {
187 TestBed.runInInjectionContext(() => {
188 injectDevtoolsPanel(() => ({
189 hostElement: TestBed.inject(ElementRef),
190 hideDisabledQueries: true,
191 theme: 'dark',
192 }))
193 })
194β
195 TestBed.tick()
196 await waitForDevtoolsToBeCreated()
197 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
198 expect.objectContaining({ hideDisabledQueries: true, theme: 'dark' }),
199 )
200 })
201β
202 it('should update theme', async () => {
203 const theme = signal<Theme | undefined>('light')
204β
205 TestBed.runInInjectionContext(() => {
206 return injectDevtoolsPanel(() => ({
207 hostElement: TestBed.inject(ElementRef),
208 theme: theme(),
209 }))
210 })
211β
212 TestBed.tick()
213 await waitForDevtoolsToBeCreated()
214 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(0)
215β
216 theme.set('dark')
217 TestBed.tick()
218 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(1)
219 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledWith('dark')
220β
221 theme.set(undefined)
222 TestBed.tick()
223 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(2)
224 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenLastCalledWith(
225 undefined,
226 )
227 })
228})