From: elinuxhenrik Date: Mon, 22 Mar 2021 13:42:46 +0000 (+0100) Subject: Fix json formatting and tests NoTypePolicyEditor X-Git-Tag: 2.2.0~54 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F86%2F5786%2F1;p=portal%2Fnonrtric-controlpanel.git Fix json formatting and tests NoTypePolicyEditor Change-Id: Iaf9f2ee891031d7cec54cfffff64cc7b031bc5f4 Signed-off-by: elinuxhenrik Issue-ID: NONRTRIC-463 --- diff --git a/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.spec.ts b/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.spec.ts index c87a656..33170db 100644 --- a/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.spec.ts +++ b/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.spec.ts @@ -33,7 +33,6 @@ import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { NoTypePolicyEditorComponent } from "./no-type-policy-editor.component"; describe("NoTypePolicyEditorComponent", () => { - let component: TestNoTypePolicyEditorComponentHostComponent; let fixture: ComponentFixture; let loader: HarnessLoader; @@ -67,12 +66,12 @@ describe("NoTypePolicyEditorComponent", () => { }); it("should contain provided policy json and enabled Format button", async () => { - let textArea: MatInputHarness = await loader.getHarness( + const textArea: MatInputHarness = await loader.getHarness( MatInputHarness.with({ selector: "#policyJsonTextArea" }) ); expect(await textArea.getValue()).toEqual('{"A":"A"}'); - let formatButton: MatButtonHarness = await loader.getHarness( + const formatButton: MatButtonHarness = await loader.getHarness( MatButtonHarness.with({ selector: "#formatButton" }) ); expect(await formatButton.isDisabled()).toBeFalsy(); @@ -84,7 +83,7 @@ describe("NoTypePolicyEditorComponent", () => { ); ele.setValue("{"); - let formatButton: MatButtonHarness = await loader.getHarness( + const formatButton: MatButtonHarness = await loader.getHarness( MatButtonHarness.with({ selector: "#formatButton" }) ); expect(await formatButton.isDisabled()).toBeTruthy(); @@ -94,11 +93,40 @@ describe("NoTypePolicyEditorComponent", () => { const textArea = component.noTypePolicyEditorComponent.instanceForm.get( "policyJsonTextArea" ); - textArea.setValue('{"A":"A"}'); + expect(textArea.value).toEqual('{"A":"A"}'); + component.noTypePolicyEditorComponent.formatJsonInput(); - expect(component.noTypePolicyEditorComponent.policyJson).toEqual( - '{\n "A": "A"\n}' + expect(textArea.value).toEqual('{\n "A": "A"\n}'); + }); + + it("should send valid json", async () => { + const textArea = component.noTypePolicyEditorComponent.instanceForm.get( + "policyJsonTextArea" ); + expect(textArea.value).toEqual('{"A":"A"}'); + + let validJson: string; + component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => { + validJson = json; + }); + + textArea.setValue('{"B":"B"}'); + expect(validJson).toEqual('{"B":"B"}'); + }); + + it("should send null when invalid json", async () => { + const textArea = component.noTypePolicyEditorComponent.instanceForm.get( + "policyJsonTextArea" + ); + expect(textArea.value).toEqual('{"A":"A"}'); + + let invalidJson: string; + component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => { + invalidJson = json; + }); + + textArea.setValue('{'); + expect(invalidJson).toBeFalsy(); }); @Component({ diff --git a/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts b/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts index d231b00..5d2398c 100644 --- a/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts +++ b/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts @@ -63,9 +63,9 @@ export class NoTypePolicyEditorComponent implements OnInit { } formatJsonInput(): void { - this.policyJson = formatJsonString( - JSON.parse(this.policyJsonTextArea.value) - ); + let jsonBefore: string = this.policyJsonTextArea.value; + let jsonAfter = formatJsonString(JSON.parse(jsonBefore)); + this.policyJsonTextArea.setValue(jsonAfter); } jsonValidator(): ValidatorFn { @@ -85,16 +85,14 @@ export class NoTypePolicyEditorComponent implements OnInit { } isJsonValid(json: string): boolean { + let valid = false as boolean; try { if (json != null) { JSON.parse(json); - return true; - } else { - return false; + valid = true; } - } catch (jsonError) { - return false; - } + } catch (jsonError) {} + return valid; } }