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

inject-devtools-panel.test.ts

188 lines4.6 KB

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