]>
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'; | |
e306af50 | 4 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
9f95a23c TL |
5 | import { RouterTestingModule } from '@angular/router/testing'; |
6 | ||
7 | import { ToastrModule } from 'ngx-toastr'; | |
8 | import { BehaviorSubject, of } from 'rxjs'; | |
9 | ||
10 | import { | |
11 | configureTestBed, | |
12 | FixtureHelper, | |
13 | FormHelper, | |
14 | i18nProviders | |
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'; | |
26 | ||
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[] = [ | |
36 | { | |
37 | hostname: 'node0', | |
38 | uid: '1', | |
39 | ||
40 | path: '/dev/sda', | |
41 | sys_api: { | |
42 | vendor: 'VENDOR', | |
43 | model: 'MODEL', | |
44 | size: 1024, | |
45 | rotational: 'false', | |
46 | human_readable_size: '1 KB' | |
47 | }, | |
48 | available: true, | |
49 | rejected_reasons: [''], | |
50 | device_id: 'VENDOR-MODEL-ID', | |
51 | human_readable_type: 'nvme/ssd', | |
52 | osd_ids: [] | |
53 | } | |
54 | ]; | |
55 | ||
56 | const expectPreviewButton = (enabled: boolean) => { | |
57 | const debugElement = fixtureHelper.getElementByCss('.card-footer button'); | |
58 | expect(debugElement.nativeElement.disabled).toBe(!enabled); | |
59 | }; | |
60 | ||
61 | const selectDevices = (type: string) => { | |
62 | const event: DevicesSelectionChangeEvent = { | |
63 | type: type, | |
64 | filters: [], | |
65 | data: devices, | |
66 | dataOut: [] | |
67 | }; | |
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; | |
75 | } | |
76 | fixture.detectChanges(); | |
77 | }; | |
78 | ||
79 | const clearDevices = (type: string) => { | |
80 | const event: DevicesSelectionClearEvent = { | |
81 | type: type, | |
82 | clearedDevices: [] | |
83 | }; | |
84 | component.onDevicesCleared(event); | |
85 | fixture.detectChanges(); | |
86 | }; | |
87 | ||
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); | |
94 | } | |
95 | }; | |
96 | ||
97 | configureTestBed({ | |
98 | imports: [ | |
e306af50 | 99 | BrowserAnimationsModule, |
9f95a23c TL |
100 | HttpClientTestingModule, |
101 | FormsModule, | |
102 | SharedModule, | |
103 | RouterTestingModule, | |
104 | ReactiveFormsModule, | |
105 | ToastrModule.forRoot() | |
106 | ], | |
107 | providers: [i18nProviders], | |
108 | declarations: [OsdFormComponent, OsdDevicesSelectionGroupsComponent, InventoryDevicesComponent] | |
109 | }); | |
110 | ||
111 | beforeEach(() => { | |
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' }); | |
122 | }); | |
123 | ||
124 | it('should create', () => { | |
125 | expect(component).toBeTruthy(); | |
126 | }); | |
127 | ||
128 | describe('without orchestrator', () => { | |
129 | beforeEach(() => { | |
130 | spyOn(orchService, 'status').and.returnValue(of({ available: false })); | |
131 | spyOn(orchService, 'inventoryDeviceList').and.callThrough(); | |
132 | fixture.detectChanges(); | |
133 | }); | |
134 | ||
135 | it('should display info panel to document', () => { | |
136 | fixtureHelper.expectElementVisible('cd-alert-panel', true); | |
137 | fixtureHelper.expectElementVisible('.col-sm-10 form', false); | |
138 | }); | |
139 | ||
140 | it('should not call inventoryDeviceList', () => { | |
141 | expect(orchService.inventoryDeviceList).not.toHaveBeenCalled(); | |
142 | }); | |
143 | }); | |
144 | ||
145 | describe('with orchestrator', () => { | |
146 | beforeEach(() => { | |
147 | spyOn(orchService, 'status').and.returnValue(of({ available: true })); | |
148 | spyOn(orchService, 'inventoryDeviceList').and.returnValue(of([])); | |
149 | fixture.detectChanges(); | |
150 | }); | |
151 | ||
152 | it('should display form', () => { | |
153 | fixtureHelper.expectElementVisible('cd-alert-panel', false); | |
154 | fixtureHelper.expectElementVisible('.cd-col-form form', true); | |
155 | }); | |
156 | ||
157 | describe('without data devices selected', () => { | |
158 | it('should disable preview button', () => { | |
159 | expectPreviewButton(false); | |
160 | }); | |
161 | ||
162 | it('should not display shared devices slots', () => { | |
163 | fixtureHelper.expectElementVisible('#walSlots', false); | |
164 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
165 | }); | |
166 | ||
167 | it('should disable the checkboxes', () => { | |
168 | checkFeatures(false); | |
169 | }); | |
170 | }); | |
171 | ||
172 | describe('with data devices selected', () => { | |
173 | beforeEach(() => { | |
174 | selectDevices('data'); | |
175 | }); | |
176 | ||
177 | it('should enable preview button', () => { | |
178 | expectPreviewButton(true); | |
179 | }); | |
180 | ||
181 | it('should not display shared devices slots', () => { | |
182 | fixtureHelper.expectElementVisible('#walSlots', false); | |
183 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
184 | }); | |
185 | ||
186 | it('should enable the checkboxes', () => { | |
187 | checkFeatures(true); | |
188 | }); | |
189 | ||
190 | it('should disable the checkboxes after clearing data devices', () => { | |
191 | clearDevices('data'); | |
192 | checkFeatures(false); | |
193 | }); | |
194 | ||
195 | describe('with shared devices selected', () => { | |
196 | beforeEach(() => { | |
197 | selectDevices('wal'); | |
198 | selectDevices('db'); | |
199 | }); | |
200 | ||
201 | it('should display slots', () => { | |
202 | fixtureHelper.expectElementVisible('#walSlots', true); | |
203 | fixtureHelper.expectElementVisible('#dbSlots', true); | |
204 | }); | |
205 | ||
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'); | |
211 | } | |
212 | }); | |
213 | ||
214 | describe('test clearing data devices', () => { | |
215 | beforeEach(() => { | |
216 | clearDevices('data'); | |
217 | }); | |
218 | ||
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); | |
223 | }); | |
224 | }); | |
225 | }); | |
226 | }); | |
227 | }); | |
228 | }); |