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