]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
5a8e63011f6258fed66146ab6d823dc3150057ee
[ceph.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { of } from 'rxjs';
3
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';
7
8 describe('OverviewStorageCardComponent (Jest)', () => {
9   let component: OverviewStorageCardComponent;
10   let fixture: ComponentFixture<OverviewStorageCardComponent>;
11
12   let mockPrometheusService: {
13     getPrometheusQueryData: jest.Mock;
14   };
15
16   let mockFormatterService: {
17     formatToBinary: jest.Mock;
18     convertToUnit: jest.Mock;
19   };
20
21   const mockPrometheusResponse = {
22     result: [
23       {
24         metric: { application: 'Block' },
25         value: [0, 1024]
26       },
27       {
28         metric: { application: 'Filesystem' },
29         value: [0, 2048]
30       },
31       {
32         metric: { application: 'Object' },
33         value: [0, 0] // should be filtered
34       }
35     ]
36   };
37
38   beforeEach(async () => {
39     mockPrometheusService = {
40       getPrometheusQueryData: jest.fn().mockReturnValue(of(mockPrometheusResponse))
41     };
42
43     mockFormatterService = {
44       formatToBinary: jest.fn().mockReturnValue([10, 'GiB']),
45       convertToUnit: jest.fn((value: number) => Number(value))
46     };
47
48     await TestBed.configureTestingModule({
49       imports: [OverviewStorageCardComponent],
50       providers: [
51         { provide: PrometheusService, useValue: mockPrometheusService },
52         { provide: FormatterService, useValue: mockFormatterService }
53       ]
54     }).compileComponents();
55
56     fixture = TestBed.createComponent(OverviewStorageCardComponent);
57     component = fixture.componentInstance;
58     fixture.detectChanges(); // triggers ngOnInit
59   });
60
61   afterEach(() => {
62     jest.clearAllMocks();
63   });
64
65   // --------------------------------------------------
66   // CREATION
67   // --------------------------------------------------
68
69   it('should create', () => {
70     expect(component).toBeTruthy();
71   });
72
73   // --------------------------------------------------
74   // TOTAL setter (truthy)
75   // --------------------------------------------------
76
77   it('should set total when valid value provided', () => {
78     component.total = 1024;
79
80     expect(component.totalRaw).toBe(10);
81     expect(component.totalRawUnit).toBe('GiB');
82   });
83
84   // --------------------------------------------------
85   // TOTAL setter (falsy)
86   // --------------------------------------------------
87
88   it('should not set total when formatter returns NaN', () => {
89     mockFormatterService.formatToBinary.mockReturnValue([NaN, 'GiB']);
90
91     component.total = 0;
92
93     expect(component.totalRaw).toBeUndefined();
94   });
95
96   // --------------------------------------------------
97   // USED setter
98   // --------------------------------------------------
99
100   it('should set used correctly', () => {
101     component.used = 2048;
102
103     expect(component.usedRaw).toBe(10);
104     expect(component.usedRawUnit).toBe('GiB');
105   });
106
107   // --------------------------------------------------
108   // TOGGLE
109   // --------------------------------------------------
110
111   it('should switch to RAW when toggled true', () => {
112     component.toggleRawCapacity(true);
113
114     expect(component.isRawCapacity).toBe(true);
115     expect(component.selectedCapacityType).toBe('raw');
116   });
117
118   it('should switch to USED when toggled false', () => {
119     component.toggleRawCapacity(false);
120
121     expect(component.isRawCapacity).toBe(false);
122     expect(component.selectedCapacityType).toBe('used');
123   });
124
125   it('should call Prometheus again when toggled', () => {
126     component.toggleRawCapacity(false);
127
128     expect(mockPrometheusService.getPrometheusQueryData).toHaveBeenCalledTimes(2);
129   });
130
131   // --------------------------------------------------
132   // ngOnInit data load
133   // --------------------------------------------------
134
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)
138   });
139
140   // --------------------------------------------------
141   // FILTERING
142   // --------------------------------------------------
143
144   it('should filter displayData for selected storage type', () => {
145     component.allData = [
146       { group: 'Block', value: 10 },
147       { group: 'Filesystem', value: 20 }
148     ];
149
150     component.selectedStorageType = 'Block';
151     (component as any).setChartData();
152
153     expect(component.displayData.length).toBe(1);
154     expect(component.displayData[0].group).toBe('Block');
155   });
156
157   it('should show all data when ALL selected', () => {
158     component.allData = [
159       { group: 'Block', value: 10 },
160       { group: 'Filesystem', value: 20 }
161     ];
162
163     component.selectedStorageType = 'All';
164     (component as any).setChartData();
165
166     expect(component.displayData.length).toBe(2);
167   });
168
169   // --------------------------------------------------
170   // DROPDOWN
171   // --------------------------------------------------
172
173   it('should update storage type from dropdown selection', () => {
174     component.onStorageTypeSelect({
175       item: { content: 'Block', selected: true }
176     });
177
178     expect(component.selectedStorageType).toBe('Block');
179   });
180
181   it('should auto-select single item if only one exists', () => {
182     component.allData = [{ group: 'Block', value: 10 }];
183
184     (component as any).setDropdownItemsAndStorageType();
185
186     expect(component.selectedStorageType).toBe('Block');
187     expect(component.dropdownItems.length).toBe(1);
188   });
189
190   it('should reset to ALL if previous selection missing', () => {
191     component.selectedStorageType = 'Block';
192
193     component.allData = [
194       { group: 'Filesystem', value: 20 },
195       { group: 'Object', value: 30 }
196     ];
197
198     (component as any).setDropdownItemsAndStorageType();
199
200     expect(component.selectedStorageType).toBe('All');
201   });
202
203   // --------------------------------------------------
204   // DESTROY
205   // --------------------------------------------------
206
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');
210
211     component.ngOnDestroy();
212
213     expect(nextSpy).toHaveBeenCalled();
214     expect(completeSpy).toHaveBeenCalled();
215   });
216 });