Fix PolicyInstanceDialogComponent
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-editor / no-type-policy-editor.component.spec.ts
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: 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 { HarnessLoader } from "@angular/cdk/testing";
22 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
23 import { Component, ViewChild, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
24 import { ComponentFixture, TestBed } from "@angular/core/testing";
25 import { FormBuilder, FormGroup } from "@angular/forms";
26 import { MatButtonModule } from "@angular/material/button";
27 import { MatButtonHarness } from "@angular/material/button/testing";
28 import { MatFormFieldModule } from "@angular/material/form-field";
29 import { MatInputModule } from "@angular/material/input";
30 import { MatInputHarness } from "@angular/material/input/testing";
31 import { BrowserModule } from "@angular/platform-browser";
32 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
33
34 import { NoTypePolicyEditorComponent } from "./no-type-policy-editor.component";
35
36 describe("NoTypePolicyEditorComponent", () => {
37   let formGroup: FormGroup = new FormGroup({});
38
39   let component: TestNoTypePolicyEditorComponentHostComponent;
40   let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
41   let loader: HarnessLoader;
42
43   beforeEach(async () => {
44     TestBed.configureTestingModule({
45       imports: [
46         BrowserModule,
47         BrowserAnimationsModule,
48         MatButtonModule,
49         MatFormFieldModule,
50         MatInputModule,
51       ],
52       schemas: [CUSTOM_ELEMENTS_SCHEMA],
53       declarations: [
54         NoTypePolicyEditorComponent,
55         TestNoTypePolicyEditorComponentHostComponent,
56       ],
57       providers: [FormBuilder],
58     }).compileComponents();
59
60     fixture = TestBed.createComponent(
61       TestNoTypePolicyEditorComponentHostComponent
62     );
63     component = fixture.componentInstance;
64     fixture.detectChanges();
65     loader = TestbedHarnessEnvironment.loader(fixture);
66   });
67
68   it("should create", () => {
69     expect(component).toBeTruthy();
70   });
71
72   it("should be added to form group with required validator", async () => {
73     let textArea: MatInputHarness = await loader.getHarness(
74       MatInputHarness.with({ selector: "#policyJsonTextArea" })
75     );
76
77     expect(formGroup.get("policyJsonTextArea")).toBeTruthy();
78     expect(await textArea.isRequired()).toBeTruthy();
79   });
80
81   it("should contain provided policy json and enabled Format button", async () => {
82     let textArea: MatInputHarness = await loader.getHarness(
83       MatInputHarness.with({ selector: "#policyJsonTextArea" })
84     );
85     expect(await textArea.getValue()).toEqual('{"A":"A"}');
86
87     let formatButton: MatButtonHarness = await loader.getHarness(
88       MatButtonHarness.with({ selector: "#formatButton" })
89     );
90     expect(await formatButton.isDisabled()).toBeFalsy();
91   });
92
93   it("Format button should be disabled when json not valid", async () => {
94     const ele = formGroup.get("policyJsonTextArea");
95     ele.setValue("{");
96
97     let formatButton: MatButtonHarness = await loader.getHarness(
98       MatButtonHarness.with({ selector: "#formatButton" })
99     );
100     expect(await formatButton.isDisabled()).toBeTruthy();
101   });
102
103   it("should format unformatted json", async () => {
104     const textArea = formGroup.get("policyJsonTextArea");
105     textArea.setValue('{"A":"A"}');
106     component.noTypePolicyEditorComponent.formatJsonInput();
107     expect(component.noTypePolicyEditorComponent.policyJson).toEqual(
108       '{\n  "A": "A"\n}'
109     );
110   });
111
112   @Component({
113     selector: `no-type-policy-editor-host-component`,
114     template: `<nrcp-no-type-policy-editor
115       [policyJson]="this.policyJson"
116       [instanceForm]="instanceForm"
117     ></nrcp-no-type-policy-editor>`,
118   })
119   class TestNoTypePolicyEditorComponentHostComponent {
120     @ViewChild(NoTypePolicyEditorComponent)
121     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
122     instanceForm: FormGroup = formGroup;
123     policyJson: string = '{"A":"A"}';
124   }
125 });