Skip to content

Commit

Permalink
Add initialErrors to Form (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed Mar 18, 2021
1 parent a9d7780 commit 3224e82
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ Form.DEFAULT_PROPS = DEFAULT_PROPS;

function Form(_props) {
const props = { ...DEFAULT_PROPS, ..._props };
const { initialValues, onSubmit, debug, children, testId } = props;
const {
initialValues,
initialErrors,
onSubmit,
debug,
children,
testId,
} = props;
const [state, setState] = useState({
values: initialValues,
errors: {},
errors: initialErrors ?? {},
shouldValidateOnChange: false,
namesToValidate: null,
submitStatus: "READY",
Expand Down Expand Up @@ -301,6 +308,7 @@ function Form(_props) {
Form.propTypes = {
...responsiveWidthType,
initialValues: PropTypes.object.isRequired,
initialErrors: PropTypes.object,
onSubmit: PropTypes.func,
debug: PropTypes.bool,
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]).isRequired,
Expand Down
27 changes: 24 additions & 3 deletions src/components/Form.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/prop-types */
import React, { useState } from "react";
import "@testing-library/jest-dom/extend-expect";
import {
Expand Down Expand Up @@ -45,7 +46,6 @@ const hungryOptions = [
},
];

// eslint-disable-next-line react/prop-types
function SimpleForm({ testId }) {
const initialValues = {
name: "",
Expand All @@ -58,8 +58,12 @@ function SimpleForm({ testId }) {
);
}

// eslint-disable-next-line react/prop-types
function ComplexForm({ initialValues, onSubmit, unMountFormOnSubmit = false }) {
function ComplexForm({
initialValues,
initialErrors,
onSubmit,
unMountFormOnSubmit = false,
}) {
const [isSubmitted, setIsSubmitted] = useState(false);
const formInitialValues = {
name: "",
Expand Down Expand Up @@ -90,6 +94,7 @@ function ComplexForm({ initialValues, onSubmit, unMountFormOnSubmit = false }) {
return (
<Form
initialValues={formInitialValues}
initialErrors={initialErrors}
onSubmit={(args) => {
onSubmit(args);

Expand Down Expand Up @@ -136,6 +141,7 @@ function ComplexForm({ initialValues, onSubmit, unMountFormOnSubmit = false }) {
</Form>
);
}
// eslint-disable react/prop-types

describe("Form", () => {
it("renders a form", () => {
Expand Down Expand Up @@ -286,6 +292,21 @@ describe("Form", () => {
});
});

it("with initialErrors", () => {
const initialErrors = {
name: ["This name is already taken."],
aboutYourself: ["You can't use inappropriate words.", "Max 500 words."],
};

render(<ComplexForm initialErrors={initialErrors} />);

expect(screen.getByText("This name is already taken.")).toBeInTheDocument();
expect(
screen.getByText("You can't use inappropriate words.")
).toBeInTheDocument();
expect(screen.getByText("Max 500 words.")).toBeInTheDocument();
});

it("with testId", () => {
const { container } = render(<SimpleForm testId="my-form" />);

Expand Down

1 comment on commit 3224e82

@vercel
Copy link

@vercel vercel bot commented on 3224e82 Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.