Fix import VFC with attributes
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / attributes / attributes-modal.component.spec.ts
1 import { NO_ERRORS_SCHEMA } from '@angular/core';
2 import { async, ComponentFixture } from '@angular/core/testing';
3 import { ConfigureFn, configureTests } from '../../../../../jest/test-config.helper';
4 import { AttributeModel } from '../../../../models/attributes';
5 import { ValidationUtils } from '../../../../utils/validation-utils';
6 import { CacheService } from '../../../services/cache.service';
7 import { TranslatePipe } from '../../../shared/translator/translate.pipe';
8 import { TranslateService } from '../../../shared/translator/translate.service';
9 import { AttributeModalComponent } from './attribute-modal.component';
10
11 describe('attributes modal component', () => {
12
13     let fixture: ComponentFixture<AttributeModalComponent>;
14
15     // Mocks
16     let translateServiceMock: Partial<TranslateService>;
17     let cacheServiceMock: Partial<CacheService>;
18
19     const validationPatterns = {
20         integerNoLeadingZero : 'int_regx',
21         number : 'number_regx'
22     };
23
24     const newAttribute = {
25         uniqueId: '1', name: 'attr1', description: 'description1', type: 'string', hidden: false, defaultValue: 'val1', schema: null
26     };
27
28     beforeEach(
29         async(() => {
30
31             translateServiceMock = {
32                 translate: jest.fn()
33             };
34
35             cacheServiceMock = {
36                 get: jest.fn().mockImplementation((k) => {
37                     return { validationPatterns};
38                 } )
39             };
40
41             const configure: ConfigureFn = (testBed) => {
42                 testBed.configureTestingModule({
43                     declarations: [AttributeModalComponent, TranslatePipe],
44                     imports: [],
45                     schemas: [NO_ERRORS_SCHEMA],
46                     providers: [
47                         {provide: TranslateService, useValue: translateServiceMock},
48                         {provide: CacheService, useValue: cacheServiceMock},
49                     ]
50                 });
51             };
52
53             configureTests(configure).then((testBed) => {
54                 fixture = testBed.createComponent(AttributeModalComponent);
55             });
56         })
57     );
58
59     it('test that when type is set to boolean default value is cleared', async () => {
60         const component = fixture.componentInstance;
61         component.attributeToEdit = new AttributeModel();
62         component.ngOnInit();
63
64         component.onTypeSelected({ value : 'string', label : 'string'});
65         component.attributeToEdit._default = 'some_value';
66         component.onTypeSelected({ value : 'boolean', label : 'boolean'});
67         expect(component.attributeToEdit._default).toBe('');
68
69         component.onBooleanDefaultValueSelected({ value : 'true', label : 'true'});
70         expect(component.attributeToEdit._default).toBe('true');
71     });
72
73     it('test that when certain type is selected, the correct regex pattern is chosen', async () => {
74         const component = fixture.componentInstance;
75         component.attributeToEdit = new AttributeModel();
76         component.ngOnInit();
77
78         // integer
79         component.onTypeSelected({ value : 'integer', label : 'integer'});
80         expect(component.defaultValuePattern).toBe(validationPatterns.integerNoLeadingZero);
81
82         // float
83         component.onTypeSelected({ value : 'float', label : 'float'});
84         expect(component.defaultValuePattern).toBe(validationPatterns.number);
85
86         // list is chosen with no schema, regex pattern is set to default
87         component.onTypeSelected({ value : 'list', label : 'list'});
88         expect(component.defaultValuePattern).toEqual('.*');
89
90         // schema is set to list of int
91         component.onEntrySchemaTypeSelected({ value : 'integer', label : 'integer' });
92         expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyListPatterns().integer);
93
94         // schema is set to list of float
95         component.onEntrySchemaTypeSelected({ value : 'float', label : 'float' });
96         expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyListPatterns().float);
97
98         // map is selected (float schema is still selected from previous line)
99         component.onTypeSelected({ value : 'map', label : 'map'});
100         expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyMapPatterns().float);
101
102         // change schema type to boolean
103         component.onEntrySchemaTypeSelected({ value : 'boolean', label : 'boolean' });
104     });
105
106     it('should detect map with non-unique keys', async () => {
107         const component = fixture.componentInstance;
108         component.attributeToEdit = new AttributeModel();
109         component.ngOnInit();
110         expect(component.isMapUnique()).toBe(true); // map is not selected so return true by default
111         component.onTypeSelected({ value : 'map', label : 'map'});
112         component.onEntrySchemaTypeSelected({ value : 'boolean', label : 'boolean' });
113         component.attributeToEdit._default = '"1":true,"2":false';
114         expect(component.isMapUnique()).toBe(true);
115         component.attributeToEdit._default = '"1":true,"1":false';
116         expect(component.isMapUnique()).toBe(false);
117     });
118 });