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';
15 } from '../../../../../testing/unit-test-helper';
16 import { OrchestratorService } from '../../../../shared/api/orchestrator.service';
17 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
18 import { SummaryService } from '../../../../shared/services/summary.service';
19 import { SharedModule } from '../../../../shared/shared.module';
20 import { InventoryDevice } from '../../inventory/inventory-devices/inventory-device.model';
21 import { InventoryDevicesComponent } from '../../inventory/inventory-devices/inventory-devices.component';
22 import { DevicesSelectionChangeEvent } from '../osd-devices-selection-groups/devices-selection-change-event.interface';
23 import { DevicesSelectionClearEvent } from '../osd-devices-selection-groups/devices-selection-clear-event.interface';
24 import { OsdDevicesSelectionGroupsComponent } from '../osd-devices-selection-groups/osd-devices-selection-groups.component';
25 import { OsdFormComponent } from './osd-form.component';
27 describe('OsdFormComponent', () => {
28 let form: CdFormGroup;
29 let component: OsdFormComponent;
30 let formHelper: FormHelper;
31 let fixture: ComponentFixture<OsdFormComponent>;
32 let fixtureHelper: FixtureHelper;
33 let orchService: OrchestratorService;
34 let summaryService: SummaryService;
35 const devices: InventoryDevice[] = [
46 human_readable_size: '1 KB'
49 rejected_reasons: [''],
50 device_id: 'VENDOR-MODEL-ID',
51 human_readable_type: 'nvme/ssd',
56 const expectPreviewButton = (enabled: boolean) => {
57 const debugElement = fixtureHelper.getElementByCss('.card-footer button');
58 expect(debugElement.nativeElement.disabled).toBe(!enabled);
61 const selectDevices = (type: string) => {
62 const event: DevicesSelectionChangeEvent = {
68 component.onDevicesSelected(event);
69 if (type === 'data') {
70 component.dataDeviceSelectionGroups.devices = devices;
71 } else if (type === 'wal') {
72 component.walDeviceSelectionGroups.devices = devices;
73 } else if (type === 'db') {
74 component.dbDeviceSelectionGroups.devices = devices;
76 fixture.detectChanges();
79 const clearDevices = (type: string) => {
80 const event: DevicesSelectionClearEvent = {
84 component.onDevicesCleared(event);
85 fixture.detectChanges();
88 const features = ['encrypted'];
89 const checkFeatures = (enabled: boolean) => {
90 for (const feature of features) {
91 const element = fixtureHelper.getElementByCss(`#${feature}`).nativeElement;
92 expect(element.disabled).toBe(!enabled);
93 expect(element.checked).toBe(false);
99 BrowserAnimationsModule,
100 HttpClientTestingModule,
105 ToastrModule.forRoot()
107 providers: [i18nProviders],
108 declarations: [OsdFormComponent, OsdDevicesSelectionGroupsComponent, InventoryDevicesComponent]
112 fixture = TestBed.createComponent(OsdFormComponent);
113 fixtureHelper = new FixtureHelper(fixture);
114 component = fixture.componentInstance;
115 form = component.form;
116 formHelper = new FormHelper(form);
117 orchService = TestBed.get(OrchestratorService);
118 summaryService = TestBed.get(SummaryService);
119 summaryService['summaryDataSource'] = new BehaviorSubject(null);
120 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
121 summaryService['summaryDataSource'].next({ version: 'master' });
124 it('should create', () => {
125 expect(component).toBeTruthy();
128 describe('without orchestrator', () => {
130 spyOn(orchService, 'status').and.returnValue(of({ available: false }));
131 spyOn(orchService, 'inventoryDeviceList').and.callThrough();
132 fixture.detectChanges();
135 it('should display info panel to document', () => {
136 fixtureHelper.expectElementVisible('cd-alert-panel', true);
137 fixtureHelper.expectElementVisible('.col-sm-10 form', false);
140 it('should not call inventoryDeviceList', () => {
141 expect(orchService.inventoryDeviceList).not.toHaveBeenCalled();
145 describe('with orchestrator', () => {
147 spyOn(orchService, 'status').and.returnValue(of({ available: true }));
148 spyOn(orchService, 'inventoryDeviceList').and.returnValue(of([]));
149 fixture.detectChanges();
152 it('should display form', () => {
153 fixtureHelper.expectElementVisible('cd-alert-panel', false);
154 fixtureHelper.expectElementVisible('.cd-col-form form', true);
157 describe('without data devices selected', () => {
158 it('should disable preview button', () => {
159 expectPreviewButton(false);
162 it('should not display shared devices slots', () => {
163 fixtureHelper.expectElementVisible('#walSlots', false);
164 fixtureHelper.expectElementVisible('#dbSlots', false);
167 it('should disable the checkboxes', () => {
168 checkFeatures(false);
172 describe('with data devices selected', () => {
174 selectDevices('data');
177 it('should enable preview button', () => {
178 expectPreviewButton(true);
181 it('should not display shared devices slots', () => {
182 fixtureHelper.expectElementVisible('#walSlots', false);
183 fixtureHelper.expectElementVisible('#dbSlots', false);
186 it('should enable the checkboxes', () => {
190 it('should disable the checkboxes after clearing data devices', () => {
191 clearDevices('data');
192 checkFeatures(false);
195 describe('with shared devices selected', () => {
197 selectDevices('wal');
201 it('should display slots', () => {
202 fixtureHelper.expectElementVisible('#walSlots', true);
203 fixtureHelper.expectElementVisible('#dbSlots', true);
206 it('validate slots', () => {
207 for (const control of ['walSlots', 'dbSlots']) {
208 formHelper.expectValid(control);
209 formHelper.expectValidChange(control, 1);
210 formHelper.expectErrorChange(control, -1, 'min');
214 describe('test clearing data devices', () => {
216 clearDevices('data');
219 it('should not display shared devices slots and should disable checkboxes', () => {
220 fixtureHelper.expectElementVisible('#walSlots', false);
221 fixtureHelper.expectElementVisible('#dbSlots', false);
222 checkFeatures(false);