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, 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';
14 describe('ConfigurationFormComponent', () => {
15 let component: ConfigurationFormComponent;
16 let fixture: ComponentFixture<ConfigurationFormComponent>;
20 HttpClientTestingModule,
23 ToastModule.forRoot(),
26 declarations: [ConfigurationFormComponent],
29 provide: ActivatedRoute
36 fixture = TestBed.createComponent(ConfigurationFormComponent);
37 component = fixture.componentInstance;
40 it('should create', () => {
41 expect(component).toBeTruthy();
44 describe('getValidators', () => {
45 it('should return a validator for types float, addr and uuid', () => {
46 const types = ['float', 'addr', 'uuid'];
48 types.forEach((valType) => {
49 const configOption = new ConfigFormModel();
50 configOption.type = valType;
52 const ret = component.getValidators(configOption);
53 expect(ret).toBeTruthy();
54 expect(ret.length).toBe(1);
58 it('should not return a validator for types str and bool', () => {
59 const types = ['str', 'bool'];
61 types.forEach((valType) => {
62 const configOption = new ConfigFormModel();
63 configOption.type = valType;
65 const ret = component.getValidators(configOption);
66 expect(ret).toBeUndefined();
70 it('should return a pattern and a min validator', () => {
71 const configOption = new ConfigFormModel();
72 configOption.type = 'int';
75 const ret = component.getValidators(configOption);
76 expect(ret).toBeTruthy();
77 expect(ret.length).toBe(2);
78 expect(component.minValue).toBe(2);
79 expect(component.maxValue).toBeUndefined();
82 it('should return a pattern and a max validator', () => {
83 const configOption = new ConfigFormModel();
84 configOption.type = 'int';
87 const ret = component.getValidators(configOption);
88 expect(ret).toBeTruthy();
89 expect(ret.length).toBe(2);
90 expect(component.minValue).toBeUndefined();
91 expect(component.maxValue).toBe(5);
94 it('should return multiple validators', () => {
95 const configOption = new ConfigFormModel();
96 configOption.type = 'float';
97 configOption.max = 5.2;
98 configOption.min = 1.5;
100 const ret = component.getValidators(configOption);
101 expect(ret).toBeTruthy();
102 expect(ret.length).toBe(3);
103 expect(component.minValue).toBe(1.5);
104 expect(component.maxValue).toBe(5.2);
108 describe('getStep', () => {
109 it('should return the correct step for type uint and value 0', () => {
110 const ret = component.getStep('uint', 0);
114 it('should return the correct step for type int and value 1', () => {
115 const ret = component.getStep('int', 1);
119 it('should return the correct step for type int and value null', () => {
120 const ret = component.getStep('int', null);
124 it('should return the correct step for type size and value 2', () => {
125 const ret = component.getStep('size', 2);
129 it('should return the correct step for type secs and value 3', () => {
130 const ret = component.getStep('secs', 3);
134 it('should return the correct step for type float and value 1', () => {
135 const ret = component.getStep('float', 1);
136 expect(ret).toBe(0.1);
139 it('should return the correct step for type float and value 0.1', () => {
140 const ret = component.getStep('float', 0.1);
141 expect(ret).toBe(0.1);
144 it('should return the correct step for type float and value 0.02', () => {
145 const ret = component.getStep('float', 0.02);
146 expect(ret).toBe(0.01);
149 it('should return the correct step for type float and value 0.003', () => {
150 const ret = component.getStep('float', 0.003);
151 expect(ret).toBe(0.001);
154 it('should return the correct step for type float and value null', () => {
155 const ret = component.getStep('float', null);
156 expect(ret).toBe(0.1);
159 it('should return undefined for unknown type', () => {
160 const ret = component.getStep('unknown', 1);
161 expect(ret).toBeUndefined();