1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { of } from 'rxjs';
4 import { OverviewStorageCardComponent } from './overview-storage-card.component';
5 import { PrometheusService } from '~/app/shared/api/prometheus.service';
6 import { FormatterService } from '~/app/shared/services/formatter.service';
8 describe('OverviewStorageCardComponent (Jest)', () => {
9 let component: OverviewStorageCardComponent;
10 let fixture: ComponentFixture<OverviewStorageCardComponent>;
12 let mockPrometheusService: {
13 getPrometheusQueryData: jest.Mock;
16 let mockFormatterService: {
17 formatToBinary: jest.Mock;
18 convertToUnit: jest.Mock;
21 const mockPrometheusResponse = {
24 metric: { application: 'Block' },
28 metric: { application: 'Filesystem' },
32 metric: { application: 'Object' },
33 value: [0, 0] // should be filtered
38 beforeEach(async () => {
39 mockPrometheusService = {
40 getPrometheusQueryData: jest.fn().mockReturnValue(of(mockPrometheusResponse))
43 mockFormatterService = {
44 formatToBinary: jest.fn().mockReturnValue([10, 'GiB']),
45 convertToUnit: jest.fn((value: number) => Number(value))
48 await TestBed.configureTestingModule({
49 imports: [OverviewStorageCardComponent],
51 { provide: PrometheusService, useValue: mockPrometheusService },
52 { provide: FormatterService, useValue: mockFormatterService }
54 }).compileComponents();
56 fixture = TestBed.createComponent(OverviewStorageCardComponent);
57 component = fixture.componentInstance;
58 fixture.detectChanges(); // triggers ngOnInit
65 // --------------------------------------------------
67 // --------------------------------------------------
69 it('should create', () => {
70 expect(component).toBeTruthy();
73 // --------------------------------------------------
74 // TOTAL setter (truthy)
75 // --------------------------------------------------
77 it('should set total when valid value provided', () => {
78 component.total = 1024;
80 expect(component.totalRaw).toBe(10);
81 expect(component.totalRawUnit).toBe('GiB');
84 // --------------------------------------------------
85 // TOTAL setter (falsy)
86 // --------------------------------------------------
88 it('should not set total when formatter returns NaN', () => {
89 mockFormatterService.formatToBinary.mockReturnValue([NaN, 'GiB']);
93 expect(component.totalRaw).toBeUndefined();
96 // --------------------------------------------------
98 // --------------------------------------------------
100 it('should set used correctly', () => {
101 component.used = 2048;
103 expect(component.usedRaw).toBe(10);
104 expect(component.usedRawUnit).toBe('GiB');
107 // --------------------------------------------------
109 // --------------------------------------------------
111 it('should switch to RAW when toggled true', () => {
112 component.toggleRawCapacity(true);
114 expect(component.isRawCapacity).toBe(true);
115 expect(component.selectedCapacityType).toBe('raw');
118 it('should switch to USED when toggled false', () => {
119 component.toggleRawCapacity(false);
121 expect(component.isRawCapacity).toBe(false);
122 expect(component.selectedCapacityType).toBe('used');
125 it('should call Prometheus again when toggled', () => {
126 component.toggleRawCapacity(false);
128 expect(mockPrometheusService.getPrometheusQueryData).toHaveBeenCalledTimes(2);
131 // --------------------------------------------------
132 // ngOnInit data load
133 // --------------------------------------------------
135 it('should load and filter data on init', () => {
136 expect(mockPrometheusService.getPrometheusQueryData).toHaveBeenCalled();
137 expect(component.allData.length).toBe(2); // Object filtered (0 value)
140 // --------------------------------------------------
142 // --------------------------------------------------
144 it('should filter displayData for selected storage type', () => {
145 component.allData = [
146 { group: 'Block', value: 10 },
147 { group: 'Filesystem', value: 20 }
150 component.selectedStorageType = 'Block';
151 (component as any).setChartData();
153 expect(component.displayData.length).toBe(1);
154 expect(component.displayData[0].group).toBe('Block');
157 it('should show all data when ALL selected', () => {
158 component.allData = [
159 { group: 'Block', value: 10 },
160 { group: 'Filesystem', value: 20 }
163 component.selectedStorageType = 'All';
164 (component as any).setChartData();
166 expect(component.displayData.length).toBe(2);
169 // --------------------------------------------------
171 // --------------------------------------------------
173 it('should update storage type from dropdown selection', () => {
174 component.onStorageTypeSelect({
175 item: { content: 'Block', selected: true }
178 expect(component.selectedStorageType).toBe('Block');
181 it('should auto-select single item if only one exists', () => {
182 component.allData = [{ group: 'Block', value: 10 }];
184 (component as any).setDropdownItemsAndStorageType();
186 expect(component.selectedStorageType).toBe('Block');
187 expect(component.dropdownItems.length).toBe(1);
190 it('should reset to ALL if previous selection missing', () => {
191 component.selectedStorageType = 'Block';
193 component.allData = [
194 { group: 'Filesystem', value: 20 },
195 { group: 'Object', value: 30 }
198 (component as any).setDropdownItemsAndStorageType();
200 expect(component.selectedStorageType).toBe('All');
203 // --------------------------------------------------
205 // --------------------------------------------------
207 it('should clean up on destroy', () => {
208 const nextSpy = jest.spyOn((component as any).destroy$, 'next');
209 const completeSpy = jest.spyOn((component as any).destroy$, 'complete');
211 component.ngOnDestroy();
213 expect(nextSpy).toHaveBeenCalled();
214 expect(completeSpy).toHaveBeenCalled();