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