Clean up and format
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / ric-selector / ric-selector.component.spec.ts
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: 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
21 import { HarnessLoader } from "@angular/cdk/testing";
22 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
23 import { Component, ViewChild, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
24 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
25 import { MatSelectModule } from "@angular/material/select";
26 import { MatSelectHarness } from "@angular/material/select/testing";
27 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
28 import { of } from "rxjs/observable/of";
29 import { Ric } from "src/app/interfaces/ric";
30 import { PolicyService } from "src/app/services/policy/policy.service";
31
32 import { RicSelectorComponent } from "./ric-selector.component";
33
34 describe("RicSelectorComponent", () => {
35   let component: TestRicSelectorHostComponent;
36   let fixture: ComponentFixture<TestRicSelectorHostComponent>;
37   let loader: HarnessLoader;
38   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
39   const ric1: Ric = {
40     ric_id: "ric1",
41     managed_element_ids: ["me1"],
42     policytype_ids: ["type1"],
43     state: "",
44   };
45   const ric2: Ric = {
46     ric_id: "ric2",
47     managed_element_ids: ["me1"],
48     policytype_ids: ["type1"],
49     state: "",
50   };
51
52   beforeEach(async(() => {
53     policyServiceSpy = jasmine.createSpyObj("PolicyService", ["getRics"]);
54     const policyData = {
55       createSchema: "{}",
56       instanceId: null,
57       instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
58       name: "name",
59       ric: null,
60     };
61
62     policyServiceSpy.getRics.and.returnValue(of({ rics: [ric1, ric2] }));
63     TestBed.configureTestingModule({
64       imports: [BrowserAnimationsModule, MatSelectModule],
65       schemas: [CUSTOM_ELEMENTS_SCHEMA],
66       declarations: [RicSelectorComponent, TestRicSelectorHostComponent],
67       providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
68     }).compileComponents();
69
70     fixture = TestBed.createComponent(TestRicSelectorHostComponent);
71     component = fixture.componentInstance;
72     fixture.detectChanges();
73     loader = TestbedHarnessEnvironment.loader(fixture);
74   }));
75
76   it("should create", () => {
77     expect(component).toBeTruthy();
78   });
79
80   it("no ric selected when no ric provided", async () => {
81     let ricSelector: MatSelectHarness = await loader.getHarness(
82       MatSelectHarness.with({ selector: "#ricSelector" })
83     );
84
85     expect(await ricSelector.isEmpty()).toBeTruthy();
86   });
87
88   it("options should contain rics for policy type", async () => {
89     let ricSelector: MatSelectHarness = await loader.getHarness(
90       MatSelectHarness.with({ selector: "#ricSelector" })
91     );
92
93     expect(policyServiceSpy.getRics).toHaveBeenCalledWith("policyTypeName");
94     await ricSelector.open();
95     const count = (await ricSelector.getOptions()).length;
96     expect(count).toEqual(2);
97   });
98
99   @Component({
100     selector: `ric-selector-host-component`,
101     template: `<nrcp-ric-selector
102       [policyTypeName]="policyTypeName"
103     ></nrcp-ric-selector>`,
104   })
105   class TestRicSelectorHostComponent {
106     @ViewChild(RicSelectorComponent)
107     private ricSelectorComponent: RicSelectorComponent;
108     policyTypeName: string = "policyTypeName";
109   }
110 });