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';
11 describe('attributes modal component', () => {
13 let fixture: ComponentFixture<AttributeModalComponent>;
16 let translateServiceMock: Partial<TranslateService>;
17 let cacheServiceMock: Partial<CacheService>;
19 const validationPatterns = {
20 integerNoLeadingZero : 'int_regx',
21 number : 'number_regx'
24 const newAttribute = {
25 uniqueId: '1', name: 'attr1', description: 'description1', type: 'string', hidden: false, defaultValue: 'val1', schema: null
31 translateServiceMock = {
36 get: jest.fn().mockImplementation((k) => {
37 return { validationPatterns};
41 const configure: ConfigureFn = (testBed) => {
42 testBed.configureTestingModule({
43 declarations: [AttributeModalComponent, TranslatePipe],
45 schemas: [NO_ERRORS_SCHEMA],
47 {provide: TranslateService, useValue: translateServiceMock},
48 {provide: CacheService, useValue: cacheServiceMock},
53 configureTests(configure).then((testBed) => {
54 fixture = testBed.createComponent(AttributeModalComponent);
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();
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('');
69 component.onBooleanDefaultValueSelected({ value : 'true', label : 'true'});
70 expect(component.attributeToEdit._default).toBe('true');
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();
79 component.onTypeSelected({ value : 'integer', label : 'integer'});
80 expect(component.defaultValuePattern).toBe(validationPatterns.integerNoLeadingZero);
83 component.onTypeSelected({ value : 'float', label : 'float'});
84 expect(component.defaultValuePattern).toBe(validationPatterns.number);
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('.*');
90 // schema is set to list of int
91 component.onEntrySchemaTypeSelected({ value : 'integer', label : 'integer' });
92 expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyListPatterns().integer);
94 // schema is set to list of float
95 component.onEntrySchemaTypeSelected({ value : 'float', label : 'float' });
96 expect(component.defaultValuePattern).toEqual(ValidationUtils.getPropertyListPatterns().float);
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);
102 // change schema type to boolean
103 component.onEntrySchemaTypeSelected({ value : 'boolean', label : 'boolean' });
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);