]>
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 | ||
f67539c2 TL |
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'; | |
9f95a23c TL |
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'; | |
22 | ||
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; | |
f67539c2 | 30 | let hostService: HostService; |
9f95a23c TL |
31 | let summaryService: SummaryService; |
32 | const devices: InventoryDevice[] = [ | |
33 | { | |
34 | hostname: 'node0', | |
35 | uid: '1', | |
36 | ||
37 | path: '/dev/sda', | |
38 | sys_api: { | |
39 | vendor: 'VENDOR', | |
40 | model: 'MODEL', | |
41 | size: 1024, | |
42 | rotational: 'false', | |
43 | human_readable_size: '1 KB' | |
44 | }, | |
45 | available: true, | |
46 | rejected_reasons: [''], | |
47 | device_id: 'VENDOR-MODEL-ID', | |
48 | human_readable_type: 'nvme/ssd', | |
49 | osd_ids: [] | |
50 | } | |
51 | ]; | |
52 | ||
53 | const expectPreviewButton = (enabled: boolean) => { | |
f67539c2 | 54 | const debugElement = fixtureHelper.getElementByCss('.tc_submitButton'); |
9f95a23c TL |
55 | expect(debugElement.nativeElement.disabled).toBe(!enabled); |
56 | }; | |
57 | ||
58 | const selectDevices = (type: string) => { | |
59 | const event: DevicesSelectionChangeEvent = { | |
60 | type: type, | |
61 | filters: [], | |
62 | data: devices, | |
63 | dataOut: [] | |
64 | }; | |
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; | |
72 | } | |
73 | fixture.detectChanges(); | |
74 | }; | |
75 | ||
76 | const clearDevices = (type: string) => { | |
77 | const event: DevicesSelectionClearEvent = { | |
78 | type: type, | |
79 | clearedDevices: [] | |
80 | }; | |
81 | component.onDevicesCleared(event); | |
82 | fixture.detectChanges(); | |
83 | }; | |
84 | ||
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); | |
91 | } | |
92 | }; | |
93 | ||
94 | configureTestBed({ | |
95 | imports: [ | |
e306af50 | 96 | BrowserAnimationsModule, |
9f95a23c TL |
97 | HttpClientTestingModule, |
98 | FormsModule, | |
99 | SharedModule, | |
100 | RouterTestingModule, | |
101 | ReactiveFormsModule, | |
102 | ToastrModule.forRoot() | |
103 | ], | |
9f95a23c TL |
104 | declarations: [OsdFormComponent, OsdDevicesSelectionGroupsComponent, InventoryDevicesComponent] |
105 | }); | |
106 | ||
107 | beforeEach(() => { | |
108 | fixture = TestBed.createComponent(OsdFormComponent); | |
109 | fixtureHelper = new FixtureHelper(fixture); | |
110 | component = fixture.componentInstance; | |
111 | form = component.form; | |
112 | formHelper = new FormHelper(form); | |
f67539c2 TL |
113 | orchService = TestBed.inject(OrchestratorService); |
114 | hostService = TestBed.inject(HostService); | |
115 | summaryService = TestBed.inject(SummaryService); | |
9f95a23c TL |
116 | summaryService['summaryDataSource'] = new BehaviorSubject(null); |
117 | summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable(); | |
118 | summaryService['summaryDataSource'].next({ version: 'master' }); | |
119 | }); | |
120 | ||
121 | it('should create', () => { | |
122 | expect(component).toBeTruthy(); | |
123 | }); | |
124 | ||
125 | describe('without orchestrator', () => { | |
126 | beforeEach(() => { | |
127 | spyOn(orchService, 'status').and.returnValue(of({ available: false })); | |
f67539c2 | 128 | spyOn(hostService, 'inventoryDeviceList').and.callThrough(); |
9f95a23c TL |
129 | fixture.detectChanges(); |
130 | }); | |
131 | ||
132 | it('should display info panel to document', () => { | |
133 | fixtureHelper.expectElementVisible('cd-alert-panel', true); | |
134 | fixtureHelper.expectElementVisible('.col-sm-10 form', false); | |
135 | }); | |
136 | ||
137 | it('should not call inventoryDeviceList', () => { | |
f67539c2 | 138 | expect(hostService.inventoryDeviceList).not.toHaveBeenCalled(); |
9f95a23c TL |
139 | }); |
140 | }); | |
141 | ||
142 | describe('with orchestrator', () => { | |
143 | beforeEach(() => { | |
144 | spyOn(orchService, 'status').and.returnValue(of({ available: true })); | |
f67539c2 | 145 | spyOn(hostService, 'inventoryDeviceList').and.returnValue(of([])); |
9f95a23c TL |
146 | fixture.detectChanges(); |
147 | }); | |
148 | ||
149 | it('should display form', () => { | |
150 | fixtureHelper.expectElementVisible('cd-alert-panel', false); | |
151 | fixtureHelper.expectElementVisible('.cd-col-form form', true); | |
152 | }); | |
153 | ||
154 | describe('without data devices selected', () => { | |
155 | it('should disable preview button', () => { | |
156 | expectPreviewButton(false); | |
157 | }); | |
158 | ||
159 | it('should not display shared devices slots', () => { | |
160 | fixtureHelper.expectElementVisible('#walSlots', false); | |
161 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
162 | }); | |
163 | ||
164 | it('should disable the checkboxes', () => { | |
165 | checkFeatures(false); | |
166 | }); | |
167 | }); | |
168 | ||
169 | describe('with data devices selected', () => { | |
170 | beforeEach(() => { | |
171 | selectDevices('data'); | |
172 | }); | |
173 | ||
174 | it('should enable preview button', () => { | |
175 | expectPreviewButton(true); | |
176 | }); | |
177 | ||
178 | it('should not display shared devices slots', () => { | |
179 | fixtureHelper.expectElementVisible('#walSlots', false); | |
180 | fixtureHelper.expectElementVisible('#dbSlots', false); | |
181 | }); | |
182 | ||
183 | it('should enable the checkboxes', () => { | |
184 | checkFeatures(true); | |
185 | }); | |
186 | ||
187 | it('should disable the checkboxes after clearing data devices', () => { | |
188 | clearDevices('data'); | |
189 | checkFeatures(false); | |
190 | }); | |
191 | ||
192 | describe('with shared devices selected', () => { | |
193 | beforeEach(() => { | |
194 | selectDevices('wal'); | |
195 | selectDevices('db'); | |
196 | }); | |
197 | ||
198 | it('should display slots', () => { | |
199 | fixtureHelper.expectElementVisible('#walSlots', true); | |
200 | fixtureHelper.expectElementVisible('#dbSlots', true); | |
201 | }); | |
202 | ||
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'); | |
208 | } | |
209 | }); | |
210 | ||
211 | describe('test clearing data devices', () => { | |
212 | beforeEach(() => { | |
213 | clearDevices('data'); | |
214 | }); | |
215 | ||
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); | |
220 | }); | |
221 | }); | |
222 | }); | |
223 | }); | |
224 | }); | |
225 | }); |