Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / deployment / panel / panel-tabs / hierarchy-tab / hierarchy-tab.component.spec.ts
1 import {async, ComponentFixture} from "@angular/core/testing";
2 import {HierarchyTabComponent} from "./hierarchy-tab.component";
3 import {ConfigureFn, configureTests} from "../../../../../../../../jest/test-config.helper";
4 import {NO_ERRORS_SCHEMA} from "@angular/core";
5 import {TranslateModule} from "../../../../../../shared/translator/translate.module";
6 import {TopologyTemplateService} from "../../../../../../services/component-services/topology-template.service";
7 import {WorkspaceService} from "../../../../workspace.service";
8 import {ModulesService} from "../../../../../../services/modules.service";
9 import {GlobalPipesModule} from "../../../../../../pipes/global-pipes.module";
10 import {TranslateService} from "../../../../../../shared/translator/translate.service";
11 import {ModalsHandler} from "../../../../../../../utils/modals-handler";
12 import {ComponentFactory} from "../../../../../../../utils/component-factory";
13 import {NgxsModule} from "@ngxs/store";
14 import {  SdcUiServices } from "onap-ui-angular";
15 import {Observable} from "rxjs";
16 import {DisplayModule, Module} from "../../../../../../../models/modules/base-module";
17 import {DeploymentGraphService} from "../../../../../composition/deployment/deployment-graph.service";
18 import {ComponentMetadata} from "../../../../../../../models/component-metadata";
19
20 describe('HierarchyTabComponent', () => {
21
22     let fixture: ComponentFixture<HierarchyTabComponent>;
23     let workspaceService: Partial<WorkspaceService>;
24     let popoverServiceMock: Partial<SdcUiServices.PopoverService>;
25     let modulesServiceMock: Partial<ModulesService>;
26
27     let editModuleNameInstanceMock = {innerPopoverContent:{instance: { clickButtonEvent: Observable.of("new heat name")}},
28         closePopover: jest.fn()};
29     let eventMock  = {x: 1650, y: 350};
30     let moduleMock: Array<Module> = [{name: "NewVf2..base_vepdg..module-0", uniqueId: '1'}];
31     let selectedModuleMock: DisplayModule = {name: "NewVf2..base_vepdg..module-0", vfInstanceName: "NewVf2", moduleName:"module-0",
32                     heatName: "base_vepdg", uniqueId: '1', updateName: jest.fn().mockImplementation(() => {
33                     selectedModuleMock.name = selectedModuleMock.vfInstanceName + '..' + selectedModuleMock.heatName + '..' +
34                     selectedModuleMock.moduleName;})}
35     let updateSelectedModuleMock = () => {
36         selectedModuleMock.heatName = "base_vepdg";
37         selectedModuleMock.name = "NewVf2..base_vepdg..module-0";
38         fixture.componentInstance.selectedModule = selectedModuleMock;
39         fixture.componentInstance.modules = moduleMock;
40     }
41     beforeEach(
42         async(() => {
43
44             workspaceService ={
45                 metadata:  <ComponentMetadata> {
46                     name: '',
47                     componentType: ''
48                 }
49             }
50             popoverServiceMock = {
51                 createPopOverWithInnerComponent: jest.fn().mockImplementation(() => {return editModuleNameInstanceMock})
52             }
53             modulesServiceMock = {
54                 updateModuleMetadata: jest.fn().mockReturnValue(Observable.of({}))
55             }
56
57             const configure: ConfigureFn = testBed => {
58                 testBed.configureTestingModule({
59                     declarations: [HierarchyTabComponent],
60                     schemas: [NO_ERRORS_SCHEMA],
61                     imports: [TranslateModule, NgxsModule.forRoot([]), GlobalPipesModule],
62                     providers: [
63                         {provide: DeploymentGraphService, useValue: {}},
64                         {provide: ComponentFactory, useValue: {}},
65                         {provide: TopologyTemplateService, useValue: {}},
66                         {provide: WorkspaceService, useValue: workspaceService},
67                         {provide: ModulesService, useValue: modulesServiceMock},
68                         {provide: TranslateService, useValue: {}},
69                         {provide: ModalsHandler, useValue: {}},
70                         {provide: SdcUiServices.PopoverService, useValue: popoverServiceMock}
71                     ]
72                 });
73             };
74
75             configureTests(configure).then(testBed => {
76                 fixture = testBed.createComponent(HierarchyTabComponent);
77             });
78         })
79     );
80
81     it('expected heirarchy component to be defined', () => {
82         expect(fixture).toBeDefined();
83     });
84
85     it('Update heat name and name sucessfully', () => {
86         updateSelectedModuleMock();
87         fixture.componentInstance.openEditModuleNamePopup(eventMock);
88         expect(fixture.componentInstance.selectedModule.updateName).toHaveBeenCalled();
89         expect(modulesServiceMock.updateModuleMetadata).toHaveBeenCalled();
90         expect(fixture.componentInstance.selectedModule.name).toEqual('NewVf2..new heat name..module-0');
91         expect(fixture.componentInstance.modules[0].name).toEqual('NewVf2..new heat name..module-0');
92         expect(fixture.componentInstance.selectedModule.heatName).toEqual('new heat name');
93     })
94     it('Try to update heat name and name and get error from server', () => {
95         updateSelectedModuleMock();
96         modulesServiceMock.updateModuleMetadata.mockImplementation(() => Observable.throwError({}));
97         fixture.componentInstance.openEditModuleNamePopup(eventMock);
98         expect(fixture.componentInstance.selectedModule.updateName).toHaveBeenCalled();
99         expect(modulesServiceMock.updateModuleMetadata).toHaveBeenCalled();
100         expect(fixture.componentInstance.modules[0].name).toEqual('NewVf2..base_vepdg..module-0');
101         expect(fixture.componentInstance.selectedModule.heatName).toEqual('base_vepdg');
102         expect(fixture.componentInstance.selectedModule.name).toEqual('NewVf2..base_vepdg..module-0');
103     })
104     it('Try to update heat name and name but not find the module with the same uniqueId', () => {
105         selectedModuleMock.uniqueId = '2'
106         updateSelectedModuleMock();
107         fixture.componentInstance.openEditModuleNamePopup(eventMock);
108         expect(fixture.componentInstance.selectedModule.updateName).toHaveBeenCalled();
109         expect(modulesServiceMock.updateModuleMetadata).not.toHaveBeenCalled();
110         expect(fixture.componentInstance.modules[0].name).toEqual('NewVf2..base_vepdg..module-0');
111         expect(fixture.componentInstance.selectedModule.heatName).toEqual('base_vepdg');
112         expect(fixture.componentInstance.selectedModule.name).toEqual('NewVf2..base_vepdg..module-0');
113         selectedModuleMock.uniqueId = '1'
114     })
115     it('Open edit  module name popover and change the heat name', () => {
116         updateSelectedModuleMock();
117         spyOn(fixture.componentInstance, 'updateHeatName');
118         spyOn(fixture.componentInstance, 'updateOriginalHeatName');
119         fixture.componentInstance.openEditModuleNamePopup(eventMock);
120         expect(popoverServiceMock.createPopOverWithInnerComponent).toHaveBeenCalled();
121         expect(fixture.componentInstance.selectedModule.heatName).toEqual("new heat name");
122         expect(fixture.componentInstance.updateHeatName).toHaveBeenCalled();
123     })
124
125
126     it('Open edit  module name popover and not change the heat name', () => {
127         updateSelectedModuleMock();
128         editModuleNameInstanceMock.innerPopoverContent.instance.clickButtonEvent = Observable.of(null);
129         fixture.componentInstance.openEditModuleNamePopup(eventMock);
130         expect(popoverServiceMock.createPopOverWithInnerComponent).toHaveBeenCalled();
131         expect(fixture.componentInstance.selectedModule.heatName).toEqual("base_vepdg");
132     })
133 });