From 941981f8fc9d415f620457c7f75df233e810b073 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Thu, 2 Feb 2023 23:42:59 +0100 Subject: [PATCH] Add a component test for `MultiLineInput` (#4287) --- .../cypress/component/MultiLineInput.cy.tsx | 155 ++++++++++++++++++ .../multi-line-input/MultiLineInput.tsx | 1 + 2 files changed, 156 insertions(+) create mode 100644 apps/admin-ui/cypress/component/MultiLineInput.cy.tsx diff --git a/apps/admin-ui/cypress/component/MultiLineInput.cy.tsx b/apps/admin-ui/cypress/component/MultiLineInput.cy.tsx new file mode 100644 index 0000000000..83a198f121 --- /dev/null +++ b/apps/admin-ui/cypress/component/MultiLineInput.cy.tsx @@ -0,0 +1,155 @@ +import { + ActionGroup, + Button, + Form, + FormGroup, + Page, + PageSection, +} from "@patternfly/react-core"; +import { mount } from "cypress/react"; +import { useEffect } from "react"; +import { FormProvider, useForm } from "react-hook-form"; +import { + MultiLineInput, + MultiLineInputProps, +} from "../../src/components/multi-line-input/MultiLineInput"; + +type MultiLineInputTestProps = Omit & { + submit: (values: any) => void; + defaultValues?: string | string[]; +}; + +describe("MultiLineInput", () => { + const MultiLineInputTest = ({ + submit, + defaultValues, + ...rest + }: MultiLineInputTestProps) => { + const form = useForm(); + + useEffect(() => { + form.setValue("name", defaultValues || ""); + }, [form.setValue]); + + return ( + + + +
+ + + + + + +
+
+
+
+ ); + }; + + it("basic interaction", () => { + const submit = cy.spy().as("onSubmit"); + mount(); + + cy.get("input").should("exist"); + cy.findByTestId("name0").type("value"); + cy.findAllByTestId("save").click(); + cy.get("@onSubmit").should("have.been.calledWith", { name: ["value"] }); + }); + + it("add values", () => { + const submit = cy.spy().as("onSubmit"); + mount(); + + cy.findByTestId("name0").type("value"); + cy.findByTestId("addValue").click(); + cy.findByTestId("name1").type("value1"); + cy.findByTestId("addValue").click(); + cy.findByTestId("name2").type("value2"); + cy.findAllByTestId("save").click(); + cy.get("@onSubmit").should("have.been.calledWith", { + name: ["value", "value1", "value2"], + }); + }); + + it("from existing values as string", () => { + const submit = cy.spy().as("onSubmit"); + mount( + + ); + + cy.findByTestId("name0").should("have.value", "one"); + cy.findByTestId("name1").should("have.value", "two"); + cy.findByTestId("name2").should("have.value", "three"); + cy.findByTestId("addValue").click(); + cy.findByTestId("name3").type("four"); + cy.findAllByTestId("save").click(); + cy.get("@onSubmit").should("have.been.calledWith", { + name: "one##two##three##four", + }); + }); + + it("from existing values as string[]", () => { + const submit = cy.spy().as("onSubmit"); + mount( + + ); + + cy.findByTestId("name0").should("have.value", "one"); + cy.findByTestId("name1").should("have.value", "two"); + cy.findByTestId("name2").should("have.value", "three"); + cy.findByTestId("remove0").click(); + cy.findAllByTestId("save").click(); + cy.get("@onSubmit").should("have.been.calledWith", { + name: ["two", "three"], + }); + }); + + it("remove test", () => { + const submit = cy.spy().as("onSubmit"); + mount( + + ); + + cy.findByTestId("remove0").click(); + cy.findByTestId("remove2").click(); + cy.findByTestId("remove2").click(); + cy.findAllByTestId("save").click(); + cy.get("@onSubmit").should("have.been.calledWith", { + name: ["two", "three", "six"], + }); + }); + + it("add / update test", () => { + const submit = cy.spy().as("onSubmit"); + mount(); + + cy.findByTestId("name0").type("-one"); + cy.findByTestId("addValue").click(); + cy.findByTestId("name1").type("twos"); + cy.findAllByTestId("save").click(); + cy.get("@onSubmit").should("have.been.calledWith", { + name: ["one-one", "twos"], + }); + }); +}); diff --git a/apps/admin-ui/src/components/multi-line-input/MultiLineInput.tsx b/apps/admin-ui/src/components/multi-line-input/MultiLineInput.tsx index 90e4429c0a..83ca9ba0bd 100644 --- a/apps/admin-ui/src/components/multi-line-input/MultiLineInput.tsx +++ b/apps/admin-ui/src/components/multi-line-input/MultiLineInput.tsx @@ -90,6 +90,7 @@ export const MultiLineInput = ({ {...rest} />