From: elinuxhenrik Date: Thu, 1 Apr 2021 12:31:07 +0000 (+0200) Subject: Add test coverage of PolicyInstanceCompnent X-Git-Tag: 2.2.0~40^2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=94a4ef61a407db7514f2320ea8886c1dda526d4d;p=portal%2Fnonrtric-controlpanel.git Add test coverage of PolicyInstanceCompnent Change-Id: Ia30ece354c5f313e05caec28d2ccf20eddc8d8a0 Issue-ID: NONRTRIC-472 Signed-off-by: elinuxhenrik --- diff --git a/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.spec.ts b/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.spec.ts index 2546b6d..1260464 100644 --- a/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.spec.ts +++ b/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.spec.ts @@ -18,38 +18,109 @@ * ========================LICENSE_END=================================== */ -import { async, ComponentFixture } from "@angular/core/testing"; +import { Component, ViewChild } from "@angular/core"; +import { async, ComponentFixture, TestBed } from "@angular/core/testing"; +import { MatDialog } from "@angular/material/dialog"; +import { + PolicyInstance, + PolicyInstances, + PolicyStatus, + PolicyTypeSchema, +} from "@app/interfaces/policy.types"; import { PolicyService } from "@app/services/policy/policy.service"; +import { ConfirmDialogService } from "@app/services/ui/confirm-dialog.service"; +import { ErrorDialogService } from "@app/services/ui/error-dialog.service"; +import { NotificationService } from "@app/services/ui/notification.service"; +import { UiService } from "@app/services/ui/ui.service"; +import { ToastrModule } from "ngx-toastr"; +import { of } from "rxjs"; import { PolicyInstanceComponent } from "./policy-instance.component"; describe("PolicyInstanceComponent", () => { - let component: PolicyInstanceComponent; - let fixture: ComponentFixture; - - // beforeEach(async(() => { - // policyDataSourceSpy = jasmine.createSpyObj("PolicyInstanceDataSource", ["getPolicyType"]); - // const policyTypeSchema = JSON.parse( - // '{"title": "1", "description": "Type 1 policy type"}' - // ); - // const policyType = { policy_schema: policyTypeSchema } as PolicyType; - // policyDataSourceSpy.getPolicyType.and.returnValue(of(policyType)); - - // TestBed.configureTestingModule({ - // declarations: [ - // PolicyTypeComponent, - // MockComponent(PolicyInstanceComponent), - // ], - // providers: [{ provide: PolicyService, useValue: policyDataSourceSpy }], - // }).compileComponents(); - // })); - - // beforeEach(() => { - // fixture = TestBed.createComponent(PolicyTypeComponent); - // component = fixture.componentInstance; - // fixture.detectChanges(); - // }); - - // it("should create", () => { - // expect(component).toBeTruthy(); - // }); -}) \ No newline at end of file + let hostComponent: PolicyInstanceComponentHostComponent; + let hostFixture: ComponentFixture; + let policyServiceSpy: jasmine.SpyObj; + let dialogSpy: jasmine.SpyObj; + + @Component({ + selector: "policy-instance-compnent-host-component", + template: + "", + }) + class PolicyInstanceComponentHostComponent { + @ViewChild(PolicyInstanceComponent) + componentUnderTest: PolicyInstanceComponent; + policyTypeSchema = JSON.parse( + '{"title": "1", "description": "Type 1 policy type"}' + ); + policyType = { + id: "type1", + name: "1", + schemaObject: this.policyTypeSchema, + } as PolicyTypeSchema; + } + + beforeEach(async(() => { + policyServiceSpy = jasmine.createSpyObj("PolicyService", [ + "getPolicyInstancesByType", + "getPolicyInstance", + "getPolicyStatus", + ]); + let policyInstances = { policy_ids: ["policy1", "policy2"] } as PolicyInstances; + policyServiceSpy.getPolicyInstancesByType.and.returnValue( + of(policyInstances) + ); + let policy1 = { + policy_id: "policy1", + policy_data: "{}", + ric_id: "1", + service_id: "service", + lastModified: "Now", + } as PolicyInstance; + let policy2 = { + policy_id: "policy2", + policy_data: "{}", + ric_id: "2", + service_id: "service", + lastModified: "Now", + } as PolicyInstance; + policyServiceSpy.getPolicyInstance.and.returnValues( + of(policy1), + of(policy2) + ); + let policy1Status = { last_modified: "Just now" } as PolicyStatus; + let policy2Status = { last_modified: "Before" } as PolicyStatus; + policyServiceSpy.getPolicyStatus.and.returnValues( + of(policy1Status), + of(policy2Status) + ); + + dialogSpy = jasmine.createSpyObj("MatDialog", ["open"]); + + TestBed.configureTestingModule({ + imports: [ToastrModule.forRoot()], + declarations: [ + PolicyInstanceComponent, + PolicyInstanceComponentHostComponent, + ], + providers: [ + { provide: PolicyService, useValue: policyServiceSpy }, + { provide: MatDialog, useValue: dialogSpy }, + ErrorDialogService, + NotificationService, + ConfirmDialogService, + UiService, + ], + }).compileComponents(); + })); + + beforeEach(() => { + hostFixture = TestBed.createComponent(PolicyInstanceComponentHostComponent); + hostComponent = hostFixture.componentInstance; + hostFixture.detectChanges(); + }); + + it("should create", () => { + expect(hostComponent).toBeTruthy(); + }); +}); diff --git a/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.ts b/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.ts index 8442c23..aafc08e 100644 --- a/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.ts +++ b/webapp-frontend/src/app/policy/policy-instance/policy-instance.component.ts @@ -30,7 +30,7 @@ import { PolicyInstance } from "@interfaces/policy.types"; import { PolicyInstanceDialogComponent } from "../policy-instance-dialog/policy-instance-dialog.component"; import { getPolicyDialogProperties } from "../policy-instance-dialog/policy-instance-dialog.component"; import { HttpErrorResponse, HttpResponse } from "@angular/common/http"; -import { BehaviorSubject, Observable } from "rxjs"; +import { BehaviorSubject } from "rxjs"; import { UiService } from "@services/ui/ui.service"; import { FormControl, FormGroup } from "@angular/forms"; import { MatTableDataSource } from "@angular/material/table"; @@ -48,7 +48,6 @@ class PolicyTypeInfo { }) export class PolicyInstanceComponent implements OnInit { @Input() policyTypeSchema: PolicyTypeSchema; - @Input() expanded: Observable; policyInstances: PolicyInstance[] = []; private policyInstanceSubject = new BehaviorSubject([]); policyTypeInfo = new Map(); @@ -73,8 +72,6 @@ export class PolicyInstanceComponent implements OnInit { } ngOnInit() { - this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded)); - this.getPolicyInstances(); this.policyInstanceSubject.subscribe((data) => { this.instanceDataSource.data = data; @@ -105,14 +102,14 @@ export class PolicyInstanceComponent implements OnInit { getPolicyInstances() { this.policyInstances = [] as PolicyInstance[]; this.policySvc - .getPolicyInstancesByType(this.policyTypeSchema.id) - .subscribe((policies) => { - if (policies.policy_ids.length != 0) { - policies.policy_ids.forEach((policyId) => { + .getPolicyInstancesByType(this.policyTypeSchema.id) + .subscribe((policies) => { + if (policies.policy_ids.length != 0) { + policies.policy_ids.forEach((policyId) => { + this.policySvc + .getPolicyInstance(policyId) + .subscribe((policyInstance) => { this.policySvc - .getPolicyInstance(policyId) - .subscribe((policyInstance) => { - this.policySvc .getPolicyStatus(policyId) .subscribe((policyStatus) => { policyInstance.lastModified = policyStatus.last_modified; diff --git a/webapp-frontend/src/app/policy/policy-type/policy-type.component.html b/webapp-frontend/src/app/policy/policy-type/policy-type.component.html index ee8b2d0..6c95bf9 100644 --- a/webapp-frontend/src/app/policy/policy-type/policy-type.component.html +++ b/webapp-frontend/src/app/policy/policy-type/policy-type.component.html @@ -30,5 +30,5 @@ - + \ No newline at end of file diff --git a/webapp-frontend/src/app/policy/policy-type/policy-type.component.spec.ts b/webapp-frontend/src/app/policy/policy-type/policy-type.component.spec.ts index 28e99b0..859e1f8 100644 --- a/webapp-frontend/src/app/policy/policy-type/policy-type.component.spec.ts +++ b/webapp-frontend/src/app/policy/policy-type/policy-type.component.spec.ts @@ -102,6 +102,5 @@ describe("PolicyTypeComponent", () => { schemaObject: JSON.parse("{}") } as PolicyTypeSchema; expect(policyInstanceComp.policyTypeSchema).toEqual(expectedPolicyType); - expect(policyInstanceComp.expanded).toBeTruthy(); }); });