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