Clean up and format
[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 { MatButtonModule } from "@angular/material/button";
26 import { MatButtonHarness } from "@angular/material/button/testing";
27 import { MatFormFieldModule } from "@angular/material/form-field";
28 import { MatInputModule } from "@angular/material/input";
29 import { MatInputHarness } from "@angular/material/input/testing";
30 import { BrowserModule } from "@angular/platform-browser";
31 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
32
33 import { NoTypePolicyEditorComponent } from "./no-type-policy-editor.component";
34
35 describe("NoTypePolicyEditorComponent", () => {
36
37   let component: TestNoTypePolicyEditorComponentHostComponent;
38   let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
39   let loader: HarnessLoader;
40
41   beforeEach(async () => {
42     TestBed.configureTestingModule({
43       imports: [
44         BrowserModule,
45         BrowserAnimationsModule,
46         MatButtonModule,
47         MatFormFieldModule,
48         MatInputModule,
49       ],
50       schemas: [CUSTOM_ELEMENTS_SCHEMA],
51       declarations: [
52         NoTypePolicyEditorComponent,
53         TestNoTypePolicyEditorComponentHostComponent,
54       ],
55     }).compileComponents();
56
57     fixture = TestBed.createComponent(
58       TestNoTypePolicyEditorComponentHostComponent
59     );
60     component = fixture.componentInstance;
61     fixture.detectChanges();
62     loader = TestbedHarnessEnvironment.loader(fixture);
63   });
64
65   it("should create", () => {
66     expect(component).toBeTruthy();
67   });
68
69   it("should contain provided policy json and enabled Format button", async () => {
70     let textArea: MatInputHarness = await loader.getHarness(
71       MatInputHarness.with({ selector: "#policyJsonTextArea" })
72     );
73     expect(await textArea.getValue()).toEqual('{"A":"A"}');
74
75     let formatButton: MatButtonHarness = await loader.getHarness(
76       MatButtonHarness.with({ selector: "#formatButton" })
77     );
78     expect(await formatButton.isDisabled()).toBeFalsy();
79   });
80
81   it("Format button should be disabled when json not valid", async () => {
82     const ele = component.noTypePolicyEditorComponent.instanceForm.get(
83       "policyJsonTextArea"
84     );
85     ele.setValue("{");
86
87     let formatButton: MatButtonHarness = await loader.getHarness(
88       MatButtonHarness.with({ selector: "#formatButton" })
89     );
90     expect(await formatButton.isDisabled()).toBeTruthy();
91   });
92
93   it("should format unformatted json", async () => {
94     const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
95       "policyJsonTextArea"
96     );
97     textArea.setValue('{"A":"A"}');
98     component.noTypePolicyEditorComponent.formatJsonInput();
99     expect(component.noTypePolicyEditorComponent.policyJson).toEqual(
100       '{\n  "A": "A"\n}'
101     );
102   });
103
104   @Component({
105     selector: `no-type-policy-editor-host-component`,
106     template: `<nrcp-no-type-policy-editor
107       [policyJson]="this.policyJson"
108     ></nrcp-no-type-policy-editor>`,
109   })
110   class TestNoTypePolicyEditorComponentHostComponent {
111     @ViewChild(NoTypePolicyEditorComponent)
112     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
113     policyJson: string = '{"A":"A"}';
114   }
115 });