X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Fpolicy%2Fno-type-policy-editor%2Fno-type-policy-editor.component.spec.ts;h=7c552bc1ddd0e9a86bfdb1eb3eef2003fa96f521;hb=HEAD;hp=fb3a6a603a2c666fd441eb1681e6818574facecc;hpb=042a087cf3eea5e6f941ee9add6e1c50cb106e91;p=portal%2Fnonrtric-controlpanel.git 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 fb3a6a6..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 @@ -22,7 +22,6 @@ import { HarnessLoader } from "@angular/cdk/testing"; import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed"; import { Component, ViewChild, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core"; import { ComponentFixture, TestBed } from "@angular/core/testing"; -import { FormBuilder, FormGroup } from "@angular/forms"; import { MatButtonModule } from "@angular/material/button"; import { MatButtonHarness } from "@angular/material/button/testing"; import { MatFormFieldModule } from "@angular/material/form-field"; @@ -34,8 +33,6 @@ import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { NoTypePolicyEditorComponent } from "./no-type-policy-editor.component"; describe("NoTypePolicyEditorComponent", () => { - let formGroup: FormGroup = new FormGroup({}); - let component: TestNoTypePolicyEditorComponentHostComponent; let fixture: ComponentFixture; let loader: HarnessLoader; @@ -54,7 +51,6 @@ describe("NoTypePolicyEditorComponent", () => { NoTypePolicyEditorComponent, TestNoTypePolicyEditorComponentHostComponent, ], - providers: [FormBuilder], }).compileComponents(); fixture = TestBed.createComponent( @@ -69,57 +65,76 @@ describe("NoTypePolicyEditorComponent", () => { expect(component).toBeTruthy(); }); - it("should be added to form group with required validator", async () => { - let textArea: MatInputHarness = await loader.getHarness( - MatInputHarness.with({ selector: "#policyJsonTextArea" }) - ); - - expect(formGroup.get("policyJsonTextArea")).toBeTruthy(); - expect(await textArea.isRequired()).toBeTruthy(); - }); - 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(); }); it("Format button should be disabled when json not valid", async () => { - const ele = formGroup.get("policyJsonTextArea"); + const ele = component.noTypePolicyEditorComponent.instanceForm.get( + "policyJsonTextArea" + ); ele.setValue("{"); - let formatButton: MatButtonHarness = await loader.getHarness( + const formatButton: MatButtonHarness = await loader.getHarness( MatButtonHarness.with({ selector: "#formatButton" }) ); expect(await formatButton.isDisabled()).toBeTruthy(); }); - it("should format unformatted json", async () => { - const textArea = formGroup.get("policyJsonTextArea"); - textArea.setValue('{"A":"A"}'); - component.noTypePolicyEditorComponent.formatJsonInput(); - expect(component.noTypePolicyEditorComponent.policyJson).toEqual( - '{\n "A": "A"\n}' + it("should send valid json", async () => { + const textAreaHarness: MatInputHarness = await loader.getHarness( + MatInputHarness.with({ selector: "#policyJsonTextArea" }) + ); + expect(await textAreaHarness.getValue()).toEqual('{"A":"A"}'); + + let validJson: string; + component.noTypePolicyEditorComponent.validJson.subscribe( + (json: string) => { + validJson = json; + } + ); + + const textArea = component.noTypePolicyEditorComponent.instanceForm.get( + "policyJsonTextArea" + ); + textArea.setValue('{"B":"B"}'); + expect(validJson).toEqual('{"B":"B"}'); + }); + + it("should send null when invalid json", async () => { + const textArea: MatInputHarness = await loader.getHarness( + MatInputHarness.with({ selector: "#policyJsonTextArea" }) ); + expect(await textArea.getValue()).toEqual('{"A":"A"}'); + + let invalidJson: string; + component.noTypePolicyEditorComponent.validJson.subscribe( + (json: string) => { + invalidJson = json; + } + ); + + textArea.setValue("{"); + expect(invalidJson).toBeFalsy(); }); @Component({ selector: `no-type-policy-editor-host-component`, template: ``, }) class TestNoTypePolicyEditorComponentHostComponent { @ViewChild(NoTypePolicyEditorComponent) noTypePolicyEditorComponent: NoTypePolicyEditorComponent; - instanceForm: FormGroup = formGroup; policyJson: string = '{"A":"A"}'; } });