2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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===================================
20 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
21 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
22 import { By } from "@angular/platform-browser";
23 import { MatIconModule } from "@angular/material/icon";
24 import { MatTableModule } from "@angular/material/table";
25 import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from "@angular/core";
26 import { of } from "rxjs";
28 import { PolicyControlComponent } from "./policy-control.component";
29 import { PolicyTypes } from "@interfaces/policy.types";
30 import { PolicyService } from "@services/policy/policy.service";
31 import { MockComponent } from "ng-mocks";
32 import { PolicyTypeComponent } from "./policy-type/policy-type.component";
33 import { MatButtonHarness } from '@angular/material/button/testing';
34 import { MatButtonModule } from '@angular/material/button';
35 import { HarnessLoader } from '@angular/cdk/testing';
36 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
38 describe("PolicyControlComponent", () => {
39 let hostComponent: PolicyControlComponent;
40 let hostFixture: ComponentFixture<PolicyControlComponent>;
41 let loader: HarnessLoader;
42 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
45 beforeEach(async(() => {
46 policyServiceSpy = jasmine.createSpyObj("PolicyService", [
50 TestBed.configureTestingModule({
54 BrowserAnimationsModule,
57 schemas: [CUSTOM_ELEMENTS_SCHEMA],
59 PolicyControlComponent,
60 MockComponent(PolicyTypeComponent),
63 { provide: PolicyService, useValue: policyServiceSpy }
68 describe("normally functioning", () => {
70 const policyTypes = { policytype_ids: ["type1", "type2"] } as PolicyTypes;
71 policyServiceSpy.getPolicyTypes.and.returnValue(of(policyTypes));
73 compileAndGetComponents();
76 it("should create", () => {
77 expect(hostComponent).toBeTruthy();
80 it("should contain two PolicyType components instantiated with the correct type", () => {
81 const typeComponents: PolicyTypeComponent[] = hostFixture.debugElement
82 .queryAll(By.directive(PolicyTypeComponent))
83 .map((component) => component.componentInstance);
85 expect(typeComponents.length).toEqual(2);
86 expect(typeComponents[0].policyTypeId).toEqual("type1");
87 expect(typeComponents[1].policyTypeId).toEqual("type2");
90 it("should call the refresh button when clicking on it", async () => {
91 let refreshButton: MatButtonHarness = await loader.getHarness(
92 MatButtonHarness.with({ selector: "#refreshButton" })
94 spyOn(hostComponent, "refreshTables");
95 await refreshButton.click();
96 expect(hostComponent.refreshTables).toHaveBeenCalled();
99 it("should close instance tables when clicking on refresh button", async () => {
100 let refreshButton: MatButtonHarness = await loader.getHarness(
101 MatButtonHarness.with({ selector: "#refreshButton" })
103 const policyTypeComponent: PolicyTypeComponent = hostFixture.debugElement.query(
104 By.directive(PolicyTypeComponent)
106 let booleanTrigger = policyTypeComponent.minimiseTrigger
107 await refreshButton.click();
108 expect(policyTypeComponent.minimiseTrigger).not.toEqual(booleanTrigger);
111 it("should render the types sorted when clicking on refresh button", async () => {
112 const typeComponents: PolicyTypeComponent[] = hostFixture.debugElement
113 .queryAll(By.directive(PolicyTypeComponent))
114 .map((component) => component.componentInstance);
116 for(var i= 0; i < typeComponents.length-1; i++){
117 expect(typeComponents[i].policyTypeId<typeComponents[i+1].policyTypeId).toBeTruthy();
122 describe("no types", () => {
124 policyServiceSpy.getPolicyTypes.and.returnValue(
130 compileAndGetComponents();
133 it("should display message of no types", async () => {
134 expect(policyServiceSpy.getPolicyTypes.length).toEqual(0);
135 const content = el.query(By.css('#noInstance')).nativeElement;
136 expect(content.innerText).toBe("There are no policy types to display.");
140 function compileAndGetComponents() {
141 TestBed.compileComponents();
142 console.log(TestBed);
144 hostFixture = TestBed.createComponent(PolicyControlComponent);
145 hostComponent = hostFixture.componentInstance;
146 el = hostFixture.debugElement;
147 hostFixture.detectChanges();
148 loader = TestbedHarnessEnvironment.loader(hostFixture);
149 return { hostFixture, hostComponent, loader };