7fb64c4acafc753a7e9f225136e22356538b4f65
[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 contain provided policy json and enabled Format button", async () => {
73     let textArea: MatInputHarness = await loader.getHarness(
74       MatInputHarness.with({ selector: "#policyJsonTextArea" })
75     );
76     expect(await textArea.getValue()).toEqual('{"A":"A"}');
77
78     let formatButton: MatButtonHarness = await loader.getHarness(
79       MatButtonHarness.with({ selector: "#formatButton" })
80     );
81     expect(await formatButton.isDisabled()).toBeFalsy();
82   });
83
84   it("Format button should be disabled when json not valid", async () => {
85     const ele = component.noTypePolicyEditorComponent.instanceForm.get("policyJsonTextArea");
86     ele.setValue("{");
87
88     let formatButton: MatButtonHarness = await loader.getHarness(
89       MatButtonHarness.with({ selector: "#formatButton" })
90     );
91     expect(await formatButton.isDisabled()).toBeTruthy();
92   });
93
94   it("should format unformatted json", async () => {
95     const textArea = component.noTypePolicyEditorComponent.instanceForm.get("policyJsonTextArea");
96     textArea.setValue('{"A":"A"}');
97     component.noTypePolicyEditorComponent.formatJsonInput();
98     expect(component.noTypePolicyEditorComponent.policyJson).toEqual(
99       '{\n  "A": "A"\n}'
100     );
101   });
102
103   @Component({
104     selector: `no-type-policy-editor-host-component`,
105     template: `<nrcp-no-type-policy-editor
106       [policyJson]="this.policyJson"
107     ></nrcp-no-type-policy-editor>`,
108   })
109   class TestNoTypePolicyEditorComponentHostComponent {
110     @ViewChild(NoTypePolicyEditorComponent)
111     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
112     policyJson: string = '{"A":"A"}';
113   }
114 });