33170dba47165fe46cf231e0fde52299d332bfab
[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 format unformatted json", async () => {
93     const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
94       "policyJsonTextArea"
95     );
96     expect(textArea.value).toEqual('{"A":"A"}');
97
98     component.noTypePolicyEditorComponent.formatJsonInput();
99     expect(textArea.value).toEqual('{\n  "A": "A"\n}');
100   });
101
102   it("should send valid json", async () => {
103     const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
104       "policyJsonTextArea"
105     );
106     expect(textArea.value).toEqual('{"A":"A"}');
107
108     let validJson: string;
109     component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => {
110       validJson = json;
111     });
112
113     textArea.setValue('{"B":"B"}');
114     expect(validJson).toEqual('{"B":"B"}');
115   });
116
117   it("should send null when invalid json", async () => {
118     const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
119       "policyJsonTextArea"
120     );
121     expect(textArea.value).toEqual('{"A":"A"}');
122
123     let invalidJson: string;
124     component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => {
125       invalidJson = json;
126     });
127
128     textArea.setValue('{');
129     expect(invalidJson).toBeFalsy();
130   });
131
132   @Component({
133     selector: `no-type-policy-editor-host-component`,
134     template: `<nrcp-no-type-policy-editor
135       [policyJson]="this.policyJson"
136     ></nrcp-no-type-policy-editor>`,
137   })
138   class TestNoTypePolicyEditorComponentHostComponent {
139     @ViewChild(NoTypePolicyEditorComponent)
140     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
141     policyJson: string = '{"A":"A"}';
142   }
143 });