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