First version of policy editor components
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / ric-selector / ric-selector.component.spec.ts
1 import { HarnessLoader } from '@angular/cdk/testing';
2 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
3 import { Component, ViewChild } from '@angular/core';
4 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
5 import { FormBuilder, FormGroup } from '@angular/forms';
6 import { MatSelectModule } from '@angular/material/select';
7 import { MatSelectHarness } from '@angular/material/select/testing';
8 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
9 import { of } from "rxjs/observable/of";
10 import { Ric } from 'src/app/interfaces/ric';
11 import { PolicyService } from 'src/app/services/policy/policy.service';
12
13 import { RicSelectorComponent } from './ric-selector.component';
14
15 let formGroup: FormGroup = new FormGroup({});
16
17 describe('RicSelectorComponent', () => {
18   let component: TestRicSelectorHostComponent;
19   let fixture: ComponentFixture<TestRicSelectorHostComponent>;
20   let loader: HarnessLoader;
21   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
22   const ric1: Ric = { ric_id: 'ric1', managed_element_ids: ['me1'], policytype_ids: ['type1'], state: '' };
23   const ric2: Ric = { ric_id: 'ric2', managed_element_ids: ['me1'], policytype_ids: ['type1'], state: '' };
24
25   beforeEach(async(() => {
26     policyServiceSpy = jasmine.createSpyObj('PolicyService', ['getRics']);
27     const policyData = {
28       createSchema: "{}",
29       instanceId: null,
30       instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
31       name: "name",
32       ric: null
33     };
34
35     policyServiceSpy.getRics.and.returnValue(of({ rics: [ric1, ric2] }));
36     TestBed.configureTestingModule({
37       imports: [
38         BrowserAnimationsModule,
39         MatSelectModule,
40       ],
41       declarations: [
42         RicSelectorComponent,
43         TestRicSelectorHostComponent
44       ],
45       providers: [
46         { provide: PolicyService, useValue: policyServiceSpy },
47         FormBuilder
48       ]
49     })
50       .compileComponents();
51
52     fixture = TestBed.createComponent(TestRicSelectorHostComponent);
53     component = fixture.componentInstance;
54     fixture.detectChanges();
55     loader = TestbedHarnessEnvironment.loader(fixture);
56   }));
57
58   it('should create', () => {
59     expect(component).toBeTruthy();
60   });
61
62   it('should be added to form group with required validator', async () => {
63     let ricSelector: MatSelectHarness = await loader.getHarness(MatSelectHarness.with({ selector: '#ricSelector' }));
64
65     expect(formGroup.get('ricSelector')).toBeTruthy();
66     expect(await ricSelector.isRequired()).toBeTruthy();
67   });
68
69   it('no ric selected', async () => {
70     let ricSelector: MatSelectHarness = await loader.getHarness(MatSelectHarness.with({ selector: '#ricSelector' }));
71
72     expect(await ricSelector.isEmpty()).toBeTruthy();
73   });
74
75   it('options should contain rics for policy type', async () => {
76     let ricSelector: MatSelectHarness = await loader.getHarness(MatSelectHarness.with({ selector: '#ricSelector' }));
77
78     expect(policyServiceSpy.getRics).toHaveBeenCalledWith('policyTypeName');
79     await ricSelector.open();
80     const count = (await ricSelector.getOptions()).length;
81     expect(count).toEqual(2);
82   });
83 });
84
85 @Component({
86   selector: `ric-selector-host-component`,
87   template: `<nrcp-ric-selector [instanceForm]="instanceForm" [policyTypeName]="policyTypeName"></nrcp-ric-selector>`
88 })
89 export class TestRicSelectorHostComponent {
90   @ViewChild(RicSelectorComponent)
91   private ricSelectorComponent: RicSelectorComponent;
92   instanceForm: FormGroup = formGroup;
93   policyTypeName: string = 'policyTypeName';
94 }