Fix bug in NoTypePolicyEditorComponent 01/5801/2
authorelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 26 Mar 2021 07:22:16 +0000 (08:22 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 26 Mar 2021 07:38:49 +0000 (08:38 +0100)
The provided json wasn't displayed properly.

Change-Id: I6b09218b38d88394b96bb27f721e92ab40aaec49
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Issue-ID: NONRTRIC-463

webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.spec.ts
webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts

index 33170db..7c552bc 100644 (file)
@@ -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();
   });
 
index 877e0e3..ece0175 100644 (file)
@@ -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 {