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