summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/panel/panel-header/panel-header.component.spec.ts
blob: 34a25298fe4a3eb5e2a8ffff06be7521a658310a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {NO_ERRORS_SCHEMA} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {CompositionService} from 'app/ng2/pages/composition/composition.service';
import {EventListenerService} from '../../../../../services/event-listener-service';
import {ComponentInstanceServiceNg2} from 'app/ng2/services/component-instance-services/component-instance.service';
import {WorkspaceService} from 'app/ng2/pages/workspace/workspace.service';
import {GroupsService, PoliciesService} from 'app/services-ng2';
import {CompositionPanelHeaderComponent} from './panel-header.component';
import {SdcUiServices} from 'onap-ui-angular';
import {
    CapabilitiesGroup,
    Capability,
    Component,
    ComponentInstance,
    FullComponentInstance,
    GroupInstance,
    PolicyInstance,
    Requirement,
    RequirementsGroup
} from "app/models";
import {Observable, of} from "rxjs";

describe('CompositionPanelHeaderComponent', () => {
    let component: CompositionPanelHeaderComponent;
    let fixture: ComponentFixture<CompositionPanelHeaderComponent>;
    const componentInstanceServiceNg2Stub = {
        updateComponentInstance: jest.fn()
    };
    const valueEditModalInstance = {
        innerModalContent: {
            instance: {name: "VF Test"}
        },
        buttons: [{id: 'saveButton', text: 'OK', size: 'xsm', callback: jest.fn(), closeModal: false}],
        closeModal: jest.fn()
    };

    beforeEach(
        () => {
            const compositionServiceStub = {};
            const eventListenerServiceStub = {};

            const workspaceServiceStub = {
                metadata: {
                    componentType: "SERVICE",
                    uniqueId: "123"
                }
            };
            const groupsServiceStub = {
                updateName: jest.fn()
            };
            const policiesServiceStub = {
                updateName: jest.fn()
            };

            TestBed.configureTestingModule({
                schemas: [NO_ERRORS_SCHEMA],
                declarations: [CompositionPanelHeaderComponent],
                providers: [
                    {provide: CompositionService, useValue: compositionServiceStub},
                    {provide: EventListenerService, useValue: eventListenerServiceStub},
                    {
                        provide: ComponentInstanceServiceNg2,
                        useValue: componentInstanceServiceNg2Stub
                    },
                    {provide: WorkspaceService, useValue: workspaceServiceStub},
                    {provide: GroupsService, useValue: groupsServiceStub},
                    {provide: PoliciesService, useValue: policiesServiceStub},
                    {provide: SdcUiServices.ModalService, useValue: {}}
                ]
            });
            fixture = TestBed.createComponent(CompositionPanelHeaderComponent);
            component = fixture.componentInstance;
        }
    );

    it('can load instance', () => {
        expect(component).toBeTruthy();
    });

    it('should close the modal without saving if the name has not changed', () => {
        component.selectedComponent = <FullComponentInstance>{name: "VF Test"};
        component.valueEditModalInstance = valueEditModalInstance;

        component.saveInstanceName();
        expect(component.componentInstanceService.updateComponentInstance).not.toHaveBeenCalled();
        expect(component.valueEditModalInstance.closeModal).toHaveBeenCalled();
    });

    it('after editing instance name, capabilities/requirements should be updated with new name', () => {
        const newName = "New VF NAME";
        component.selectedComponent = new FullComponentInstance(<ComponentInstance>{
            name: "VF Test",
            requirements: new RequirementsGroup(),
            capabilities: new CapabilitiesGroup()
        }, <Component>{});
        component.selectedComponent.requirements['key'] = [<Requirement>{ownerName: "VF Test"}, <Requirement>{ownerName: "VF Test"}];
        component.selectedComponent.capabilities['key'] = [<Capability>{ownerName: "VF Test"}];
        component.valueEditModalInstance = valueEditModalInstance;
        component.valueEditModalInstance.innerModalContent.instance.name = newName;
        jest.spyOn(component.componentInstanceService, 'updateComponentInstance').mockReturnValue(of(<ComponentInstance>{name: newName}));
        component.saveInstanceName();

        expect(component.selectedComponent.name).toBe(newName);
        expect(component.selectedComponent.requirements['key'][0].ownerName).toEqual(newName);
        expect(component.selectedComponent.requirements['key'][1].ownerName).toEqual(newName);
        expect(component.selectedComponent.capabilities['key'][0].ownerName).toEqual(newName);
    });

    it('if update fails, name is reverted to old value', () => {
        component.selectedComponent = new GroupInstance(<GroupInstance>{name: "GROUP NAME"});
        component.valueEditModalInstance = valueEditModalInstance;
        jest.spyOn(component.groupService, 'updateName').mockReturnValue(Observable.throw(new Error('Error')));
        component.saveInstanceName();
        expect(component.selectedComponent.name).toEqual("GROUP NAME");
    });

    it('policy instance uses policies service for update name', () => {
        component.selectedComponent = new PolicyInstance(<PolicyInstance>{name: "Policy OLD NAME"});
        component.valueEditModalInstance = valueEditModalInstance;
        jest.spyOn(component.policiesService, 'updateName').mockReturnValue(of(true));
        component.saveInstanceName();
        expect(component.policiesService.updateName).toHaveBeenCalledTimes(1);
    });

    it('group instance uses groups service for update name', () => {
        component.selectedComponent = new GroupInstance(<GroupInstance>{name: "GROUP NAME"});
        component.valueEditModalInstance = valueEditModalInstance;
        jest.spyOn(component.groupService, 'updateName').mockReturnValue(of(true));
        component.saveInstanceName();
        expect(component.groupService.updateName).toHaveBeenCalledTimes(1);
    });

});