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