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