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

inject-devtools-panel.test.ts

228 lines6.5 KB

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']>(),
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})