Add test coverage of PolicyInstanceCompnent 47/5847/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Thu, 1 Apr 2021 12:31:07 +0000 (14:31 +0200)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Thu, 1 Apr 2021 12:31:13 +0000 (14:31 +0200)
Change-Id: Ia30ece354c5f313e05caec28d2ccf20eddc8d8a0
Issue-ID: NONRTRIC-472
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
webapp-frontend/src/app/policy/policy-instance/policy-instance.component.spec.ts
webapp-frontend/src/app/policy/policy-instance/policy-instance.component.ts
webapp-frontend/src/app/policy/policy-type/policy-type.component.html
webapp-frontend/src/app/policy/policy-type/policy-type.component.spec.ts

index 2546b6d..1260464 100644 (file)
  * ========================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<PolicyInstanceComponent>;
-
-    // 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<PolicyInstanceComponentHostComponent>;
+  let policyServiceSpy: jasmine.SpyObj<PolicyService>;
+  let dialogSpy: jasmine.SpyObj<MatDialog>;
+
+  @Component({
+    selector: "policy-instance-compnent-host-component",
+    template:
+      "<nrcp-policy-instance [policyTypeSchema]=policyType></nrcp-policy-instance>",
+  })
+  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();
+  });
+});
index 8442c23..aafc08e 100644 (file)
@@ -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<boolean>;
   policyInstances: PolicyInstance[] = [];
   private policyInstanceSubject = new BehaviorSubject<PolicyInstance[]>([]);
   policyTypeInfo = new Map<string, PolicyTypeInfo>();
@@ -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;
index ee8b2d0..6c95bf9 100644 (file)
@@ -30,5 +30,5 @@
   </div>
 </div>
 
-<nrcp-policy-instance *ngIf="isVisible.value" [policyTypeSchema]=policyTypeInfo.type [expanded]=isVisible.asObservable()>
+<nrcp-policy-instance *ngIf="isVisible.value" [policyTypeSchema]=policyTypeInfo.type>
 </nrcp-policy-instance>
\ No newline at end of file
index 28e99b0..859e1f8 100644 (file)
@@ -102,6 +102,5 @@ describe("PolicyTypeComponent", () => {
       schemaObject: JSON.parse("{}")
     } as PolicyTypeSchema;
     expect(policyInstanceComp.policyTypeSchema).toEqual(expectedPolicyType);
-    expect(policyInstanceComp.expanded).toBeTruthy();
   });
 });