2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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 { ReactiveFormsModule } from "@angular/forms";
33 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
34 import { ToastrModule } from "ngx-toastr";
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 { Ric } from "../../interfaces/ric";
42 describe('NoTypePolicyInstanceDialogComponent', () => {
43 let component: NoTypePolicyInstanceDialogComponent;
44 let fixture: ComponentFixture<NoTypePolicyInstanceDialogComponent>;
45 let loader: HarnessLoader;
46 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
47 let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
48 const ric1: Ric = { ric_id: 'ric1', managed_element_ids: [ 'me1' ], policytype_ids: [ 'type1' ], state: ''};
49 const ric2: Ric = { ric_id: 'ric2', managed_element_ids: [ 'me1' ], policytype_ids: [ 'type1' ], state: ''};
51 beforeEach(async () => {
52 policyServiceSpy = jasmine.createSpyObj('PolicyService', [ 'putPolicy', 'getRics' ]);
53 errDialogServiceSpy = jasmine.createSpyObj('ErrorDialogService', [ 'displayError' ]);
55 policyServiceSpy.getRics.and.returnValue(of({ rics: [ ric1, ric2 ] }));
57 TestBed.configureTestingModule({
59 BrowserAnimationsModule,
65 ToastrModule.forRoot()
68 NoTypePolicyInstanceDialogComponent
71 { provide: MatDialogRef, useValue: component },
72 { provide: PolicyService, useValue: policyServiceSpy },
73 { provide: ErrorDialogService, useValue: errDialogServiceSpy },
74 { provide: MAT_DIALOG_DATA, useValue: true },
80 describe('content when creating policy', () => {
81 beforeEach(async () => {
82 ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
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');
89 ele = fixture.debugElement.nativeElement.querySelector('text');
90 expect(ele.childNodes[0].childNodes[0].textContent).toEqual('Create new policy instance of < No type >');
92 ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
93 expect(ele).toBeFalsy();
96 it('should contain enabled Target selection with no ric selected and json should be empty', async () => {
97 let ricSelector: MatSelectHarness = await loader.getHarness(MatSelectHarness.with({selector: '#ricSelector'}));
99 expect(await ricSelector.isEmpty()).toBeTruthy();
100 expect(await ricSelector.isDisabled()).toBeFalsy();
101 await ricSelector.open();
102 const count = (await ricSelector.getOptions()).length;
103 expect(count).toEqual(2);
105 let jsonTextArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({selector: '#policyJsonTextArea'}));
106 expect(await jsonTextArea.isDisabled()).toBeFalsy();
107 const actualJson: string = await jsonTextArea.getValue();
108 expect(actualJson).toEqual('');
111 it('should contain disabled Format and Submit buttons and enabled Close button', async () => {
112 component.ngOnInit();
114 let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#formatButton'}));
115 expect(await formatButton.isDisabled()).toBeTruthy();
116 expect(await formatButton.getText()).toEqual('Format JSON');
118 let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#closeButton'}));
119 expect(await closeButton.isDisabled()).toBeFalsy();
120 expect(await closeButton.getText()).toEqual('Close');
122 let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#submitButton'}));
123 expect(await submitButton.isDisabled()).toBeTruthy();
124 expect(await submitButton.getText()).toEqual('Submit');
128 describe('content when editing policy', () => {
129 beforeEach(async () => {
132 instanceId: "instanceId",
133 instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
137 TestBed.overrideProvider(MAT_DIALOG_DATA, {useValue: policyData }); // Should be provided with a policy
138 ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
141 it('should contain oran logo and instance info', async () => {
142 let ele = fixture.debugElement.nativeElement.querySelector('img');
143 expect(ele.src).toContain('assets/oran-logo.png');
145 ele = fixture.debugElement.nativeElement.querySelector('text');
146 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
148 ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
149 expect(ele).toBeTruthy();
150 expect(ele.innerText).toEqual('[ric1] Instance ID: instanceId');
153 it('should contain json and no Target selection', async () => {
154 let ele = fixture.debugElement.nativeElement.querySelector('#ricSelector');
155 expect(ele).toBeFalsy();
157 let jsonTextArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({selector: '#policyJsonTextArea'}));
158 expect(await jsonTextArea.isDisabled()).toBeFalsy();
159 const actualJson: string = await jsonTextArea.getValue();
160 expect(actualJson).toContain('qosObjectives');
163 it('should contain enabled Format, Submit and Close buttons', async () => {
164 let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#formatButton'}));
165 expect(await formatButton.isDisabled()).toBeFalsy();
166 expect(await formatButton.getText()).toEqual('Format JSON');
168 let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#closeButton'}));
169 expect(await closeButton.isDisabled()).toBeFalsy();
170 expect(await closeButton.getText()).toEqual('Close');
172 let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#submitButton'}));
173 expect(await submitButton.isDisabled()).toBeFalsy();
174 expect(await submitButton.getText()).toEqual('Submit');
180 function compileAndGetComponents(fixture: ComponentFixture<NoTypePolicyInstanceDialogComponent>, component: NoTypePolicyInstanceDialogComponent, loader: HarnessLoader) {
181 TestBed.compileComponents();
183 fixture = TestBed.createComponent(NoTypePolicyInstanceDialogComponent);
184 component = fixture.componentInstance;
185 fixture.detectChanges();
186 loader = TestbedHarnessEnvironment.loader(fixture);
187 return { fixture, component, loader };