Merge "Add test coverage of PolicyControlComponent"
[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 } 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
34 describe("PolicyControlComponent", () => {
35   let component: PolicyControlComponent;
36   let fixture: ComponentFixture<PolicyControlComponent>;
37
38   beforeEach(async(() => {
39     const policyServiceSpy = jasmine.createSpyObj("PolicyService", [
40       "getPolicyTypes",
41     ]);
42     const policyTypes = { policytype_ids: ["type1", "type2"] } as PolicyTypes;
43     policyServiceSpy.getPolicyTypes.and.returnValue(of(policyTypes));
44
45     TestBed.configureTestingModule({
46       imports: [MatIconModule, MatTableModule, BrowserAnimationsModule],
47       schemas: [CUSTOM_ELEMENTS_SCHEMA],
48       declarations: [
49         PolicyControlComponent,
50         MockComponent(PolicyTypeComponent),
51       ],
52       providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
53     }).compileComponents();
54   }));
55
56   beforeEach(() => {
57     fixture = TestBed.createComponent(PolicyControlComponent);
58     component = fixture.componentInstance;
59     fixture.detectChanges();
60   });
61
62   it("should create", () => {
63     expect(component).toBeTruthy();
64   });
65
66   it("should contain two PolicyType components instantiated with the correct type", () => {
67     const typeComponents: PolicyTypeComponent[] = fixture.debugElement
68       .queryAll(By.directive(PolicyTypeComponent))
69       .map((component) => component.componentInstance);
70
71     expect(typeComponents.length).toEqual(2);
72     expect(typeComponents[0].policyTypeId).toEqual("type1");
73     expect(typeComponents[1].policyTypeId).toEqual("type2");
74   });
75 });