Catalog alignment
[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 hidden is clicked, hidden attribute is set', async () => {
60         fixture.componentInstance.attributeToEdit = new AttributeModel();
61         const hidden = fixture.componentInstance.attributeToEdit.hidden;
62         fixture.componentInstance.ngOnInit();
63
64         expect(hidden).toBe(false);
65         fixture.componentInstance.onHiddenCheckboxClicked(true);
66         expect(fixture.componentInstance.attributeToEdit.hidden).toBe(true);
67     });
68
69     it('test that when type is set to boolean default value is cleared', async () => {
70         const component = fixture.componentInstance;
71         component.attributeToEdit = new AttributeModel();
72         component.ngOnInit();
73
74         component.onTypeSelected({ value : 'string', label : 'string'});
75         component.attributeToEdit.defaultValue = 'some_value';
76         component.onTypeSelected({ value : 'boolean', label : 'boolean'});
77         expect(component.attributeToEdit.defaultValue).toBe('');
78
79         component.onBooleanDefaultValueSelected({ value : 'true', label : 'true'});
80         expect(component.attributeToEdit.defaultValue).toBe('true');
81     });
82
83     it('test that when certain type is selected, the correct regex pattern is chosen', async () => {
84         const component = fixture.componentInstance;
85         component.attributeToEdit = new AttributeModel();
86         component.ngOnInit();
87
88         // integer
89         component.onTypeSelected({ value : 'integer', label : 'integer'});
90         expect(component.defaultValuePattern).toBe(validationPatterns.integerNoLeadingZero);
91
92         // float
93         component.onTypeSelected({ value : 'float', label : 'float'});
94         expect(component.defaultValuePattern).toBe(validationPatterns.number);
95
96         // list is chosen with no schema, regex pattern is set to default
97         component.onTypeSelected({ value : 'list', label : 'list'});
98         expect(component.defaultValuePattern).toEqual('.*');
99
100         // schema is set to list of int
101         component.onEntrySchemaTypeSelected({ value : 'integer', label : 'integer' });
102         expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyListPatterns().integer);
103
104         // schema is set to list of float
105         component.onEntrySchemaTypeSelected({ value : 'float', label : 'float' });
106         expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyListPatterns().float);
107
108         // map is selected (float schema is still selected from previous line)
109         component.onTypeSelected({ value : 'map', label : 'map'});
110         expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyMapPatterns().float);
111
112         // change schema type to boolean
113         component.onEntrySchemaTypeSelected({ value : 'boolean', label : 'boolean' });
114     });
115
116     it('should detect map with non-unique keys', async () => {
117         const component = fixture.componentInstance;
118         component.attributeToEdit = new AttributeModel();
119         component.ngOnInit();
120         expect(component.isMapUnique()).toBe(true); // map is not selected so return true by default
121         component.onTypeSelected({ value : 'map', label : 'map'});
122         component.onEntrySchemaTypeSelected({ value : 'boolean', label : 'boolean' });
123         component.attributeToEdit.defaultValue = '"1":true,"2":false';
124         expect(component.isMapUnique()).toBe(true);
125         component.attributeToEdit.defaultValue = '"1":true,"1":false';
126         expect(component.isMapUnique()).toBe(false);
127     });
128 });