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 { ComponentLoaderFactory } from 'ngx-bootstrap/component-loader';
7 import { PositioningService } from 'ngx-bootstrap/positioning';
8 import { TooltipConfig, TooltipModule } from 'ngx-bootstrap/tooltip';
10 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
11 import { DirectivesModule } from '../../../shared/directives/directives.module';
12 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
13 import { RbdConfigurationSourceField } from '../../../shared/models/configuration';
14 import { DimlessBinaryPerSecondPipe } from '../../../shared/pipes/dimless-binary-per-second.pipe';
15 import { FormatterService } from '../../../shared/services/formatter.service';
16 import { RbdConfigurationService } from '../../../shared/services/rbd-configuration.service';
17 import { SharedModule } from '../../../shared/shared.module';
18 import { RbdConfigurationFormComponent } from './rbd-configuration-form.component';
20 describe('RbdConfigurationFormComponent', () => {
21 let component: RbdConfigurationFormComponent;
22 let fixture: ComponentFixture<RbdConfigurationFormComponent>;
27 imports: [ReactiveFormsModule, TooltipModule, DirectivesModule, SharedModule],
28 declarations: [RbdConfigurationFormComponent],
30 ComponentLoaderFactory,
33 RbdConfigurationService,
35 DimlessBinaryPerSecondPipe,
41 fixture = TestBed.createComponent(RbdConfigurationFormComponent);
42 component = fixture.componentInstance;
43 component.form = new CdFormGroup({}, null);
44 fh = new FormHelper(component.form);
45 fixture.detectChanges();
46 sections = TestBed.get(RbdConfigurationService).sections;
49 it('should create', () => {
50 expect(component).toBeTruthy();
53 it('should create all form fields mentioned in RbdConfiguration::OPTIONS', () => {
54 /* Test form creation on a TypeScript level */
55 const actual = Object.keys((component.form.get('configuration') as CdFormGroup).controls);
56 const expected = sections
57 .map((section) => section.options)
58 .reduce((a, b) => a.concat(b))
59 .map((option) => option.name);
60 expect(actual).toEqual(expected);
62 /* Test form creation on a template level */
63 const controlDebugElements = fixture.debugElement.queryAll(By.css('input.form-control'));
64 expect(controlDebugElements.length).toBe(expected.length);
65 controlDebugElements.forEach((element) => expect(element.nativeElement).toBeTruthy());
68 it('should only contain values of changed controls if submitted', () => {
70 component.changes.subscribe((getDirtyValues: Function) => {
71 values = getDirtyValues();
73 fh.setValue('configuration.rbd_qos_bps_limit', 0, true);
74 fixture.detectChanges();
76 expect(values).toEqual({ rbd_qos_bps_limit: 0 });
79 describe('test loading of initial data for editing', () => {
81 component.initializeData = new EventEmitter<any>();
82 fixture.detectChanges();
86 it('should return dirty values without any units', () => {
88 component.changes.subscribe((getDirtyValues) => {
89 dirtyValues = getDirtyValues();
92 fh.setValue('configuration.rbd_qos_bps_limit', 55, true);
93 fh.setValue('configuration.rbd_qos_iops_limit', 22, true);
95 expect(dirtyValues['rbd_qos_bps_limit']).toBe(55);
96 expect(dirtyValues['rbd_qos_iops_limit']).toBe(22);
99 it('should load initial data into forms', () => {
100 component.initializeData.emit({
103 name: 'rbd_qos_bps_limit',
108 sourceType: RbdConfigurationSourceField.pool
111 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('55 B/s');
114 it('should not load initial data if the source is not the pool itself', () => {
115 component.initializeData.emit({
118 name: 'rbd_qos_bps_limit',
120 source: RbdConfigurationSourceField.image
123 name: 'rbd_qos_iops_limit',
125 source: RbdConfigurationSourceField.global
128 sourceType: RbdConfigurationSourceField.pool
131 expect(component.form.getValue('configuration.rbd_qos_iops_limit')).toEqual('0 IOPS');
132 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('0 B/s');
135 it('should not load initial data if the source is not the image itself', () => {
136 component.initializeData.emit({
139 name: 'rbd_qos_bps_limit',
141 source: RbdConfigurationSourceField.pool
144 name: 'rbd_qos_iops_limit',
146 source: RbdConfigurationSourceField.global
149 sourceType: RbdConfigurationSourceField.image
152 expect(component.form.getValue('configuration.rbd_qos_iops_limit')).toEqual('0 IOPS');
153 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('0 B/s');
156 it('should always have formatted results', () => {
157 component.initializeData.emit({
160 name: 'rbd_qos_bps_limit',
162 source: RbdConfigurationSourceField.image
165 name: 'rbd_qos_iops_limit',
167 source: RbdConfigurationSourceField.image
170 name: 'rbd_qos_read_bps_limit',
171 value: null, // incorrect type
172 source: RbdConfigurationSourceField.image
175 name: 'rbd_qos_read_bps_limit',
176 value: undefined, // incorrect type
177 source: RbdConfigurationSourceField.image
180 sourceType: RbdConfigurationSourceField.image
183 expect(component.form.getValue('configuration.rbd_qos_iops_limit')).toEqual('22 IOPS');
184 expect(component.form.getValue('configuration.rbd_qos_bps_limit')).toEqual('55 B/s');
185 expect(component.form.getValue('configuration.rbd_qos_read_bps_limit')).toEqual('0 B/s');
186 expect(component.form.getValue('configuration.rbd_qos_read_bps_limit')).toEqual('0 B/s');
190 it('should reset the corresponding form field correctly', () => {
191 const fieldName = 'rbd_qos_bps_limit';
192 const getValue = () => component.form.get(`configuration.${fieldName}`).value;
195 fh.setValue(`configuration.${fieldName}`, 418, true);
196 expect(getValue()).toBe(418);
199 component.reset(fieldName);
200 expect(getValue()).toBe(null);
203 component.reset(fieldName);
204 expect(getValue()).toBe(418);
207 component.reset(fieldName);
208 expect(getValue()).toBe(null);
211 component.reset(fieldName);
212 expect(getValue()).toBe(418);
215 describe('should verify that getDirtyValues() returns correctly', () => {
219 component.initializeData = new EventEmitter<any>();
220 fixture.detectChanges();
221 component.ngOnInit();
225 name: 'rbd_qos_bps_limit',
227 source: RbdConfigurationSourceField.image
230 name: 'rbd_qos_iops_limit',
232 source: RbdConfigurationSourceField.image
235 name: 'rbd_qos_read_bps_limit',
237 source: RbdConfigurationSourceField.image
240 name: 'rbd_qos_read_iops_limit',
242 source: RbdConfigurationSourceField.image
245 name: 'rbd_qos_read_iops_burst',
247 source: RbdConfigurationSourceField.image
250 name: 'rbd_qos_write_bps_burst',
252 source: RbdConfigurationSourceField.global
255 name: 'rbd_qos_write_iops_burst',
257 source: RbdConfigurationSourceField.global
260 sourceType: RbdConfigurationSourceField.image
262 component.initializeData.emit(data);
265 it('should return an empty object', () => {
266 expect(component.getDirtyValues()).toEqual({});
267 expect(component.getDirtyValues(true, RbdConfigurationSourceField.image)).toEqual({});
270 it('should return dirty values', () => {
271 component.form.get('configuration.rbd_qos_write_bps_burst').markAsDirty();
272 expect(component.getDirtyValues()).toEqual({ rbd_qos_write_bps_burst: 0 });
274 component.form.get('configuration.rbd_qos_write_iops_burst').markAsDirty();
275 expect(component.getDirtyValues()).toEqual({
276 rbd_qos_write_iops_burst: 0,
277 rbd_qos_write_bps_burst: 0
281 it('should also return all local values if they do not contain their initial values', () => {
282 // Change value for all options
283 data.initialData = data.initialData.map((o) => {
289 ['rbd_qos_read_iops_limit', 'rbd_qos_write_bps_burst'].forEach((option) => {
290 component.form.get(`configuration.${option}`).markAsDirty();
293 expect(component.getDirtyValues(true, RbdConfigurationSourceField.image)).toEqual({
294 rbd_qos_read_iops_limit: 0,
295 rbd_qos_write_bps_burst: 0
299 it('should throw an error if used incorrectly', () => {
300 expect(() => component.getDirtyValues(true)).toThrowError(
301 /^ProgrammingError: If local values shall be included/