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";
30 import { Component, SimpleChange, ViewChild } from '@angular/core';
32 describe("PolicyTypeComponent", () => {
33 let component: TestPolicyTypeHostComponent;
34 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
35 let fixture: ComponentFixture<TestPolicyTypeHostComponent>;
37 beforeEach(async(() => {
38 policyServiceSpy = jasmine.createSpyObj("PolicyService", ["getPolicyType"]);
39 const policyTypeSchema = JSON.parse(
40 '{"title": "1", "description": "Type 1 policy type"}'
42 const policyType = { policy_schema: policyTypeSchema } as PolicyType;
43 policyServiceSpy.getPolicyType.and.returnValue(of(policyType));
45 TestBed.configureTestingModule({
48 MockComponent(PolicyInstanceComponent),
49 TestPolicyTypeHostComponent,
51 providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
52 }).compileComponents();
56 fixture = TestBed.createComponent(TestPolicyTypeHostComponent);
57 component = fixture.componentInstance;
58 fixture.detectChanges();
61 it("should create", () => {
62 expect(component).toBeTruthy();
65 it("should not call service when no type, display correct type info and no PolicyInstanceComponent added", () => {
66 expect(policyServiceSpy.getPolicyType).not.toHaveBeenCalled();
68 expect(component.policyTypeComponent.policyType).toEqual("< No Type >");
69 expect(component.policyTypeComponent.policyDescription).toEqual("Type with no schema");
71 const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
72 expect(ele).toBeFalsy();
75 it("should call service when type, display correct type info and no PolicyInstanceComponent added", () => {
76 component.policyTypeComponent.policyTypeId = "type1";
77 component.policyTypeComponent.loadTypeInfo();
79 expect(policyServiceSpy.getPolicyType).toHaveBeenCalledWith("type1");
81 expect(component.policyTypeComponent.policyType).toEqual("type1");
82 expect(component.policyTypeComponent.policyDescription).toEqual("Type 1 policy type");
84 const ele = fixture.debugElement.nativeElement.querySelector("nrcp-policy-instance");
85 expect(ele).toBeFalsy();
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");
93 fixture.detectChanges();
95 expect(ele.innerText).toEqual("expand_less");
97 const policyInstanceComp: PolicyInstanceComponent = fixture.debugElement.query(
98 By.directive(PolicyInstanceComponent)
100 expect(policyInstanceComp).toBeTruthy();
101 const expectedPolicyType = {
104 schemaObject: JSON.parse("{}")
105 } as PolicyTypeSchema;
106 expect(policyInstanceComp.policyTypeSchema).toEqual(expectedPolicyType);
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();
116 it("should close all tables when the types are refreshed", async() => {
117 const ele = fixture.debugElement.nativeElement.querySelector("#visible");
119 fixture.detectChanges();
120 component.policyTypeComponent.ngOnChanges({
121 minimiseTrigger: new SimpleChange(null, null, component.policyTypeComponent.minimiseTrigger)
123 fixture.detectChanges();
124 expect(ele.innerText).toEqual("expand_more");
128 selector: `policy-type-host-component`,
129 template: `<nrcp-policy-type
130 [minimiseTrigger]="this.minimiseTrigger"
131 ></nrcp-policy-type>`,
133 class TestPolicyTypeHostComponent {
134 @ViewChild(PolicyTypeComponent)
135 policyTypeComponent: PolicyTypeComponent;
136 minimiseTrigger: boolean = false;