Testing in Policy Control Component
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-type / policy-type.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
22
23 import { PolicyTypeComponent } from "./policy-type.component";
24 import { PolicyType, PolicyTypeSchema } from "@interfaces/policy.types";
25 import { PolicyService } from "@services/policy/policy.service";
26 import { of } from "rxjs";
27 import { MockComponent } from "ng-mocks";
28 import { PolicyInstanceComponent } from "../policy-instance/policy-instance.component";
29 import { By } from "@angular/platform-browser";
30 import { Component, SimpleChange, ViewChild } from '@angular/core';
31
32 describe("PolicyTypeComponent", () => {
33   let component: TestPolicyTypeHostComponent;
34   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
35   let fixture: ComponentFixture<TestPolicyTypeHostComponent>;
36
37   beforeEach(async(() => {
38     policyServiceSpy = jasmine.createSpyObj("PolicyService", ["getPolicyType"]);
39     const policyTypeSchema = JSON.parse(
40       '{"title": "1", "description": "Type 1 policy type"}'
41     );
42     const policyType = { policy_schema: policyTypeSchema } as PolicyType;
43     policyServiceSpy.getPolicyType.and.returnValue(of(policyType));
44
45     TestBed.configureTestingModule({
46       declarations: [
47         PolicyTypeComponent,
48         MockComponent(PolicyInstanceComponent),
49         TestPolicyTypeHostComponent,
50       ],
51       providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
52     }).compileComponents();
53   }));
54
55   beforeEach(() => {
56     fixture = TestBed.createComponent(TestPolicyTypeHostComponent);
57     component = fixture.componentInstance;
58     fixture.detectChanges();
59   });
60
61   it("should create", () => {
62     expect(component).toBeTruthy();
63   });
64
65   it("should not call service when no type, display correct type info and no PolicyInstanceComponent added", () => {
66     expect(policyServiceSpy.getPolicyType).not.toHaveBeenCalled();
67
68     expect(component.policyTypeComponent.policyType).toEqual("< No Type >");
69     expect(component.policyTypeComponent.policyDescription).toEqual("Type with no schema");
70
71     const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
72     expect(ele).toBeFalsy();
73 });
74
75   it("should call service when type, display correct type info and no PolicyInstanceComponent added", () => {
76     component.policyTypeComponent.policyTypeId = "type1";
77     component.policyTypeComponent.loadTypeInfo();
78
79     expect(policyServiceSpy.getPolicyType).toHaveBeenCalledWith("type1");
80
81     expect(component.policyTypeComponent.policyType).toEqual("type1");
82     expect(component.policyTypeComponent.policyDescription).toEqual("Type 1 policy type");
83
84     const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
85     expect(ele).toBeFalsy();
86   });
87
88   it("should add PolicyInstanceComponent with correct data when toggle visible to visible", async () => {
89     const ele = fixture.debugElement.nativeElement.querySelector("#visible");
90     expect(ele.innerText).toEqual("expand_more");
91
92     ele.click();
93     fixture.detectChanges();
94
95     expect(ele.innerText).toEqual("expand_less");
96
97     const policyInstanceComp: PolicyInstanceComponent = fixture.debugElement.query(
98       By.directive(PolicyInstanceComponent)
99     ).componentInstance;
100     expect(policyInstanceComp).toBeTruthy();
101     const expectedPolicyType = {
102       id: undefined,
103       name: undefined,
104       schemaObject: JSON.parse("{}")
105     } as PolicyTypeSchema;
106     expect(policyInstanceComp.policyTypeSchema).toEqual(expectedPolicyType);
107   });
108
109   it("should call ngOnChanges when minimiseTrigger is changed", async() => {
110     spyOn(component.policyTypeComponent, "ngOnChanges");
111     component.minimiseTrigger = !component.minimiseTrigger;
112     fixture.detectChanges();
113     expect(component.policyTypeComponent.ngOnChanges).toHaveBeenCalled();
114   });
115
116   it("should close all tables when the types are refreshed", async() => {
117     const ele = fixture.debugElement.nativeElement.querySelector("#visible");
118     ele.click();
119     fixture.detectChanges();
120     component.policyTypeComponent.ngOnChanges({
121       minimiseTrigger: new SimpleChange(null, null, component.policyTypeComponent.minimiseTrigger)
122     });
123     fixture.detectChanges();
124     expect(ele.innerText).toEqual("expand_more");
125   });
126
127   @Component({
128     selector: `policy-type-host-component`,
129     template: `<nrcp-policy-type
130       [minimiseTrigger]="this.minimiseTrigger"
131     ></nrcp-policy-type>`,
132   })
133   class TestPolicyTypeHostComponent {
134     @ViewChild(PolicyTypeComponent)
135     policyTypeComponent: PolicyTypeComponent;
136     minimiseTrigger: boolean = false;
137   }
138 });