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';
15 describe('attributes component', () => {
17 let fixture: ComponentFixture<AttributesComponent>;
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>;
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},
33 const newAttribute = {
34 uniqueId : '5', name : 'attr5', description: 'description5', type : 'string', hidden : false, defaultValue : 'val5', schema : null
36 const updatedAttribute = {
37 uniqueId : '2', name : 'attr2', description: 'description_new', type : 'string', hidden : false, defaultValue : 'new_val2', schema : null
39 const errorAttribute = {
40 uniqueId : '99', name : 'attr99', description: 'description_error', type : 'string', hidden : false, defaultValue : 'error', schema : null
46 componentMetadataMock = new ComponentMetadata();
47 componentMetadataMock.uniqueId = 'fake';
48 componentMetadataMock.componentType = 'VL';
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();
57 return Observable.of(newAttribute).toPromise();
61 updateAttributeAsync: jest.fn().mockImplementation(
62 (compType, cUid, attr) => {
63 if (attr === errorAttribute) {
64 return Observable.throwError('update_error').toPromise();
66 return Observable.of(updatedAttribute).toPromise();
70 deleteAttributeAsync: jest.fn().mockImplementation((cid, ctype, attr) => Observable.of(attr))
73 workspaceServiceMock = {
74 metadata: componentMetadataMock
77 const customModalInstance = { innerModalContent: { instance: { onValidationChange: { subscribe: jest.fn()}}}};
80 openInfoModal: jest.fn(),
81 openCustomModal: jest.fn().mockImplementation(() => customModalInstance)
89 const configure: ConfigureFn = (testBed) => {
90 testBed.configureTestingModule({
91 declarations: [AttributesComponent],
92 imports: [NgxDatatableModule],
93 schemas: [NO_ERRORS_SCHEMA],
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 }
105 configureTests(configure).then((testBed) => {
106 fixture = testBed.createComponent(AttributesComponent);
111 it('should see exactly 1 attributes on init', async () => {
112 await fixture.componentInstance.asyncInitComponent();
113 expect(fixture.componentInstance.getAttributes().length).toEqual(4);
116 it('should see exactly 5 attributes when adding', async () => {
117 await fixture.componentInstance.asyncInitComponent();
118 expect(fixture.componentInstance.getAttributes().length).toEqual(4);
120 await fixture.componentInstance.addOrUpdateAttribute(newAttribute, false);
121 expect(fixture.componentInstance.getAttributes().length).toEqual(5);
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);
134 it('should see updated attribute', async () => {
135 await fixture.componentInstance.asyncInitComponent();
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;
142 expect(attribute.description).toEqual( 'description_new');
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);
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);
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();
169 it('on add modal shell be opened', async () => {
170 await fixture.componentInstance.asyncInitComponent();
171 fixture.componentInstance.onAddAttribute();
172 expect(modalServiceMock.openCustomModal).toHaveBeenCalled();
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();