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

inject-devtools-panel.test.ts

273 lines7.9 KB

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