Fix bug in NoTypePolicyEditorComponent
[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   let component: TestNoTypePolicyEditorComponentHostComponent;
37   let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
38   let loader: HarnessLoader;
39
40   beforeEach(async () => {
41     TestBed.configureTestingModule({
42       imports: [
43         BrowserModule,
44         BrowserAnimationsModule,
45         MatButtonModule,
46         MatFormFieldModule,
47         MatInputModule,
48       ],
49       schemas: [CUSTOM_ELEMENTS_SCHEMA],
50       declarations: [
51         NoTypePolicyEditorComponent,
52         TestNoTypePolicyEditorComponentHostComponent,
53       ],
54     }).compileComponents();
55
56     fixture = TestBed.createComponent(
57       TestNoTypePolicyEditorComponentHostComponent
58     );
59     component = fixture.componentInstance;
60     fixture.detectChanges();
61     loader = TestbedHarnessEnvironment.loader(fixture);
62   });
63
64   it("should create", () => {
65     expect(component).toBeTruthy();
66   });
67
68   it("should contain provided policy json and enabled Format button", async () => {
69     const textArea: MatInputHarness = await loader.getHarness(
70       MatInputHarness.with({ selector: "#policyJsonTextArea" })
71     );
72     expect(await textArea.getValue()).toEqual('{"A":"A"}');
73
74     const formatButton: MatButtonHarness = await loader.getHarness(
75       MatButtonHarness.with({ selector: "#formatButton" })
76     );
77     expect(await formatButton.isDisabled()).toBeFalsy();
78   });
79
80   it("Format button should be disabled when json not valid", async () => {
81     const ele = component.noTypePolicyEditorComponent.instanceForm.get(
82       "policyJsonTextArea"
83     );
84     ele.setValue("{");
85
86     const formatButton: MatButtonHarness = await loader.getHarness(
87       MatButtonHarness.with({ selector: "#formatButton" })
88     );
89     expect(await formatButton.isDisabled()).toBeTruthy();
90   });
91
92   it("should send valid json", async () => {
93     const textAreaHarness: MatInputHarness = await loader.getHarness(
94       MatInputHarness.with({ selector: "#policyJsonTextArea" })
95     );
96     expect(await textAreaHarness.getValue()).toEqual('{"A":"A"}');
97
98     let validJson: string;
99     component.noTypePolicyEditorComponent.validJson.subscribe(
100       (json: string) => {
101         validJson = json;
102       }
103     );
104
105     const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
106       "policyJsonTextArea"
107     );
108     textArea.setValue('{"B":"B"}');
109     expect(validJson).toEqual('{"B":"B"}');
110   });
111
112   it("should send null when invalid json", async () => {
113     const textArea: MatInputHarness = await loader.getHarness(
114       MatInputHarness.with({ selector: "#policyJsonTextArea" })
115     );
116     expect(await textArea.getValue()).toEqual('{"A":"A"}');
117
118     let invalidJson: string;
119     component.noTypePolicyEditorComponent.validJson.subscribe(
120       (json: string) => {
121         invalidJson = json;
122       }
123     );
124
125     textArea.setValue("{");
126     expect(invalidJson).toBeFalsy();
127   });
128
129   @Component({
130     selector: `no-type-policy-editor-host-component`,
131     template: `<nrcp-no-type-policy-editor
132       [policyJson]="this.policyJson"
133     ></nrcp-no-type-policy-editor>`,
134   })
135   class TestNoTypePolicyEditorComponentHostComponent {
136     @ViewChild(NoTypePolicyEditorComponent)
137     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
138     policyJson: string = '{"A":"A"}';
139   }
140 });