]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
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 { RouterTestingModule } from '@angular/router/testing'; | |
5 | ||
6 | import { ToastrModule } from 'ngx-toastr'; | |
7 | import { BehaviorSubject, of } from 'rxjs'; | |
8 | ||
9 | import { | |
10 | configureTestBed, | |
11 | FixtureHelper, | |
12 | FormHelper, | |
13 | i18nProviders | |
14 | } from '../../../../../testing/unit-test-helper'; | |
15 | import { OrchestratorService } from '../../../../shared/api/orchestrator.service'; | |
16 | import { CdFormGroup } from '../../../../shared/forms/cd-form-group'; | |
17 | import { SummaryService } from '../../../../shared/services/summary.service'; | |
18 | import { SharedModule } from '../../../../shared/shared.module'; | |
19 | import { InventoryDevice } from '../../inventory/inventory-devices/inventory-device.model'; | |
20 | import { InventoryDevicesComponent } from '../../inventory/inventory-devices/inventory-devices.component'; | |
21 | import { DevicesSelectionChangeEvent } from '../osd-devices-selection-groups/devices-selection-change-event.interface'; | |
22 | import { DevicesSelectionClearEvent } from '../osd-devices-selection-groups/devices-selection-clear-event.interface'; | |
23 | import { OsdDevicesSelectionGroupsComponent } from '../osd-devices-selection-groups/osd-devices-selection-groups.component'; | |
24 | import { OsdFormComponent } from './osd-form.component'; | |
25 | ||
26 | describe('OsdFormComponent', () => { | |
27 | let form: CdFormGroup; | |
28 | let component: OsdFormComponent; | |
29 | let formHelper: FormHelper; | |
30 | let fixture: ComponentFixture<OsdFormComponent>; | |
31 | let fixtureHelper: FixtureHelper; | |
32 | let orchService: OrchestratorService; | |
33 | let summaryService: SummaryService; | |
34 | const devices: InventoryDevice[] = [ | |
35 | { | |
36 | hostname: 'node0', | |
37 | uid: '1', | |
38 | ||
39 | path: '/dev/sda', | |
40 | sys_api: { | |
41 | vendor: 'VENDOR', | |
42 | model: 'MODEL', | |
43 | size: 1024, | |
44 | rotational: 'false', | |
45 | human_readable_size: '1 KB' | |
46 | }, | |
47 | available: true, | |
48 | rejected_reasons: [''], | |
49 | device_id: 'VENDOR-MODEL-ID', | |
50 | human_readable_type: 'nvme/ssd', | |
51 | osd_ids: [] | |
52 | } | |
53 | ]; | |
54 | ||
55 | const expectPreviewButton = (enabled: boolean) => { | |
56 | const debugElement = fixtureHelper.getElementByCss('.card-footer button'); | |
57 | expect(debugElement.nativeElement.disabled).toBe(!enabled); | |
58 | }; | |
59 | ||
60 | const selectDevices = (type: string) => { | |
61 | const event: DevicesSelectionChangeEvent = { | |
62 | type: type, | |
63 | filters: [], | |
64 | data: devices, | |
65 | dataOut: [] | |
66 | }; | |
67 | component.onDevicesSelected(event); | |
68 | if (type === 'data') { | |
69 | component.dataDeviceSelectionGroups.devices = devices; | |
70 | } else if (type === 'wal') { | |
71 | component.walDeviceSelectionGroups.devices = devices; | |
72 | } else if (type === 'db') { | |
73 | component.dbDeviceSelectionGroups.devices = devices; | |
74 | } | |
75 | fixture.detectChanges(); | |
76 | }; | |
77 | ||
78 | const clearDevices = (type: string) => { | |
79 | const event: DevicesSelectionClearEvent = { | |
80 | type: type, | |
81 | clearedDevices: [] | |
82 | }; | |
83 | component.onDevicesCleared(event); | |
84 | fixture.detectChanges(); | |
85 | }; | |
86 | ||
87 | const features = ['encrypted']; | |
88 | const checkFeatures = (enabled: boolean) => { | |
89 | for (const feature of features) { | |
90 | const element = fixtureHelper.getElementByCss(`#${feature}`).nativeElement; | |
91 | expect(element.disabled).toBe(!enabled); | |
92 | expect(element.checked).toBe(false); | |
93 | } | |
94 | }; | |
95 | ||
96 | configureTestBed({ | |
97 | imports: [ | |
98 | HttpClientTestingModule, | |
99 | FormsModule, | |
100 | SharedModule, | |
101 | RouterTestingModule, | |
102 | ReactiveFormsModule, | |
103 | ToastrModule.forRoot() | |
104 | ], | |
105 | providers: [i18nProviders], | |
106 | declarations: [OsdFormComponent, OsdDevicesSelectionGroupsComponent, InventoryDevicesComponent] | |
107 | }); | |
108 | ||
109 | beforeEach(() => { | |
110 | fixture = TestBed.createComponent(OsdFormComponent); | |
111 | fixtureHelper = new FixtureHelper(fixture); | |
112 | component = fixture.componentInstance; | |
113 | form = component.form; | |
114 | formHelper = new FormHelper(form); | |
115 | orchService = TestBed.get(OrchestratorService); | |
116 | summaryService = TestBed.get(SummaryService); | |
117 | summaryService['summaryDataSource'] = new BehaviorSubject(null); | |
118 | summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable(); | |
119 | summaryService['summaryDataSource'].next({ version: 'master' }); | |
120 | }); | |
121 | ||
122 | it('should create', () => { | |
123 | expect(component).toBeTruthy(); | |
124 | }); | |
125 | ||
126 | describe('without orchestrator', () => { | |
127 | beforeEach(() => { | |
128 | spyOn(orchService, 'status').and.returnValue(of({ available: false })); | |
129 | spyOn(orchService, 'inventoryDeviceList').and.callThrough(); | |
130 | fixture.detectChanges(); | |
131 | }); | |
132 | ||
133 | it('should display info panel to document', () => { | |
134 | fixtureHelper.expectElementVisible('cd-alert-panel', true); | |
135 | fixtureHelper.expectElementVisible('.col-sm-10 form', false); | |
136 | }); | |
137 | ||
138 | it('should not call inventoryDeviceList', () => { | |
139 | expect(orchService.inventoryDeviceList).not.toHaveBeenCalled(); | |
140 | }); | |
141 | }); | |
142 | ||
143 | describe('with orchestrator', () => { | |
144 | beforeEach(() => { | |
145 | spyOn(orchService, 'status').and.returnValue(of({ available: true })); | |
146 | spyOn(orchService, 'inventoryDeviceList').and.returnValue(of([])); | |
147 | fixture.detectChanges(); | |
148 | }); | |
149 | ||
150 | it('should display form', () => { | |
151 | fixtureHelper.expectElementVisible('cd-alert-panel', false); | |
152 | fixtureHelper.expectElementVisible('.cd-col-form form', true); | |
153 | }); | |
154 | ||
155 | describe('without data devices selected', () => { | |
156 | it('should disable preview button', () => { | |
157 | expectPreviewButton(false); | |
158 | }); | |
159 | ||
160 | it('should not display shared devices slots', () => { | |
161 | fixtureHelper.expectElementVisible('#walSlots', false); | |
162 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
163 | }); | |
164 | ||
165 | it('should disable the checkboxes', () => { | |
166 | checkFeatures(false); | |
167 | }); | |
168 | }); | |
169 | ||
170 | describe('with data devices selected', () => { | |
171 | beforeEach(() => { | |
172 | selectDevices('data'); | |
173 | }); | |
174 | ||
175 | it('should enable preview button', () => { | |
176 | expectPreviewButton(true); | |
177 | }); | |
178 | ||
179 | it('should not display shared devices slots', () => { | |
180 | fixtureHelper.expectElementVisible('#walSlots', false); | |
181 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
182 | }); | |
183 | ||
184 | it('should enable the checkboxes', () => { | |
185 | checkFeatures(true); | |
186 | }); | |
187 | ||
188 | it('should disable the checkboxes after clearing data devices', () => { | |
189 | clearDevices('data'); | |
190 | checkFeatures(false); | |
191 | }); | |
192 | ||
193 | describe('with shared devices selected', () => { | |
194 | beforeEach(() => { | |
195 | selectDevices('wal'); | |
196 | selectDevices('db'); | |
197 | }); | |
198 | ||
199 | it('should display slots', () => { | |
200 | fixtureHelper.expectElementVisible('#walSlots', true); | |
201 | fixtureHelper.expectElementVisible('#dbSlots', true); | |
202 | }); | |
203 | ||
204 | it('validate slots', () => { | |
205 | for (const control of ['walSlots', 'dbSlots']) { | |
206 | formHelper.expectValid(control); | |
207 | formHelper.expectValidChange(control, 1); | |
208 | formHelper.expectErrorChange(control, -1, 'min'); | |
209 | } | |
210 | }); | |
211 | ||
212 | describe('test clearing data devices', () => { | |
213 | beforeEach(() => { | |
214 | clearDevices('data'); | |
215 | }); | |
216 | ||
217 | it('should not display shared devices slots and should disable checkboxes', () => { | |
218 | fixtureHelper.expectElementVisible('#walSlots', false); | |
219 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
220 | checkFeatures(false); | |
221 | }); | |
222 | }); | |
223 | }); | |
224 | }); | |
225 | }); | |
226 | }); |