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

inject-devtools-panel.test.ts

175 lines4.2 KB

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