Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / attributes / attributes.component.spec.ts
1 import { NO_ERRORS_SCHEMA } from '@angular/core';
2 import { async, ComponentFixture } from '@angular/core/testing';
3 import { NgxDatatableModule } from '@swimlane/ngx-datatable';
4 import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
5 import 'rxjs/add/observable/of';
6 import { Observable } from 'rxjs/Rx';
7 import { ConfigureFn, configureTests } from '../../../../../jest/test-config.helper';
8 import { ComponentMetadata } from '../../../../models/component-metadata';
9 import { ModalsHandler } from '../../../../utils';
10 import { TopologyTemplateService } from '../../../services/component-services/topology-template.service';
11 import { TranslateService } from '../../../shared/translator/translate.service';
12 import { WorkspaceService } from '../workspace.service';
13 import { AttributesComponent } from './attributes.component';
14
15 describe('attributes component', () => {
16
17     let fixture: ComponentFixture<AttributesComponent>;
18
19     // Mocks
20     let workspaceServiceMock: Partial<WorkspaceService>;
21     let topologyTemplateServiceMock: Partial<TopologyTemplateService>;
22     let loaderServiceMock: Partial<SdcUiServices.LoaderService>;
23     let componentMetadataMock: ComponentMetadata;
24     let modalServiceMock: Partial<SdcUiServices.ModalService>;
25
26     const mockAttributesList = [
27         { uniqueId: '1', name: 'attr1', description: 'description1', type: 'string', hidden: false, defaultValue: 'val1', schema: null },
28         { uniqueId : '2', name : 'attr2', description: 'description2', type : 'int', hidden : false, defaultValue : 1, schema : null},
29         { uniqueId : '3', name : 'attr3', description: 'description3', type : 'double', hidden : false, defaultValue : 1.0, schema : null},
30         { uniqueId : '4', name : 'attr4', description: 'description4', type : 'boolean', hidden : false, defaultValue : true, schema : null},
31     ];
32
33     const newAttribute = {
34         uniqueId : '5', name : 'attr5', description: 'description5', type : 'string', hidden : false, defaultValue : 'val5', schema : null
35     };
36     const updatedAttribute = {
37         uniqueId : '2', name : 'attr2', description: 'description_new', type : 'string', hidden : false, defaultValue : 'new_val2', schema : null
38     };
39     const errorAttribute = {
40         uniqueId : '99', name : 'attr99', description: 'description_error', type : 'string', hidden : false, defaultValue : 'error', schema : null
41     };
42
43     beforeEach(
44         async(() => {
45
46             componentMetadataMock = new ComponentMetadata();
47             componentMetadataMock.uniqueId = 'fake';
48             componentMetadataMock.componentType = 'VL';
49
50             topologyTemplateServiceMock = {
51                 getComponentAttributes: jest.fn().mockResolvedValue({ attributes : mockAttributesList }),
52                 addAttributeAsync: jest.fn().mockImplementation(
53                     (compType, cUid, attr) => {
54                         if (attr === errorAttribute) {
55                             return Observable.throwError('add_error').toPromise();
56                         } else {
57                             return Observable.of(newAttribute).toPromise();
58                         }
59                     }
60                 ),
61                 updateAttributeAsync: jest.fn().mockImplementation(
62                     (compType, cUid, attr) => {
63                         if (attr === errorAttribute) {
64                             return Observable.throwError('update_error').toPromise();
65                         } else {
66                             return Observable.of(updatedAttribute).toPromise();
67                         }
68                     }
69                 ),
70                 deleteAttributeAsync: jest.fn().mockImplementation((cid, ctype, attr) => Observable.of(attr))
71             };
72
73             workspaceServiceMock = {
74                 metadata: componentMetadataMock
75             };
76
77             const customModalInstance = { innerModalContent: { instance: { onValidationChange: { subscribe: jest.fn()}}}};
78
79             modalServiceMock = {
80                 openInfoModal: jest.fn(),
81                 openCustomModal: jest.fn().mockImplementation(() => customModalInstance)
82             };
83
84             loaderServiceMock = {
85                 activate: jest.fn(),
86                 deactivate: jest.fn()
87             };
88
89             const configure: ConfigureFn = (testBed) => {
90                 testBed.configureTestingModule({
91                     declarations: [AttributesComponent],
92                     imports: [NgxDatatableModule],
93                     schemas: [NO_ERRORS_SCHEMA],
94                     providers: [
95                         {provide: WorkspaceService, useValue: workspaceServiceMock},
96                         {provide: TopologyTemplateService, useValue: topologyTemplateServiceMock},
97                         {provide: ModalsHandler, useValue: {}},
98                         {provide: TranslateService, useValue: { translate: jest.fn() }},
99                         {provide: SdcUiServices.ModalService, useValue: modalServiceMock },
100                         {provide: SdcUiServices.LoaderService, useValue: loaderServiceMock }
101                     ],
102                 });
103             };
104
105             configureTests(configure).then((testBed) => {
106                 fixture = testBed.createComponent(AttributesComponent);
107             });
108         })
109     );
110
111     it('should see exactly 1 attributes on init', async () => {
112         await fixture.componentInstance.asyncInitComponent();
113         expect(fixture.componentInstance.getAttributes().length).toEqual(4);
114     });
115
116     it('should see exactly 5 attributes when adding', async () => {
117         await fixture.componentInstance.asyncInitComponent();
118         expect(fixture.componentInstance.getAttributes().length).toEqual(4);
119
120         await fixture.componentInstance.addOrUpdateAttribute(newAttribute, false);
121         expect(fixture.componentInstance.getAttributes().length).toEqual(5);
122     });
123
124     it('should see exactly 3 attributes when deleting', async () => {
125         await fixture.componentInstance.asyncInitComponent();
126         expect(fixture.componentInstance.getAttributes().length).toEqual(4);
127         const attrToDelete = mockAttributesList[0];
128         expect(fixture.componentInstance.getAttributes().filter((attr) => attr.uniqueId === attrToDelete.uniqueId).length).toEqual(1);
129         await fixture.componentInstance.deleteAttribute(attrToDelete);
130         expect(fixture.componentInstance.getAttributes().length).toEqual(3);
131         expect(fixture.componentInstance.getAttributes().filter((attr) => attr.uniqueId === attrToDelete.uniqueId).length).toEqual(0);
132     });
133
134     it('should see updated attribute', async () => {
135         await fixture.componentInstance.asyncInitComponent();
136
137         await fixture.componentInstance.addOrUpdateAttribute(updatedAttribute, true);
138         expect(fixture.componentInstance.getAttributes().length).toEqual(4);
139         const attribute = fixture.componentInstance.getAttributes().filter( (attr) => {
140             return attr.uniqueId === updatedAttribute.uniqueId;
141         })[0];
142         expect(attribute.description).toEqual( 'description_new');
143     });
144
145     it('Add fails, make sure loader is deactivated and attribute is not added', async () => {
146         await fixture.componentInstance.asyncInitComponent();
147         const numAttributes = fixture.componentInstance.getAttributes().length;
148         await fixture.componentInstance.addOrUpdateAttribute(errorAttribute, false); // Add
149         expect(loaderServiceMock.deactivate).toHaveBeenCalled();
150         expect(fixture.componentInstance.getAttributes().length).toEqual(numAttributes);
151     });
152
153     it('Update fails, make sure loader is deactivated', async () => {
154         await fixture.componentInstance.asyncInitComponent();
155         const numAttributes = fixture.componentInstance.getAttributes().length;
156         await fixture.componentInstance.addOrUpdateAttribute(errorAttribute, true); // Add
157         expect(loaderServiceMock.deactivate).toHaveBeenCalled();
158         expect(fixture.componentInstance.getAttributes().length).toEqual(numAttributes);
159     });
160
161     it('on delete modal shell be opened', async () => {
162         await fixture.componentInstance.asyncInitComponent();
163         const event = { stopPropagation: jest.fn() };
164         fixture.componentInstance.onDeleteAttribute(event, fixture.componentInstance.getAttributes()[0]);
165         expect(event.stopPropagation).toHaveBeenCalled();
166         expect(modalServiceMock.openInfoModal).toHaveBeenCalled();
167     });
168
169     it('on add modal shell be opened', async () => {
170         await fixture.componentInstance.asyncInitComponent();
171         fixture.componentInstance.onAddAttribute();
172         expect(modalServiceMock.openCustomModal).toHaveBeenCalled();
173     });
174
175     it('on edit modal shell be opened', async () => {
176         await fixture.componentInstance.asyncInitComponent();
177         const event = { stopPropagation: jest.fn() };
178         fixture.componentInstance.onEditAttribute(event, fixture.componentInstance.getAttributes()[0]);
179         expect(event.stopPropagation).toHaveBeenCalled();
180         expect(modalServiceMock.openCustomModal).toHaveBeenCalled();
181     });
182 });