1 import { ComponentFixture, TestBed, async } from '@angular/core/testing';
2 import { NO_ERRORS_SCHEMA } from '@angular/core';
3 import { SimpleChanges } from '@angular/core';
4 import { PoliciesService } from 'app/ng2/services/policies.service';
5 import { GroupsService } from 'app/ng2/services/groups.service';
6 import { EventListenerService } from 'app/services';
7 import { Store } from '@ngxs/store';
8 import { CompositionService } from 'app/ng2/pages/composition/composition.service';
9 import { ZoneInstanceComponent } from './zone-instance.component';
10 import { ZoneInstanceType, ZoneInstance, ZoneInstanceMode, ZoneInstanceAssignmentType, IZoneInstanceAssignment } from "app/models";
11 import { PolicyInstance } from "app/models/graph/zones/policy-instance";
12 import { Subject, of } from 'rxjs';
13 import { _throw } from 'rxjs/observable/throw';
15 describe('ZoneInstanceComponent', () => {
16 let component: ZoneInstanceComponent;
17 let fixture: ComponentFixture<ZoneInstanceComponent>;
19 let createPolicyInstance = () => {
20 let policy = new PolicyInstance();
21 policy.targets = {COMPONENT_INSTANCES: [], GROUPS: []};
22 return new ZoneInstance(policy, '', '');
26 const policiesServiceStub = {updateZoneInstanceAssignments : jest.fn()};
27 const groupsServiceStub = {};
28 const eventListenerServiceStub = {};
30 const compositionServiceStub = {};
31 TestBed.configureTestingModule({
32 schemas: [NO_ERRORS_SCHEMA],
33 declarations: [ZoneInstanceComponent],
35 { provide: PoliciesService, useValue: policiesServiceStub },
36 { provide: GroupsService, useValue: groupsServiceStub },
37 { provide: EventListenerService, useValue: eventListenerServiceStub },
38 { provide: Store, useValue: storeStub },
39 { provide: CompositionService, useValue: compositionServiceStub }
41 }).compileComponents().then(() => {
42 fixture = TestBed.createComponent(ZoneInstanceComponent);
43 component = fixture.componentInstance;
47 it('can load instance', async((done) => {
48 component.zoneInstance = <ZoneInstance>{type : ZoneInstanceType.POLICY, instanceData: {name: 'test policy'}, assignments: []};
49 component.forceSave = new Subject<Function>();
50 fixture.detectChanges();
51 expect(component).toBeTruthy();
55 it('if another instance is already tagging, i cannot change my mode', ()=> {
56 component.zoneInstance = <ZoneInstance>{ mode: ZoneInstanceMode.NONE };
57 component.isActive = false;
58 component.activeInstanceMode = ZoneInstanceMode.TAG;
59 component.setMode(ZoneInstanceMode.SELECTED);
60 expect(component.zoneInstance.mode).toBe(ZoneInstanceMode.NONE);
63 it('if i am active(selected) and NOT in tag mode, I can set another mode', ()=> {
64 component.isActive = true;
65 component.zoneInstance = <ZoneInstance>{ mode: ZoneInstanceMode.SELECTED };
66 jest.spyOn(component.modeChange, 'emit');
67 component.setMode(ZoneInstanceMode.NONE);
68 expect(component.modeChange.emit).toHaveBeenCalledWith({instance: component.zoneInstance, newMode: ZoneInstanceMode.NONE });
71 it('if i am active and in tag mode and i try to set mode other than tag, I am not allowed', ()=> {
72 component.isActive = true;
73 component.zoneInstance = <ZoneInstance>{ mode: ZoneInstanceMode.TAG };
74 component.setMode(ZoneInstanceMode.SELECTED);
75 expect(component.zoneInstance.mode).toBe(ZoneInstanceMode.TAG);
78 it('if i am active and in tag mode and click tag again and no changes, does NOT call save, but DOES turn tagging off', ()=> {
79 component.isActive = true;
80 component.zoneInstance = createPolicyInstance();
81 component.zoneService = component.policiesService;
82 component.zoneInstance.mode = ZoneInstanceMode.TAG;
83 jest.spyOn(component.zoneService, 'updateZoneInstanceAssignments');
84 jest.spyOn(component.modeChange, 'emit');
86 component.setMode(ZoneInstanceMode.TAG);
88 expect(component.zoneService.updateZoneInstanceAssignments).not.toHaveBeenCalled();
89 expect(component.modeChange.emit).toHaveBeenCalledWith({instance: component.zoneInstance, newMode: ZoneInstanceMode.NONE });
92 it('if i am active and in tag mode and click tag again and HAVE changes, calls save AND turns tagging off', ()=> {
93 component.isActive = true;
94 component.zoneInstance = createPolicyInstance();
95 component.zoneService = component.policiesService;
96 component.zoneInstance.mode = ZoneInstanceMode.TAG;
97 component.zoneInstance.assignments.push(<IZoneInstanceAssignment>{uniqueId: '123', type: ZoneInstanceAssignmentType.COMPONENT_INSTANCES});
98 jest.spyOn(component.zoneService, 'updateZoneInstanceAssignments').mockReturnValue(of(true));
99 jest.spyOn(component.modeChange, 'emit');
101 component.setMode(ZoneInstanceMode.TAG);
103 expect(component.zoneService.updateZoneInstanceAssignments).toHaveBeenCalled();
104 expect(component.modeChange.emit).toHaveBeenCalledWith({instance: component.zoneInstance, newMode: ZoneInstanceMode.NONE });
107 it('on save error, temporary assignment list is reverted to saved assignments', ()=> {
108 component.isActive = true;
109 component.zoneInstance = createPolicyInstance();
110 component.zoneService = component.policiesService;
111 component.zoneInstance.mode = ZoneInstanceMode.TAG;
112 component.zoneInstance.assignments.push(<IZoneInstanceAssignment>{uniqueId: '123', type: ZoneInstanceAssignmentType.COMPONENT_INSTANCES});
113 jest.spyOn(component.zoneService, 'updateZoneInstanceAssignments').mockReturnValue(_throw({status: 404}));
115 component.setMode(ZoneInstanceMode.TAG);
117 expect(component.zoneInstance.assignments.length).toEqual(0);
120 it('on save success, all changes are saved to zoneInstance.savedAssignments', ()=> {
121 component.isActive = true;
122 component.zoneInstance = createPolicyInstance();
123 component.zoneService = component.policiesService;
124 component.zoneInstance.mode = ZoneInstanceMode.TAG;
125 component.zoneInstance.assignments.push(<IZoneInstanceAssignment>{uniqueId: '123', type: ZoneInstanceAssignmentType.COMPONENT_INSTANCES});
126 jest.spyOn(component.zoneService, 'updateZoneInstanceAssignments').mockReturnValue(of(true));
128 component.setMode(ZoneInstanceMode.TAG);
130 expect(component.zoneInstance.instanceData.getSavedAssignments().length).toEqual(1);