1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { FormsModule, ReactiveFormsModule } from '@angular/forms';
4 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { ToastrModule } from 'ngx-toastr';
8 import { BehaviorSubject, of } from 'rxjs';
10 import { InventoryDevice } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-device.model';
11 import { InventoryDevicesComponent } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-devices.component';
12 import { HostService } from '~/app/shared/api/host.service';
13 import { OrchestratorService } from '~/app/shared/api/orchestrator.service';
14 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
15 import { SummaryService } from '~/app/shared/services/summary.service';
16 import { SharedModule } from '~/app/shared/shared.module';
17 import { configureTestBed, FixtureHelper, FormHelper } from '~/testing/unit-test-helper';
18 import { DevicesSelectionChangeEvent } from '../osd-devices-selection-groups/devices-selection-change-event.interface';
19 import { DevicesSelectionClearEvent } from '../osd-devices-selection-groups/devices-selection-clear-event.interface';
20 import { OsdDevicesSelectionGroupsComponent } from '../osd-devices-selection-groups/osd-devices-selection-groups.component';
21 import { OsdFormComponent } from './osd-form.component';
23 describe('OsdFormComponent', () => {
24 let form: CdFormGroup;
25 let component: OsdFormComponent;
26 let formHelper: FormHelper;
27 let fixture: ComponentFixture<OsdFormComponent>;
28 let fixtureHelper: FixtureHelper;
29 let orchService: OrchestratorService;
30 let hostService: HostService;
31 let summaryService: SummaryService;
32 const devices: InventoryDevice[] = [
43 human_readable_size: '1 KB'
46 rejected_reasons: [''],
47 device_id: 'VENDOR-MODEL-ID',
48 human_readable_type: 'nvme/ssd',
53 const expectPreviewButton = (enabled: boolean) => {
54 const debugElement = fixtureHelper.getElementByCss('.tc_submitButton');
55 expect(debugElement.nativeElement.disabled).toBe(!enabled);
58 const selectDevices = (type: string) => {
59 const event: DevicesSelectionChangeEvent = {
65 component.onDevicesSelected(event);
66 if (type === 'data') {
67 component.dataDeviceSelectionGroups.devices = devices;
68 } else if (type === 'wal') {
69 component.walDeviceSelectionGroups.devices = devices;
70 } else if (type === 'db') {
71 component.dbDeviceSelectionGroups.devices = devices;
73 fixture.detectChanges();
76 const clearDevices = (type: string) => {
77 const event: DevicesSelectionClearEvent = {
81 component.onDevicesCleared(event);
82 fixture.detectChanges();
85 const features = ['encrypted'];
86 const checkFeatures = (enabled: boolean) => {
87 for (const feature of features) {
88 const element = fixtureHelper.getElementByCss(`#${feature}`).nativeElement;
89 expect(element.disabled).toBe(!enabled);
90 expect(element.checked).toBe(false);
96 BrowserAnimationsModule,
97 HttpClientTestingModule,
102 ToastrModule.forRoot()
104 declarations: [OsdFormComponent, OsdDevicesSelectionGroupsComponent, InventoryDevicesComponent]
108 fixture = TestBed.createComponent(OsdFormComponent);
109 fixtureHelper = new FixtureHelper(fixture);
110 component = fixture.componentInstance;
111 form = component.form;
112 formHelper = new FormHelper(form);
113 orchService = TestBed.inject(OrchestratorService);
114 hostService = TestBed.inject(HostService);
115 summaryService = TestBed.inject(SummaryService);
116 summaryService['summaryDataSource'] = new BehaviorSubject(null);
117 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
118 summaryService['summaryDataSource'].next({ version: 'master' });
121 it('should create', () => {
122 expect(component).toBeTruthy();
125 describe('without orchestrator', () => {
127 spyOn(orchService, 'status').and.returnValue(of({ available: false }));
128 spyOn(hostService, 'inventoryDeviceList').and.callThrough();
129 fixture.detectChanges();
132 it('should display info panel to document', () => {
133 fixtureHelper.expectElementVisible('cd-alert-panel', true);
134 fixtureHelper.expectElementVisible('.col-sm-10 form', false);
137 it('should not call inventoryDeviceList', () => {
138 expect(hostService.inventoryDeviceList).not.toHaveBeenCalled();
142 describe('with orchestrator', () => {
144 spyOn(orchService, 'status').and.returnValue(of({ available: true }));
145 spyOn(hostService, 'inventoryDeviceList').and.returnValue(of([]));
146 fixture.detectChanges();
149 it('should display form', () => {
150 fixtureHelper.expectElementVisible('cd-alert-panel', false);
151 fixtureHelper.expectElementVisible('.cd-col-form form', true);
154 describe('without data devices selected', () => {
155 it('should disable preview button', () => {
156 expectPreviewButton(false);
159 it('should not display shared devices slots', () => {
160 fixtureHelper.expectElementVisible('#walSlots', false);
161 fixtureHelper.expectElementVisible('#dbSlots', false);
164 it('should disable the checkboxes', () => {
165 checkFeatures(false);
169 describe('with data devices selected', () => {
171 selectDevices('data');
174 it('should enable preview button', () => {
175 expectPreviewButton(true);
178 it('should not display shared devices slots', () => {
179 fixtureHelper.expectElementVisible('#walSlots', false);
180 fixtureHelper.expectElementVisible('#dbSlots', false);
183 it('should enable the checkboxes', () => {
187 it('should disable the checkboxes after clearing data devices', () => {
188 clearDevices('data');
189 checkFeatures(false);
192 describe('with shared devices selected', () => {
194 selectDevices('wal');
198 it('should display slots', () => {
199 fixtureHelper.expectElementVisible('#walSlots', true);
200 fixtureHelper.expectElementVisible('#dbSlots', true);
203 it('validate slots', () => {
204 for (const control of ['walSlots', 'dbSlots']) {
205 formHelper.expectValid(control);
206 formHelper.expectValidChange(control, 1);
207 formHelper.expectErrorChange(control, -1, 'min');
211 describe('test clearing data devices', () => {
213 clearDevices('data');
216 it('should not display shared devices slots and should disable checkboxes', () => {
217 fixtureHelper.expectElementVisible('#walSlots', false);
218 fixtureHelper.expectElementVisible('#dbSlots', false);
219 checkFeatures(false);