Fix json formatting and tests NoTypePolicyEditor 86/5786/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 22 Mar 2021 13:42:46 +0000 (14:42 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 22 Mar 2021 13:42:50 +0000 (14:42 +0100)
Change-Id: Iaf9f2ee891031d7cec54cfffff64cc7b031bc5f4
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 c87a656..33170db 100644 (file)
@@ -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<TestNoTypePolicyEditorComponentHostComponent>;
   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({
index d231b00..5d2398c 100644 (file)
@@ -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;
   }
 }