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';
7 import { ToastModule } from 'ng2-toastr';
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';
14 describe('ConfigurationFormComponent', () => {
15 let component: ConfigurationFormComponent;
16 let fixture: ComponentFixture<ConfigurationFormComponent>;
20 HttpClientTestingModule,
23 ToastModule.forRoot(),
26 declarations: [ConfigurationFormComponent],
29 provide: ActivatedRoute
35 fixture = TestBed.createComponent(ConfigurationFormComponent);
36 component = fixture.componentInstance;
39 it('should create', () => {
40 expect(component).toBeTruthy();
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);
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);
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);
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);
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);
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();
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();
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'
138 expect(ret.isNumberType).toBe(false);
139 expect(ret.allowsNegative).toBeUndefined();
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();
154 it('should throw an error for unknown type', () => {
156 component.getType('unknown').toThrowError('Found unknown type "unknown" for config option.')
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'];
165 types.forEach((valType) => {
166 const configOption = new ConfigFormModel();
167 configOption.type = valType;
169 const ret = component.getValidators(configOption);
170 expect(ret).toBeTruthy();
171 expect(ret.length).toBe(1);
175 it('should not return a validator for types std::string and bool', () => {
176 const types = ['std::string', 'bool'];
178 types.forEach((valType) => {
179 const configOption = new ConfigFormModel();
180 configOption.type = valType;
182 const ret = component.getValidators(configOption);
183 expect(ret).toBeUndefined();
187 it('should return a pattern and a min validator', () => {
188 const configOption = new ConfigFormModel();
189 configOption.type = 'int64_t';
190 configOption.min = 2;
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();
199 it('should return a pattern and a max validator', () => {
200 const configOption = new ConfigFormModel();
201 configOption.type = 'int64_t';
202 configOption.max = 5;
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);
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;
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);
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);
231 it('should return the correct step for type int64_t and value 1', () => {
232 const ret = component.getStep('int64_t', 1);
236 it('should return the correct step for type int64_t and value null', () => {
237 const ret = component.getStep('int64_t', null);
241 it('should return the correct step for type size_t and value 2', () => {
242 const ret = component.getStep('size_t', 2);
246 it('should return the correct step for type secs and value 3', () => {
247 const ret = component.getStep('secs', 3);
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);
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);
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);
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);
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);
276 it('should return undefined for unknown type', () => {
277 const ret = component.getStep('unknown', 1);
278 expect(ret).toBeUndefined();