Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-devtools-panel.test.ts

301 lines9.0 KB

1import {
2 ApplicationRef,
3 ElementRef,
4 provideZonelessChangeDetection,
5 signal,
6} from '@angular/core'
7import { TestBed } from '@angular/core/testing'
8import { QueryClient } from '@tanstack/angular-query'
9import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
10import { injectDevtoolsPanel } from '../devtools-panel'
11import { setupTanStackQueryTestBed } from './test-utils'
12import type {
13 TanstackQueryDevtoolsPanel,
14 Theme,
15} from '@tanstack/query-devtools'
16​
17const mockDevtoolsPanelInstance = {
18 mount: vi.fn<TanstackQueryDevtoolsPanel['mount']>(),
19 unmount: vi.fn<TanstackQueryDevtoolsPanel['unmount']>(),
20 setClient: vi.fn<TanstackQueryDevtoolsPanel['setClient']>(),
21 setErrorTypes: vi.fn<TanstackQueryDevtoolsPanel['setErrorTypes']>(),
22 setOnClose: vi.fn<TanstackQueryDevtoolsPanel['setOnClose']>(),
23 setTheme: vi.fn<TanstackQueryDevtoolsPanel['setTheme']>(),
25​
26const mocks = vi.hoisted(() => {
27 function MockTanstackQueryDevtoolsPanel() {
28 return mockDevtoolsPanelInstance
29 }
30​
31 return {
32 mockTanstackQueryDevtoolsPanel: vi.fn<
33 typeof MockTanstackQueryDevtoolsPanel
34 >(MockTanstackQueryDevtoolsPanel),
35 }
36})
37​
38// oxlint-disable-next-line vitest/prefer-import-in-mock -- The mock cannot implement the devtools class's private fields.
39vi.mock('@tanstack/query-devtools', () => ({
40 TanstackQueryDevtoolsPanel: mocks.mockTanstackQueryDevtoolsPanel,
41}))
42​
43describe('injectDevtoolsPanel', () => {
44 let queryClient: QueryClient
45 let mockElementRef: ElementRef
46​
47 const waitForDevtoolsToBeCreated = async () => {
48 await vi.waitFor(() => {
49 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
50 })
51 }
52​
53 beforeEach(() => {
54 vi.clearAllMocks()
55 queryClient = new QueryClient()
56 mockElementRef = new ElementRef(document.createElement('div'))
57 setupTanStackQueryTestBed(queryClient, {
58 providers: [{ provide: ElementRef, useValue: signal(mockElementRef) }],
59 })
60 })
61​
62 afterEach(() => {
63 vi.restoreAllMocks()
64 })
65​
66 it('should return a DevtoolsPanelRef', () => {
67 const result = TestBed.runInInjectionContext(() => {
68 return injectDevtoolsPanel(() => ({
69 hostElement: TestBed.inject(ElementRef),
70 }))
71 })
72​
73 expect(result).toStrictEqual({
74 destroy: expect.any(Function),
75 })
76 })
77​
78 it("should throw 'No QueryClient found' when no 'QueryClient' is provided", () => {
79 TestBed.resetTestingModule()
80 TestBed.configureTestingModule({
81 providers: [provideZonelessChangeDetection()],
82 })
83​
84 TestBed.runInInjectionContext(() => {
85 injectDevtoolsPanel(() => ({
86 hostElement: mockElementRef,
87 }))
88 })
89​
90 expect(() => TestBed.tick()).toThrow('No QueryClient found')
91 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
92 })
93​
94 it('should initialize TanstackQueryDevtoolsPanel', async () => {
95 TestBed.runInInjectionContext(() => {
96 injectDevtoolsPanel(() => ({
97 hostElement: TestBed.inject(ElementRef),
98 hideDisabledQueries: true,
99 }))
100 })
101​
102 await TestBed.inject(ApplicationRef).whenStable()
103​
104 await waitForDevtoolsToBeCreated()
105​
106 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
107 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
108 expect.objectContaining({ theme: undefined, hideDisabledQueries: true }),
109 )
110 })
111​
112 it('should update the theme and restore the saved preference when it is removed', async () => {
113 const theme = signal<Theme | undefined>('dark')
114​
115 TestBed.runInInjectionContext(() => {
116 injectDevtoolsPanel(() => ({
117 hostElement: mockElementRef,
118 theme: theme(),
119 }))
120 })
121​
122 await TestBed.inject(ApplicationRef).whenStable()
123 await waitForDevtoolsToBeCreated()
124​
125 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
126 expect.objectContaining({ theme: 'dark' }),
127 )
128 expect(mockDevtoolsPanelInstance.setTheme).not.toHaveBeenCalled()
129​
130 theme.set('light')
131 await TestBed.inject(ApplicationRef).whenStable()
132 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenLastCalledWith('light')
133​
134 theme.set(undefined)
135 await TestBed.inject(ApplicationRef).whenStable()
136 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenLastCalledWith(
137 undefined,
138 )
139 expect(mockDevtoolsPanelInstance.setTheme).toHaveBeenCalledTimes(2)
140 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
141 })
142​
143 it('should destroy TanstackQueryDevtoolsPanel', async () => {
144 const result = TestBed.runInInjectionContext(() => {
145 return injectDevtoolsPanel(() => ({
146 hostElement: TestBed.inject(ElementRef),
147 }))
148 })
149​
150 await TestBed.inject(ApplicationRef).whenStable()
151​
152 await waitForDevtoolsToBeCreated()
153​
154 result.destroy()
155​
156 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
157 })
158​
159 it('should stop reacting after it is manually destroyed', async () => {
160 const hostElement = signal<ElementRef | undefined>(mockElementRef)
161 const result = TestBed.runInInjectionContext(() =>
162 injectDevtoolsPanel(() => ({
163 hostElement: hostElement(),
164 })),
165 )
166​
167 await TestBed.inject(ApplicationRef).whenStable()
168 await waitForDevtoolsToBeCreated()
169​
170 result.destroy()
171 hostElement.set(undefined)
172 hostElement.set(mockElementRef)
173 await TestBed.inject(ApplicationRef).whenStable()
174​
175 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
176 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
177 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
178 })
179​
180 it('should not mount when destroyed while the devtools import is pending', async () => {
181 const result = TestBed.runInInjectionContext(() =>
182 injectDevtoolsPanel(() => ({
183 hostElement: mockElementRef,
184 })),
185 )
186​
187 TestBed.tick()
188 result.destroy()
189 await TestBed.inject(ApplicationRef).whenStable()
190​
191 expect(mocks.mockTanstackQueryDevtoolsPanel).not.toHaveBeenCalled()
192 expect(mockDevtoolsPanelInstance.mount).not.toHaveBeenCalled()
193 })
194​
195 it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', async () => {
196 const hostElement = signal<ElementRef | null>(mockElementRef)
197​
198 TestBed.runInInjectionContext(() => {
199 return injectDevtoolsPanel(() => ({
200 hostElement: hostElement(),
201 }))
202 })
203​
204 await TestBed.inject(ApplicationRef).whenStable()
205​
206 await waitForDevtoolsToBeCreated()
207​
208 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
209​
210 hostElement.set(null)
211​
212 await TestBed.inject(ApplicationRef).whenStable()
213​
214 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
215 })
216​
217 it('should update client', async () => {
218 const client = signal(new QueryClient())
219​
220 TestBed.runInInjectionContext(() => {
221 return injectDevtoolsPanel(() => ({
222 hostElement: TestBed.inject(ElementRef),
223 client: client(),
224 }))
225 })
226​
227 await TestBed.inject(ApplicationRef).whenStable()
228​
229 await waitForDevtoolsToBeCreated()
230​
231 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
232​
233 client.set(new QueryClient())
234​
235 await TestBed.inject(ApplicationRef).whenStable()
236​
237 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
238 })
239​
240 it('should update error types', async () => {
241 const errorTypes = signal([])
242​
243 TestBed.runInInjectionContext(() => {
244 return injectDevtoolsPanel(() => ({
245 hostElement: TestBed.inject(ElementRef),
246 errorTypes: errorTypes(),
247 }))
248 })
249​
250 await TestBed.inject(ApplicationRef).whenStable()
251​
252 await waitForDevtoolsToBeCreated()
253​
254 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
255​
256 errorTypes.set([])
257​
258 await TestBed.inject(ApplicationRef).whenStable()
259​
260 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
261 })
262​
263 it('should update onClose and clear it when removed', async () => {
264 const functionA = vi.fn<() => void>()
265 const functionB = vi.fn<() => void>()
266​
267 const onClose = signal<typeof functionA | undefined>(functionA)
268​
269 TestBed.runInInjectionContext(() => {
270 return injectDevtoolsPanel(() => ({
271 hostElement: TestBed.inject(ElementRef),
272 onClose: onClose(),
273 }))
274 })
275​
276 await TestBed.inject(ApplicationRef).whenStable()
277​
278 await waitForDevtoolsToBeCreated()
279​
280 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
281​
282 onClose.set(functionB)
283​
284 await TestBed.inject(ApplicationRef).whenStable()
285​
286 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
287 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenLastCalledWith(
288 functionB,
289 )
290​
291 onClose.set(undefined)
292 await TestBed.inject(ApplicationRef).whenStable()
293​
294 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(2)
295 const resetOnClose = mockDevtoolsPanelInstance.setOnClose.mock.lastCall?.[0]
296 expect(resetOnClose).toBeTypeOf('function')
297 resetOnClose?.()
298 expect(functionA).not.toHaveBeenCalled()
299 expect(functionB).not.toHaveBeenCalled()
300 })
301})