fb69cd1b54c16e7d0b632d5ea4bea3842c115118
[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 { OptionHarnessFilters } from "@angular/material/core/testing";
26 import { MatSelectModule } from "@angular/material/select";
27 import { MatSelectHarness } from "@angular/material/select/testing";
28 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
29 import { of } from "rxjs/observable/of";
30 import { Ric } from "src/app/interfaces/ric";
31 import { PolicyService } from "src/app/services/policy/policy.service";
32
33 import { RicSelectorComponent } from "./ric-selector.component";
34
35 describe("RicSelectorComponent", () => {
36   let component: TestRicSelectorHostComponent;
37   let fixture: ComponentFixture<TestRicSelectorHostComponent>;
38   let loader: HarnessLoader;
39   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
40   const ric1: Ric = {
41     ric_id: "ric1",
42     managed_element_ids: ["me1"],
43     policytype_ids: ["type1"],
44     state: "",
45   };
46   const ric2: Ric = {
47     ric_id: "ric2",
48     managed_element_ids: ["me1"],
49     policytype_ids: ["type1"],
50     state: "",
51   };
52
53   beforeEach(async(() => {
54     policyServiceSpy = jasmine.createSpyObj("PolicyService", ["getRics"]);
55     const policyData = {
56       createSchema: "{}",
57       instanceId: null,
58       instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
59       name: "name",
60       ric: null,
61     };
62
63     policyServiceSpy.getRics.and.returnValue(of({ rics: [ric1, ric2] }));
64     TestBed.configureTestingModule({
65       imports: [BrowserAnimationsModule, MatSelectModule],
66       schemas: [CUSTOM_ELEMENTS_SCHEMA],
67       declarations: [RicSelectorComponent, TestRicSelectorHostComponent],
68       providers: [{ provide: PolicyService, useValue: policyServiceSpy }],
69     }).compileComponents();
70
71     fixture = TestBed.createComponent(TestRicSelectorHostComponent);
72     component = fixture.componentInstance;
73     fixture.detectChanges();
74     loader = TestbedHarnessEnvironment.loader(fixture);
75   }));
76
77   it("should create", () => {
78     expect(component).toBeTruthy();
79   });
80
81   it("no ric selected", async () => {
82     let ricSelector: MatSelectHarness = await loader.getHarness(
83       MatSelectHarness.with({ selector: "#ricSelector" })
84     );
85
86     expect(await ricSelector.isEmpty()).toBeTruthy();
87   });
88
89   it("options should contain rics for policy type", async () => {
90     let ricSelector: MatSelectHarness = await loader.getHarness(
91       MatSelectHarness.with({ selector: "#ricSelector" })
92     );
93
94     expect(policyServiceSpy.getRics).toHaveBeenCalledWith("policyTypeName");
95     await ricSelector.open();
96     const count = (await ricSelector.getOptions()).length;
97     expect(count).toEqual(2);
98   });
99
100   it("should send selected ric", async () => {
101     let selectedRic: string;
102     component.ricSelectorComponent.selectedRic.subscribe((ric: string) => {
103       selectedRic = ric;
104     });
105
106     let ricSelector: MatSelectHarness = await loader.getHarness(
107       MatSelectHarness.with({ selector: "#ricSelector" })
108     );
109     await ricSelector.clickOptions({ text: "ric1" });
110
111     expect(selectedRic).toEqual("ric1");
112   });
113
114   @Component({
115     selector: `ric-selector-host-component`,
116     template: `<nrcp-ric-selector
117       [policyTypeName]="policyTypeName"
118     ></nrcp-ric-selector>`,
119   })
120   class TestRicSelectorHostComponent {
121     @ViewChild(RicSelectorComponent)
122     ricSelectorComponent: RicSelectorComponent;
123     policyTypeName: string = "policyTypeName";
124   }
125 });