1 import { EventEmitter } from '@angular/core';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { By } from '@angular/platform-browser';
6 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
7 import { DirectivesModule } from '../../../shared/directives/directives.module';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { RbdConfigurationSourceField } from '../../../shared/models/configuration';
10 import { DimlessBinaryPerSecondPipe } from '../../../shared/pipes/dimless-binary-per-second.pipe';
11 import { FormatterService } from '../../../shared/services/formatter.service';
12 import { RbdConfigurationService } from '../../../shared/services/rbd-configuration.service';
13 import { SharedModule } from '../../../shared/shared.module';
14 import { RbdConfigurationFormComponent } from './rbd-configuration-form.component';
16 describe('RbdConfigurationFormComponent', () => {
17 let component: RbdConfigurationFormComponent;
18 let fixture: ComponentFixture<RbdConfigurationFormComponent>;
23 imports: [ReactiveFormsModule, DirectivesModule, SharedModule],
24 declarations: [RbdConfigurationFormComponent],
26 RbdConfigurationService,
28 DimlessBinaryPerSecondPipe,
34 fixture = TestBed.createComponent(RbdConfigurationFormComponent);
35 component = fixture.componentInstance;
36 component.form = new CdFormGroup({}, null);
37 fh = new FormHelper(component.form);
38 fixture.detectChanges();
39 sections = TestBed.inject(RbdConfigurationService).sections;
42 it('should create', () => {
43 expect(component).toBeTruthy();
46 it('should create all form fields mentioned in RbdConfiguration::OPTIONS', () => {
47 /* Test form creation on a TypeScript level */
48 const actual = Object.keys((component.form.get('configuration') as CdFormGroup).controls);
49 const expected = sections
50 .map((section) => section.options)
51 .reduce((a, b) => a.concat(b))
52 .map((option: Record<string, any>) => option.name);
53 expect(actual).toEqual(expected);
55 /* Test form creation on a template level */
56 const controlDebugElements = fixture.debugElement.queryAll(By.css('input.form-control'));
57 expect(controlDebugElements.length).toBe(expected.length);
58 controlDebugElements.forEach((element) => expect(element.nativeElement).toBeTruthy());
61 it('should only contain values of changed controls if submitted', () => {
63 component.changes.subscribe((getDirtyValues: Function) => {
64 values = getDirtyValues();
66 fh.setValue('configuration.rbd_qos_bps_limit', 0, true);
67 fixture.detectChanges();
69 expect(values).toEqual({ rbd_qos_bps_limit: 0 });
72 describe('test loading of initial data for editing', () => {
74 component.initializeData = new EventEmitter<any>();
75 fixture.detectChanges();
79 it('should return dirty values without any units', () => {
81 component.changes.subscribe((getDirtyValues: Function) => {
82 dirtyValues = getDirtyValues();
85 fh.setValue('configuration.rbd_qos_bps_limit', 55, true);
86 fh.setValue('configuration.rbd_qos_iops_limit', 22, true);
88 expect(dirtyValues['rbd_qos_bps_limit']).toBe(55);
89 expect(dirtyValues['rbd_qos_iops_limit']).toBe(22);
92 it('should load initial data into forms', () => {
93 component.initializeData.emit({
96 name: 'rbd_qos_bps_limit',
101 sourceType: RbdConfigurationSourceField.pool
104 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('55 B/s');
107 it('should not load initial data if the source is not the pool itself', () => {
108 component.initializeData.emit({
111 name: 'rbd_qos_bps_limit',
113 source: RbdConfigurationSourceField.image
116 name: 'rbd_qos_iops_limit',
118 source: RbdConfigurationSourceField.global
121 sourceType: RbdConfigurationSourceField.pool
124 expect(component.form.getValue('configuration.rbd_qos_iops_limit')).toEqual('0 IOPS');
125 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('0 B/s');
128 it('should not load initial data if the source is not the image itself', () => {
129 component.initializeData.emit({
132 name: 'rbd_qos_bps_limit',
134 source: RbdConfigurationSourceField.pool
137 name: 'rbd_qos_iops_limit',
139 source: RbdConfigurationSourceField.global
142 sourceType: RbdConfigurationSourceField.image
145 expect(component.form.getValue('configuration.rbd_qos_iops_limit')).toEqual('0 IOPS');
146 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('0 B/s');
149 it('should always have formatted results', () => {
150 component.initializeData.emit({
153 name: 'rbd_qos_bps_limit',
155 source: RbdConfigurationSourceField.image
158 name: 'rbd_qos_iops_limit',
160 source: RbdConfigurationSourceField.image
163 name: 'rbd_qos_read_bps_limit',
164 value: null, // incorrect type
165 source: RbdConfigurationSourceField.image
168 name: 'rbd_qos_read_bps_limit',
169 value: undefined, // incorrect type
170 source: RbdConfigurationSourceField.image
173 sourceType: RbdConfigurationSourceField.image
176 expect(component.form.getValue('configuration.rbd_qos_iops_limit')).toEqual('22 IOPS');
177 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('55 B/s');
178 expect(component.form.getValue('configuration.rbd_qos_read_bps_limit')).toEqual('0 B/s');
179 expect(component.form.getValue('configuration.rbd_qos_read_bps_limit')).toEqual('0 B/s');
183 it('should reset the corresponding form field correctly', () => {
184 const fieldName = 'rbd_qos_bps_limit';
185 const getValue = () => component.form.get(`configuration.${fieldName}`).value;
188 fh.setValue(`configuration.${fieldName}`, 418, true);
189 expect(getValue()).toBe(418);
192 component.reset(fieldName);
193 expect(getValue()).toBe(null);
196 component.reset(fieldName);
197 expect(getValue()).toBe(418);
200 component.reset(fieldName);
201 expect(getValue()).toBe(null);
204 component.reset(fieldName);
205 expect(getValue()).toBe(418);
208 describe('should verify that getDirtyValues() returns correctly', () => {
212 component.initializeData = new EventEmitter<any>();
213 fixture.detectChanges();
214 component.ngOnInit();
218 name: 'rbd_qos_bps_limit',
220 source: RbdConfigurationSourceField.image
223 name: 'rbd_qos_iops_limit',
225 source: RbdConfigurationSourceField.image
228 name: 'rbd_qos_read_bps_limit',
230 source: RbdConfigurationSourceField.image
233 name: 'rbd_qos_read_iops_limit',
235 source: RbdConfigurationSourceField.image
238 name: 'rbd_qos_read_iops_burst',
240 source: RbdConfigurationSourceField.image
243 name: 'rbd_qos_write_bps_burst',
245 source: RbdConfigurationSourceField.global
248 name: 'rbd_qos_write_iops_burst',
250 source: RbdConfigurationSourceField.global
253 sourceType: RbdConfigurationSourceField.image
255 component.initializeData.emit(data);
258 it('should return an empty object', () => {
259 expect(component.getDirtyValues()).toEqual({});
260 expect(component.getDirtyValues(true, RbdConfigurationSourceField.image)).toEqual({});
263 it('should return dirty values', () => {
264 component.form.get('configuration.rbd_qos_write_bps_burst').markAsDirty();
265 expect(component.getDirtyValues()).toEqual({ rbd_qos_write_bps_burst: 0 });
267 component.form.get('configuration.rbd_qos_write_iops_burst').markAsDirty();
268 expect(component.getDirtyValues()).toEqual({
269 rbd_qos_write_iops_burst: 0,
270 rbd_qos_write_bps_burst: 0
274 it('should also return all local values if they do not contain their initial values', () => {
275 // Change value for all options
276 data.initialData = data.initialData.map((o: Record<string, any>) => {
282 ['rbd_qos_read_iops_limit', 'rbd_qos_write_bps_burst'].forEach((option) => {
283 component.form.get(`configuration.${option}`).markAsDirty();
286 expect(component.getDirtyValues(true, RbdConfigurationSourceField.image)).toEqual({
287 rbd_qos_read_iops_limit: 0,
288 rbd_qos_write_bps_burst: 0
292 it('should throw an error if used incorrectly', () => {
293 expect(() => component.getDirtyValues(true)).toThrowError(
294 /^ProgrammingError: If local values shall be included/