First version of policy editor components
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-instance-dialog / no-type-policy-instance-dialog.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 { BrowserAnimationsModule } from "@angular/platform-browser/animations";
22 import { ComponentFixture, TestBed } from "@angular/core/testing";
23 import { HarnessLoader } from "@angular/cdk/testing";
24 import { MatButtonModule } from '@angular/material/button';
25 import { MatButtonHarness } from '@angular/material/button/testing';
26 import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
27 import { MatSelectModule } from '@angular/material/select';
28 import { MatSelectHarness } from '@angular/material/select/testing';
29 import { MatInputModule } from '@angular/material/input';
30 import { MatInputHarness } from '@angular/material/input/testing';
31 import { of } from "rxjs/observable/of";
32 import { FormControl, ReactiveFormsModule } from "@angular/forms";
33 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
34 import { ToastrModule } from "ngx-toastr";
35
36 import { PolicyService } from "../../services/policy/policy.service";
37 import { ErrorDialogService } from "../../services/ui/error-dialog.service";
38 import { UiService } from "../../services/ui/ui.service";
39 import { NoTypePolicyInstanceDialogComponent } from "./no-type-policy-instance-dialog.component";
40 import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
41 import { NoTypePolicyEditorComponent } from "../no-type-policy-editor/no-type-policy-editor.component";
42
43 describe('NoTypePolicyInstanceDialogComponent', () => {
44   let component: NoTypePolicyInstanceDialogComponent;
45   let fixture: ComponentFixture<NoTypePolicyInstanceDialogComponent>;
46   let loader: HarnessLoader;
47   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
48   let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
49
50   beforeEach(async () => {
51     policyServiceSpy = jasmine.createSpyObj('PolicyService', [ 'putPolicy' ]);
52     errDialogServiceSpy = jasmine.createSpyObj('ErrorDialogService', [ 'displayError' ]);
53
54     TestBed.configureTestingModule({
55       imports: [
56         BrowserAnimationsModule,
57         MatButtonModule,
58         MatDialogModule,
59         MatInputModule,
60         MatSelectModule,
61         ReactiveFormsModule,
62         ToastrModule.forRoot()
63       ],
64       declarations: [
65         NoTypePolicyInstanceDialogComponent
66       ],
67       providers: [
68         { provide: MatDialogRef, useValue: component },
69         { provide: PolicyService, useValue: policyServiceSpy },
70         { provide: ErrorDialogService, useValue: errDialogServiceSpy },
71         { provide: MAT_DIALOG_DATA, useValue: true },
72         UiService
73       ]
74     });
75   });
76
77   describe('content when creating policy', () => {
78     beforeEach(async () => {
79       ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
80     });
81
82     it('should contain oran logo and create title and no instance info', async () => {
83       let ele = fixture.debugElement.nativeElement.querySelector('img');
84       expect(ele.src).toContain('assets/oran-logo.png');
85
86       ele = fixture.debugElement.nativeElement.querySelector('text');
87       expect(ele.childNodes[0].childNodes[0].textContent).toEqual('Create new policy instance of < No type >');
88
89       ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
90       expect(ele).toBeFalsy();
91     });
92
93     it('should contain ric select', async () => {
94       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-ric-selector');
95       expect(ele).toBeTruthy();
96     });
97
98     it('should contain json editor', async () => {
99       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-no-type-policy-editor');
100       expect(ele).toBeTruthy();
101     });
102
103     it('should contain enabled Close button and disabled Submit button', async () => {
104       component.ngOnInit();
105
106       let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#closeButton' }));
107       expect(await closeButton.isDisabled()).toBeFalsy();
108       expect(await closeButton.getText()).toEqual('Close');
109
110       let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#submitButton'}));
111       // expect(await submitButton.isDisabled()).toBeTruthy();
112       expect(await submitButton.getText()).toEqual('Submit');
113     });
114   });
115
116   describe('content when editing policy', () => {
117     beforeEach(async () => {
118       const policyData = {
119         createSchema: "{}",
120         instanceId: "instanceId",
121         instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
122         name: "name",
123         ric: "ric1"
124     };
125       TestBed.overrideProvider(MAT_DIALOG_DATA, {useValue: policyData }); // Should be provided with a policy
126       ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
127     });
128
129     it('should contain oran logo and instance info', async () => {
130         let ele = fixture.debugElement.nativeElement.querySelector('img');
131         expect(ele.src).toContain('assets/oran-logo.png');
132
133         ele = fixture.debugElement.nativeElement.querySelector('text');
134         expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
135
136         ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
137         expect(ele).toBeTruthy();
138         expect(ele.innerText).toEqual('[ric1] Instance ID: instanceId');
139     });
140
141     it('should not contain ric select', async () => {
142       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-ric-selector');
143       expect(ele).toBeFalsy();
144     });
145
146     it('should contain json editor', async () => {
147       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-no-type-policy-editor');
148       expect(ele).toBeTruthy();
149     });
150
151     it('should contain enabled Close and Submit buttons', async () => {
152       let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#closeButton'}));
153       expect(await closeButton.isDisabled()).toBeFalsy();
154       expect(await closeButton.getText()).toEqual('Close');
155
156       let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#submitButton'}));
157       expect(await submitButton.isDisabled()).toBeFalsy();
158       expect(await submitButton.getText()).toEqual('Submit');
159     });
160
161   });
162 });
163
164 function compileAndGetComponents(fixture: ComponentFixture<NoTypePolicyInstanceDialogComponent>, component: NoTypePolicyInstanceDialogComponent, loader: HarnessLoader) {
165   TestBed.compileComponents();
166
167   fixture = TestBed.createComponent(NoTypePolicyInstanceDialogComponent);
168   component = fixture.componentInstance;
169   fixture.detectChanges();
170   loader = TestbedHarnessEnvironment.loader(fixture);
171   return { fixture, component, loader };
172 }