Testing in Policy Control Component
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-control.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 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";
27
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';
37
38 describe("PolicyControlComponent", () => {
39   let hostComponent: PolicyControlComponent;
40   let hostFixture: ComponentFixture<PolicyControlComponent>;
41   let loader: HarnessLoader;
42   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
43   let el: DebugElement;
44
45   beforeEach(async(() => {
46     policyServiceSpy = jasmine.createSpyObj("PolicyService", [
47       "getPolicyTypes",
48     ]);
49
50     TestBed.configureTestingModule({
51       imports: [
52         MatIconModule,
53         MatTableModule,
54         BrowserAnimationsModule,
55         MatButtonModule,
56       ],
57       schemas: [CUSTOM_ELEMENTS_SCHEMA],
58       declarations: [
59         PolicyControlComponent,
60         MockComponent(PolicyTypeComponent),
61       ],
62       providers: [
63         { provide: PolicyService, useValue: policyServiceSpy }
64       ],
65     });
66   }));
67
68   describe("normally functioning", () => {
69     beforeEach(() => {
70       const policyTypes = { policytype_ids: ["type1", "type2"] } as PolicyTypes;
71       policyServiceSpy.getPolicyTypes.and.returnValue(of(policyTypes));
72
73       compileAndGetComponents();
74     });
75
76     it("should create", () => {
77       expect(hostComponent).toBeTruthy();
78     });
79
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);
84
85       expect(typeComponents.length).toEqual(2);
86       expect(typeComponents[0].policyTypeId).toEqual("type1");
87       expect(typeComponents[1].policyTypeId).toEqual("type2");
88     });
89
90     it("should call the refresh button when clicking on it", async () => {
91       let refreshButton: MatButtonHarness = await loader.getHarness(
92         MatButtonHarness.with({ selector: "#refreshButton" })
93       );
94       spyOn(hostComponent, "refreshTables");
95       await refreshButton.click();
96       expect(hostComponent.refreshTables).toHaveBeenCalled();
97     })
98
99     it("should close instance tables when clicking on refresh button", async () => {
100       let refreshButton: MatButtonHarness = await loader.getHarness(
101         MatButtonHarness.with({ selector: "#refreshButton" })
102       );
103       const policyTypeComponent: PolicyTypeComponent = hostFixture.debugElement.query(
104         By.directive(PolicyTypeComponent)
105       ).componentInstance;
106       let booleanTrigger = policyTypeComponent.minimiseTrigger
107       await refreshButton.click();
108       expect(policyTypeComponent.minimiseTrigger).not.toEqual(booleanTrigger);
109     })
110
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);
115
116       for(var i= 0; i < typeComponents.length-1; i++){
117         expect(typeComponents[i].policyTypeId<typeComponents[i+1].policyTypeId).toBeTruthy();
118       }
119     })
120   })
121
122   describe("no types", () => {
123     beforeEach(() => {
124       policyServiceSpy.getPolicyTypes.and.returnValue(
125         of({
126           policytype_ids: [],
127         } as PolicyTypes)
128       );
129
130       compileAndGetComponents();
131     });
132
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.");
137     });
138   });
139
140   function compileAndGetComponents() {
141     TestBed.compileComponents();
142     console.log(TestBed);
143
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 };
150   }
151 });