From: elinuxhenrik Date: Fri, 26 Mar 2021 07:22:16 +0000 (+0100) Subject: Fix bug in NoTypePolicyEditorComponent X-Git-Tag: 2.2.0~49 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=4af3bc930eb59eb9457db01b3b6ccf6b7376aaad;p=portal%2Fnonrtric-controlpanel.git Fix bug in NoTypePolicyEditorComponent The provided json wasn't displayed properly. Change-Id: I6b09218b38d88394b96bb27f721e92ab40aaec49 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 33170db..7c552bc 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 @@ -89,43 +89,40 @@ describe("NoTypePolicyEditorComponent", () => { expect(await formatButton.isDisabled()).toBeTruthy(); }); - it("should format unformatted json", async () => { - const textArea = component.noTypePolicyEditorComponent.instanceForm.get( - "policyJsonTextArea" + it("should send valid json", async () => { + const textAreaHarness: MatInputHarness = await loader.getHarness( + MatInputHarness.with({ selector: "#policyJsonTextArea" }) ); - expect(textArea.value).toEqual('{"A":"A"}'); + expect(await textAreaHarness.getValue()).toEqual('{"A":"A"}'); - component.noTypePolicyEditorComponent.formatJsonInput(); - expect(textArea.value).toEqual('{\n "A": "A"\n}'); - }); + let validJson: string; + component.noTypePolicyEditorComponent.validJson.subscribe( + (json: string) => { + validJson = json; + } + ); - 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" + const textArea: MatInputHarness = await loader.getHarness( + MatInputHarness.with({ selector: "#policyJsonTextArea" }) ); - expect(textArea.value).toEqual('{"A":"A"}'); + expect(await textArea.getValue()).toEqual('{"A":"A"}'); let invalidJson: string; - component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => { - invalidJson = json; - }); + component.noTypePolicyEditorComponent.validJson.subscribe( + (json: string) => { + invalidJson = json; + } + ); - textArea.setValue('{'); + textArea.setValue("{"); expect(invalidJson).toBeFalsy(); }); 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 877e0e3..ece0175 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 @@ -47,14 +47,19 @@ export class NoTypePolicyEditorComponent implements OnInit { constructor() {} ngOnInit(): void { + let initialJson: string; + if (this.policyJson) { + initialJson = formatJsonString(this.policyJson); + } else { + initialJson = "{}"; + } this.instanceForm.addControl( "policyJsonTextArea", - new FormControl(this.policyJson, [ + new FormControl(initialJson, [ Validators.required, this.jsonValidator(), ]) ); - if (!this.policyJson) this.policyJson = "{}"; } get policyJsonTextArea(): AbstractControl {