2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
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";
31 describe("PolicyTypeComponent", () => {
32 let component: PolicyTypeComponent;
33 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
34 let fixture: ComponentFixture<PolicyTypeComponent>;
36 beforeEach(async(() => {
37 policyServiceSpy = jasmine.createSpyObj("PolicyService", ["getPolicyType"]);
38 const policyTypeSchema = JSON.parse(
39 '{"title": "1", "description": "Type 1 policy type"}'
41 const policyType = { policy_schema: policyTypeSchema } as PolicyType;
42 policyServiceSpy.getPolicyType.and.returnValue(of(policyType));
44 TestBed.configureTestingModule({
47 MockComponent(PolicyInstanceComponent),
49 providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
50 }).compileComponents();
54 fixture = TestBed.createComponent(PolicyTypeComponent);
55 component = fixture.componentInstance;
56 fixture.detectChanges();
59 it("should create", () => {
60 expect(component).toBeTruthy();
63 it("should not call service when no type, display correct type info and no PolicyInstanceComponent added", () => {
64 expect(policyServiceSpy.getPolicyType).not.toHaveBeenCalled();
66 expect(component.policyType).toEqual("< No Type >");
67 expect(component.policyDescription).toEqual("Type with no schema");
69 const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
70 expect(ele).toBeFalsy();
73 it("should call service when type, display correct type info and no PolicyInstanceComponent added", () => {
74 component.policyTypeId = "type1";
75 component.loadTypeInfo();
77 expect(policyServiceSpy.getPolicyType).toHaveBeenCalledWith("type1");
79 expect(component.policyType).toEqual("type1");
80 expect(component.policyDescription).toEqual("Type 1 policy type");
82 const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
83 expect(ele).toBeFalsy();
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");
91 fixture.detectChanges();
93 expect(ele.innerText).toEqual("expand_less");
95 const policyInstanceComp: PolicyInstanceComponent = fixture.debugElement.query(
96 By.directive(PolicyInstanceComponent)
98 expect(policyInstanceComp).toBeTruthy();
99 const expectedPolicyType = {
102 schemaObject: JSON.parse("{}")
103 } as PolicyTypeSchema;
104 expect(policyInstanceComp.policyTypeSchema).toEqual(expectedPolicyType);
105 expect(policyInstanceComp.expanded).toBeTruthy();