]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
d411d6724d2b77d8e641e637b92cddad04747474
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { ActivatedRoute } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import { ToastModule } from 'ng2-toastr';
8
9 import { configureTestBed, i18nProviders } from '../../../../../testing/unit-test-helper';
10 import { SharedModule } from '../../../../shared/shared.module';
11 import { ConfigurationFormComponent } from './configuration-form.component';
12 import { ConfigFormModel } from './configuration-form.model';
13
14 describe('ConfigurationFormComponent', () => {
15   let component: ConfigurationFormComponent;
16   let fixture: ComponentFixture<ConfigurationFormComponent>;
17
18   configureTestBed({
19     imports: [
20       HttpClientTestingModule,
21       ReactiveFormsModule,
22       RouterTestingModule,
23       ToastModule.forRoot(),
24       SharedModule
25     ],
26     declarations: [ConfigurationFormComponent],
27     providers: [
28       {
29         provide: ActivatedRoute
30       },
31       i18nProviders
32     ]
33   });
34
35   beforeEach(() => {
36     fixture = TestBed.createComponent(ConfigurationFormComponent);
37     component = fixture.componentInstance;
38   });
39
40   it('should create', () => {
41     expect(component).toBeTruthy();
42   });
43
44   describe('getType', () => {
45     it('should return uint type', () => {
46       const ret = component.getType('uint');
47       expect(ret).toBeTruthy();
48       expect(ret.name).toBe('uint');
49       expect(ret.inputType).toBe('number');
50       expect(ret.humanReadable).toBe('Positive integer value');
51       expect(ret.defaultMin).toBe(0);
52       expect(ret.patternHelpText).toBe('The entered value needs to be a positive number.');
53       expect(ret.isNumberType).toBe(true);
54       expect(ret.allowsNegative).toBe(false);
55     });
56
57     it('should return int type', () => {
58       const ret = component.getType('int');
59       expect(ret).toBeTruthy();
60       expect(ret.name).toBe('int');
61       expect(ret.inputType).toBe('number');
62       expect(ret.humanReadable).toBe('Integer value');
63       expect(ret.defaultMin).toBeUndefined();
64       expect(ret.patternHelpText).toBe('The entered value needs to be a number.');
65       expect(ret.isNumberType).toBe(true);
66       expect(ret.allowsNegative).toBe(true);
67     });
68
69     it('should return size type', () => {
70       const ret = component.getType('size');
71       expect(ret).toBeTruthy();
72       expect(ret.name).toBe('size');
73       expect(ret.inputType).toBe('number');
74       expect(ret.humanReadable).toBe('Positive integer value (size)');
75       expect(ret.defaultMin).toBe(0);
76       expect(ret.patternHelpText).toBe('The entered value needs to be a positive number.');
77       expect(ret.isNumberType).toBe(true);
78       expect(ret.allowsNegative).toBe(false);
79     });
80
81     it('should return secs type', () => {
82       const ret = component.getType('secs');
83       expect(ret).toBeTruthy();
84       expect(ret.name).toBe('secs');
85       expect(ret.inputType).toBe('number');
86       expect(ret.humanReadable).toBe('Positive integer value (secs)');
87       expect(ret.defaultMin).toBe(1);
88       expect(ret.patternHelpText).toBe('The entered value needs to be a positive number.');
89       expect(ret.isNumberType).toBe(true);
90       expect(ret.allowsNegative).toBe(false);
91     });
92
93     it('should return float type', () => {
94       const ret = component.getType('float');
95       expect(ret).toBeTruthy();
96       expect(ret.name).toBe('float');
97       expect(ret.inputType).toBe('number');
98       expect(ret.humanReadable).toBe('Decimal value');
99       expect(ret.defaultMin).toBeUndefined();
100       expect(ret.patternHelpText).toBe('The entered value needs to be a number or decimal.');
101       expect(ret.isNumberType).toBe(true);
102       expect(ret.allowsNegative).toBe(true);
103     });
104
105     it('should return str type', () => {
106       const ret = component.getType('str');
107       expect(ret).toBeTruthy();
108       expect(ret.name).toBe('str');
109       expect(ret.inputType).toBe('text');
110       expect(ret.humanReadable).toBe('Text');
111       expect(ret.defaultMin).toBeUndefined();
112       expect(ret.patternHelpText).toBeUndefined();
113       expect(ret.isNumberType).toBe(false);
114       expect(ret.allowsNegative).toBeUndefined();
115     });
116
117     it('should return addr type', () => {
118       const ret = component.getType('addr');
119       expect(ret).toBeTruthy();
120       expect(ret.name).toBe('addr');
121       expect(ret.inputType).toBe('text');
122       expect(ret.humanReadable).toBe('IPv4 or IPv6 address');
123       expect(ret.defaultMin).toBeUndefined();
124       expect(ret.patternHelpText).toBe('The entered value needs to be a valid IP address.');
125       expect(ret.isNumberType).toBe(false);
126       expect(ret.allowsNegative).toBeUndefined();
127     });
128
129     it('should return uuid type', () => {
130       const ret = component.getType('uuid');
131       expect(ret).toBeTruthy();
132       expect(ret.name).toBe('uuid');
133       expect(ret.inputType).toBe('text');
134       expect(ret.humanReadable).toBe('UUID');
135       expect(ret.defaultMin).toBeUndefined();
136       expect(ret.patternHelpText).toBe(
137         'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8'
138       );
139       expect(ret.isNumberType).toBe(false);
140       expect(ret.allowsNegative).toBeUndefined();
141     });
142
143     it('should return bool type', () => {
144       const ret = component.getType('bool');
145       expect(ret).toBeTruthy();
146       expect(ret.name).toBe('bool');
147       expect(ret.inputType).toBe('checkbox');
148       expect(ret.humanReadable).toBe('Boolean value');
149       expect(ret.defaultMin).toBeUndefined();
150       expect(ret.patternHelpText).toBeUndefined();
151       expect(ret.isNumberType).toBe(false);
152       expect(ret.allowsNegative).toBeUndefined();
153     });
154
155     it('should throw an error for unknown type', () => {
156       expect(() =>
157         component.getType('unknown').toThrowError('Found unknown type "unknown" for config option.')
158       );
159     });
160   });
161
162   describe('getValidators', () => {
163     it('should return a validator for types float, addr and uuid', () => {
164       const types = ['float', 'addr', 'uuid'];
165
166       types.forEach((valType) => {
167         const configOption = new ConfigFormModel();
168         configOption.type = valType;
169
170         const ret = component.getValidators(configOption);
171         expect(ret).toBeTruthy();
172         expect(ret.length).toBe(1);
173       });
174     });
175
176     it('should not return a validator for types str and bool', () => {
177       const types = ['str', 'bool'];
178
179       types.forEach((valType) => {
180         const configOption = new ConfigFormModel();
181         configOption.type = valType;
182
183         const ret = component.getValidators(configOption);
184         expect(ret).toBeUndefined();
185       });
186     });
187
188     it('should return a pattern and a min validator', () => {
189       const configOption = new ConfigFormModel();
190       configOption.type = 'int';
191       configOption.min = 2;
192
193       const ret = component.getValidators(configOption);
194       expect(ret).toBeTruthy();
195       expect(ret.length).toBe(2);
196       expect(component.minValue).toBe(2);
197       expect(component.maxValue).toBeUndefined();
198     });
199
200     it('should return a pattern and a max validator', () => {
201       const configOption = new ConfigFormModel();
202       configOption.type = 'int';
203       configOption.max = 5;
204
205       const ret = component.getValidators(configOption);
206       expect(ret).toBeTruthy();
207       expect(ret.length).toBe(2);
208       expect(component.minValue).toBeUndefined();
209       expect(component.maxValue).toBe(5);
210     });
211
212     it('should return multiple validators', () => {
213       const configOption = new ConfigFormModel();
214       configOption.type = 'float';
215       configOption.max = 5.2;
216       configOption.min = 1.5;
217
218       const ret = component.getValidators(configOption);
219       expect(ret).toBeTruthy();
220       expect(ret.length).toBe(3);
221       expect(component.minValue).toBe(1.5);
222       expect(component.maxValue).toBe(5.2);
223     });
224   });
225
226   describe('getStep', () => {
227     it('should return the correct step for type uint and value 0', () => {
228       const ret = component.getStep('uint', 0);
229       expect(ret).toBe(1);
230     });
231
232     it('should return the correct step for type int and value 1', () => {
233       const ret = component.getStep('int', 1);
234       expect(ret).toBe(1);
235     });
236
237     it('should return the correct step for type int and value null', () => {
238       const ret = component.getStep('int', null);
239       expect(ret).toBe(1);
240     });
241
242     it('should return the correct step for type size and value 2', () => {
243       const ret = component.getStep('size', 2);
244       expect(ret).toBe(1);
245     });
246
247     it('should return the correct step for type secs and value 3', () => {
248       const ret = component.getStep('secs', 3);
249       expect(ret).toBe(1);
250     });
251
252     it('should return the correct step for type float and value 1', () => {
253       const ret = component.getStep('float', 1);
254       expect(ret).toBe(0.1);
255     });
256
257     it('should return the correct step for type float and value 0.1', () => {
258       const ret = component.getStep('float', 0.1);
259       expect(ret).toBe(0.1);
260     });
261
262     it('should return the correct step for type float and value 0.02', () => {
263       const ret = component.getStep('float', 0.02);
264       expect(ret).toBe(0.01);
265     });
266
267     it('should return the correct step for type float and value 0.003', () => {
268       const ret = component.getStep('float', 0.003);
269       expect(ret).toBe(0.001);
270     });
271
272     it('should return the correct step for type float and value null', () => {
273       const ret = component.getStep('float', null);
274       expect(ret).toBe(0.1);
275     });
276
277     it('should return undefined for unknown type', () => {
278       const ret = component.getStep('unknown', 1);
279       expect(ret).toBeUndefined();
280     });
281   });
282 });