12604644af913039f4fbe6881c5dbfee2ba9da77
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance / policy-instance.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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 { Component, ViewChild } from "@angular/core";
22 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
23 import { MatDialog } from "@angular/material/dialog";
24 import {
25   PolicyInstance,
26   PolicyInstances,
27   PolicyStatus,
28   PolicyTypeSchema,
29 } from "@app/interfaces/policy.types";
30 import { PolicyService } from "@app/services/policy/policy.service";
31 import { ConfirmDialogService } from "@app/services/ui/confirm-dialog.service";
32 import { ErrorDialogService } from "@app/services/ui/error-dialog.service";
33 import { NotificationService } from "@app/services/ui/notification.service";
34 import { UiService } from "@app/services/ui/ui.service";
35 import { ToastrModule } from "ngx-toastr";
36 import { of } from "rxjs";
37 import { PolicyInstanceComponent } from "./policy-instance.component";
38
39 describe("PolicyInstanceComponent", () => {
40   let hostComponent: PolicyInstanceComponentHostComponent;
41   let hostFixture: ComponentFixture<PolicyInstanceComponentHostComponent>;
42   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
43   let dialogSpy: jasmine.SpyObj<MatDialog>;
44
45   @Component({
46     selector: "policy-instance-compnent-host-component",
47     template:
48       "<nrcp-policy-instance [policyTypeSchema]=policyType></nrcp-policy-instance>",
49   })
50   class PolicyInstanceComponentHostComponent {
51     @ViewChild(PolicyInstanceComponent)
52     componentUnderTest: PolicyInstanceComponent;
53     policyTypeSchema = JSON.parse(
54       '{"title": "1", "description": "Type 1 policy type"}'
55     );
56     policyType = {
57       id: "type1",
58       name: "1",
59       schemaObject: this.policyTypeSchema,
60     } as PolicyTypeSchema;
61   }
62
63   beforeEach(async(() => {
64     policyServiceSpy = jasmine.createSpyObj("PolicyService", [
65       "getPolicyInstancesByType",
66       "getPolicyInstance",
67       "getPolicyStatus",
68     ]);
69     let policyInstances = { policy_ids: ["policy1", "policy2"] } as PolicyInstances;
70     policyServiceSpy.getPolicyInstancesByType.and.returnValue(
71       of(policyInstances)
72     );
73     let policy1 = {
74       policy_id: "policy1",
75       policy_data: "{}",
76       ric_id: "1",
77       service_id: "service",
78       lastModified: "Now",
79     } as PolicyInstance;
80     let policy2 = {
81       policy_id: "policy2",
82       policy_data: "{}",
83       ric_id: "2",
84       service_id: "service",
85       lastModified: "Now",
86     } as PolicyInstance;
87     policyServiceSpy.getPolicyInstance.and.returnValues(
88       of(policy1),
89       of(policy2)
90     );
91     let policy1Status = { last_modified: "Just now" } as PolicyStatus;
92     let policy2Status = { last_modified: "Before" } as PolicyStatus;
93     policyServiceSpy.getPolicyStatus.and.returnValues(
94       of(policy1Status),
95       of(policy2Status)
96     );
97
98     dialogSpy = jasmine.createSpyObj("MatDialog", ["open"]);
99
100     TestBed.configureTestingModule({
101       imports: [ToastrModule.forRoot()],
102       declarations: [
103         PolicyInstanceComponent,
104         PolicyInstanceComponentHostComponent,
105       ],
106       providers: [
107         { provide: PolicyService, useValue: policyServiceSpy },
108         { provide: MatDialog, useValue: dialogSpy },
109         ErrorDialogService,
110         NotificationService,
111         ConfirmDialogService,
112         UiService,
113       ],
114     }).compileComponents();
115   }));
116
117   beforeEach(() => {
118     hostFixture = TestBed.createComponent(PolicyInstanceComponentHostComponent);
119     hostComponent = hostFixture.componentInstance;
120     hostFixture.detectChanges();
121   });
122
123   it("should create", () => {
124     expect(hostComponent).toBeTruthy();
125   });
126 });