859e1f8f35e6aebb17f60961417dd3802b0157ba
[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
31 describe("PolicyTypeComponent", () => {
32   let component: PolicyTypeComponent;
33   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
34   let fixture: ComponentFixture<PolicyTypeComponent>;
35
36   beforeEach(async(() => {
37     policyServiceSpy = jasmine.createSpyObj("PolicyService", ["getPolicyType"]);
38     const policyTypeSchema = JSON.parse(
39       '{"title": "1", "description": "Type 1 policy type"}'
40     );
41     const policyType = { policy_schema: policyTypeSchema } as PolicyType;
42     policyServiceSpy.getPolicyType.and.returnValue(of(policyType));
43
44     TestBed.configureTestingModule({
45       declarations: [
46         PolicyTypeComponent,
47         MockComponent(PolicyInstanceComponent),
48       ],
49       providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
50     }).compileComponents();
51   }));
52
53   beforeEach(() => {
54     fixture = TestBed.createComponent(PolicyTypeComponent);
55     component = fixture.componentInstance;
56     fixture.detectChanges();
57   });
58
59   it("should create", () => {
60     expect(component).toBeTruthy();
61   });
62
63   it("should not call service when no type, display correct type info and no PolicyInstanceComponent added", () => {
64     expect(policyServiceSpy.getPolicyType).not.toHaveBeenCalled();
65
66     expect(component.policyType).toEqual("< No Type >");
67     expect(component.policyDescription).toEqual("Type with no schema");
68
69     const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
70     expect(ele).toBeFalsy();
71 });
72
73   it("should call service when type, display correct type info and no PolicyInstanceComponent added", () => {
74     component.policyTypeId = "type1";
75     component.loadTypeInfo();
76
77     expect(policyServiceSpy.getPolicyType).toHaveBeenCalledWith("type1");
78
79     expect(component.policyType).toEqual("type1");
80     expect(component.policyDescription).toEqual("Type 1 policy type");
81
82     const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
83     expect(ele).toBeFalsy();
84   });
85
86   it("should add PolicyInstanceComponent with correct data when toggle visible to visible", async () => {
87     const ele = fixture.debugElement.nativeElement.querySelector("#visible");
88     expect(ele.innerText).toEqual("expand_more");
89
90     ele.click();
91     fixture.detectChanges();
92
93     expect(ele.innerText).toEqual("expand_less");
94
95     const policyInstanceComp: PolicyInstanceComponent = fixture.debugElement.query(
96       By.directive(PolicyInstanceComponent)
97     ).componentInstance;
98     expect(policyInstanceComp).toBeTruthy();
99     const expectedPolicyType = {
100       id: undefined,
101       name: undefined,
102       schemaObject: JSON.parse("{}")
103     } as PolicyTypeSchema;
104     expect(policyInstanceComp.policyTypeSchema).toEqual(expectedPolicyType);
105   });
106 });