156d6536b333719b6b093306e11db53a7ded7963
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance-dialog / 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 { PolicyInstanceDialogComponent } from "./policy-instance-dialog.component";
37 import { CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
38
39 describe('PolicyInstanceDialogComponent', () => {
40   let component: PolicyInstanceDialogComponent;
41   let fixture: ComponentFixture<PolicyInstanceDialogComponent>;
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         PolicyInstanceDialogComponent
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 without type', () => {
77     beforeEach(async () => {
78       const policyData = {
79         createSchema: '{}'
80       };
81       TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
82       ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
83     });
84
85     it('should contain oran logo and create title and no instance info', async () => {
86       let ele = fixture.debugElement.nativeElement.querySelector('img');
87       expect(ele.src).toContain('assets/oran-logo.png');
88
89       ele = fixture.debugElement.nativeElement.querySelector('text');
90       expect(ele.textContent).toEqual('Create new policy instance of type < No Type >');
91
92       ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
93       expect(ele).toBeFalsy();
94     });
95
96     it('should contain ric select', async () => {
97       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-ric-selector');
98       expect(ele).toBeTruthy();
99     });
100
101     it('should contain json editor', async () => {
102       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-no-type-policy-editor');
103       expect(ele).toBeTruthy();
104     });
105
106     it('should contain enabled Close button and disabled Submit button', async () => {
107       component.ngOnInit();
108
109       let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#closeButton' }));
110       expect(await closeButton.isDisabled()).toBeFalsy();
111       expect(await closeButton.getText()).toEqual('Close');
112
113       let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#submitButton' }));
114       // expect(await submitButton.isDisabled()).toBeTruthy();
115       expect(await submitButton.getText()).toEqual('Submit');
116     });
117   });
118
119   describe('content when editing policy without type', () => {
120     beforeEach(async () => {
121       const policyData = {
122         createSchema: '{}',
123         instanceId: 'instanceId',
124         instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
125         name: 'name',
126         ric: 'ric1'
127       };
128       TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
129       ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
130     });
131
132     it('should contain oran logo and instance info', async () => {
133       let ele = fixture.debugElement.nativeElement.querySelector('img');
134       expect(ele.src).toContain('assets/oran-logo.png');
135
136       ele = fixture.debugElement.nativeElement.querySelector('text');
137       expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
138
139       ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
140       expect(ele).toBeTruthy();
141       expect(ele.innerText).toEqual('[ric1] Instance ID: instanceId');
142     });
143
144     it('should not contain ric select', async () => {
145       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-ric-selector');
146       expect(ele).toBeFalsy();
147     });
148
149     it('should contain json editor', async () => {
150       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-no-type-policy-editor');
151       expect(ele).toBeTruthy();
152     });
153
154     it('should contain enabled Close and Submit buttons', async () => {
155       let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#closeButton' }));
156       expect(await closeButton.isDisabled()).toBeFalsy();
157       expect(await closeButton.getText()).toEqual('Close');
158
159       let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#submitButton' }));
160       expect(await submitButton.isDisabled()).toBeFalsy();
161       expect(await submitButton.getText()).toEqual('Submit');
162     });
163
164   });
165 });
166
167 function compileAndGetComponents(fixture: ComponentFixture<PolicyInstanceDialogComponent>, component: PolicyInstanceDialogComponent, loader: HarnessLoader) {
168   TestBed.compileComponents();
169
170   fixture = TestBed.createComponent(PolicyInstanceDialogComponent);
171   component = fixture.componentInstance;
172   fixture.detectChanges();
173   loader = TestbedHarnessEnvironment.loader(fixture);
174   return { fixture, component, loader };
175 }