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 { 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";
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";
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>;
46 beforeEach(async () => {
47 policyServiceSpy = jasmine.createSpyObj('PolicyService', ['putPolicy']);
48 errDialogServiceSpy = jasmine.createSpyObj('ErrorDialogService', ['displayError']);
50 TestBed.configureTestingModule({
52 BrowserAnimationsModule,
58 ToastrModule.forRoot()
61 CUSTOM_ELEMENTS_SCHEMA
64 PolicyInstanceDialogComponent
67 { provide: MatDialogRef, useValue: component },
68 { provide: PolicyService, useValue: policyServiceSpy },
69 { provide: ErrorDialogService, useValue: errDialogServiceSpy },
70 { provide: MAT_DIALOG_DATA, useValue: true },
76 describe('content when creating policy without type', () => {
77 beforeEach(async () => {
81 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
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.textContent).toEqual('Create new policy instance of type < No Type >');
92 ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
93 expect(ele).toBeFalsy();
96 it('should contain ric select', async () => {
97 const ele = fixture.debugElement.nativeElement.querySelector('nrcp-ric-selector');
98 expect(ele).toBeTruthy();
101 it('should contain json editor', async () => {
102 const ele = fixture.debugElement.nativeElement.querySelector('nrcp-no-type-policy-editor');
103 expect(ele).toBeTruthy();
106 it('should contain enabled Close button and disabled Submit button', async () => {
107 component.ngOnInit();
109 let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#closeButton' }));
110 expect(await closeButton.isDisabled()).toBeFalsy();
111 expect(await closeButton.getText()).toEqual('Close');
113 let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#submitButton' }));
114 // expect(await submitButton.isDisabled()).toBeTruthy();
115 expect(await submitButton.getText()).toEqual('Submit');
119 describe('content when editing policy without type', () => {
120 beforeEach(async () => {
123 instanceId: 'instanceId',
124 instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
128 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
129 ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
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');
136 ele = fixture.debugElement.nativeElement.querySelector('text');
137 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
139 ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
140 expect(ele).toBeTruthy();
141 expect(ele.innerText).toEqual('[ric1] Instance ID: instanceId');
144 it('should not contain ric select', async () => {
145 const ele = fixture.debugElement.nativeElement.querySelector('nrcp-ric-selector');
146 expect(ele).toBeFalsy();
149 it('should contain json editor', async () => {
150 const ele = fixture.debugElement.nativeElement.querySelector('nrcp-no-type-policy-editor');
151 expect(ele).toBeTruthy();
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');
159 let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#submitButton' }));
160 expect(await submitButton.isDisabled()).toBeFalsy();
161 expect(await submitButton.getText()).toEqual('Submit');
167 function compileAndGetComponents(fixture: ComponentFixture<PolicyInstanceDialogComponent>, component: PolicyInstanceDialogComponent, loader: HarnessLoader) {
168 TestBed.compileComponents();
170 fixture = TestBed.createComponent(PolicyInstanceDialogComponent);
171 component = fixture.componentInstance;
172 fixture.detectChanges();
173 loader = TestbedHarnessEnvironment.loader(fixture);
174 return { fixture, component, loader };