diff --git a/packages/uniforms-patternfly/src/DateField.tsx b/packages/uniforms-patternfly/src/DateField.tsx index c7dfd6c86ee..7de8ffdabdd 100644 --- a/packages/uniforms-patternfly/src/DateField.tsx +++ b/packages/uniforms-patternfly/src/DateField.tsx @@ -74,12 +74,11 @@ function DateField({ onChange, ...props }: DateFieldProps) { return false; } - const dateValue = new DateConstructor(props.value); if (props.min) { const minDate = new DateConstructor(props.min); if (minDate.toString() === "Invalid Date") { return false; - } else if (dateValue < minDate) { + } else if (props.value < minDate) { return `Should be after ${minDate.toISOString()}`; } } @@ -87,7 +86,7 @@ function DateField({ onChange, ...props }: DateFieldProps) { const maxDate = new DateConstructor(props.max); if (maxDate.toString() === "Invalid Date") { return false; - } else if (dateValue > maxDate) { + } else if (props.value > maxDate) { return `Should be before ${maxDate.toISOString()}`; } } diff --git a/packages/uniforms-patternfly/tests/DateField.test.tsx b/packages/uniforms-patternfly/tests/DateField.test.tsx index 3655f390c01..8743c955cad 100644 --- a/packages/uniforms-patternfly/tests/DateField.test.tsx +++ b/packages/uniforms-patternfly/tests/DateField.test.tsx @@ -23,51 +23,47 @@ import { render, screen, fireEvent } from "@testing-library/react"; import { usingUniformsContext } from "./test-utils"; test(" - renders an input", () => { - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); }); test(" - renders a input with correct id (inherited)", () => { - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); }); test(" - renders a input with correct id (specified)", () => { - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); expect(screen.getByTestId("date-field").getAttribute("id")).toBe("y"); }); test(" - renders a input with correct name", () => { - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); expect(screen.getByTestId("date-field").getAttribute("name")).toBe("x"); }); test(" - renders an input with correct disabled state", () => { - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); expect(screen.getByTestId("date-field") as HTMLInputElement).toBeDisabled(); }); test(" - renders a input with correct label (specified)", () => { - render( - usingUniformsContext(, { x: { type: "string" } }) - ); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); expect(screen.getByText("DateFieldLabel")).toBeInTheDocument(); }); test(" - renders a input with correct label (specified)", () => { - render( - usingUniformsContext(, { x: { type: "string" } }) - ); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); expect(screen.getByText("DateFieldLabel")).toBeInTheDocument(); @@ -75,7 +71,7 @@ test(" - renders a input with correct label (specified)", () => { }); test(" - renders a input with correct value (default)", () => { - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); expect(screen.getByTestId("date-field")).toBeInTheDocument(); expect((screen.getByTestId("date-field") as HTMLInputElement).value).toBe(""); @@ -83,7 +79,7 @@ test(" - renders a input with correct value (default)", () => { test(" - renders a input with correct value (model)", () => { const now = new Date(); - render(usingUniformsContext(, { x: { type: "string" } }, { model: { x: now } })); + render(usingUniformsContext(, { x: { type: Date } }, { model: { x: now } })); const stringfyDate = now.toISOString().split(":").slice(0, 2).join(":"); expect(screen.getByTestId("date-field")).toBeInTheDocument(); @@ -93,7 +89,7 @@ test(" - renders a input with correct value (model)", () => { test(" - renders a input which correctly reacts on change", () => { const onChange = jest.fn(); - render(usingUniformsContext(, { x: { type: "string" } }, { onChange })); + render(usingUniformsContext(, { x: { type: Date } }, { onChange })); const input = screen.getByTestId("date-field") as HTMLInputElement; fireEvent.change(input, { target: { value: "2000-04-04T10:20" } }); @@ -107,7 +103,7 @@ test(" - renders a input which correctly reacts on change (empty valu render( usingUniformsContext( , - { x: { type: "string" } }, + { x: { type: Date } }, { onChange } ) ); @@ -121,7 +117,7 @@ test(" - renders a input which correctly reacts on change (empty valu test(" - renders a input which correctly reacts on change (empty)", () => { const onChange = jest.fn(); - render(usingUniformsContext(, { x: { type: "string" } })); + render(usingUniformsContext(, { x: { type: Date } })); const input = screen.getByTestId("date-field") as HTMLInputElement; fireEvent.change(input, { target: { value: "" } }); @@ -132,7 +128,7 @@ test(" - renders a input which correctly reacts on change (empty)", ( test(" - renders a input which correctly reacts on change (invalid)", () => { const onChange = jest.fn(); - render(usingUniformsContext(, { x: { type: "string" } }, { onChange })); + render(usingUniformsContext(, { x: { type: Date } }, { onChange })); const input = screen.getByTestId("date-field") as HTMLInputElement; fireEvent.change(input, { target: { value: "10:00" } }); @@ -146,7 +142,7 @@ test(" - renders a input which correctly reacts on change (valid)", ( render( usingUniformsContext( , - { x: { type: "string" } }, + { x: { type: Date } }, { onChange } ) ); @@ -160,7 +156,7 @@ test(" - renders a input which correctly reacts on change (valid)", ( test(" - renders a input which correctly reacts on change (year bigger than 9999)", () => { const onChange = jest.fn(); - render(usingUniformsContext(, { x: { type: "string" } }, { onChange })); + render(usingUniformsContext(, { x: { type: Date } }, { onChange })); const input = screen.getByTestId("date-field") as HTMLInputElement; fireEvent.change(input, { target: { value: "121212-12-12T12:12" } }); @@ -173,7 +169,7 @@ test(" - test max property - valid", () => { usingUniformsContext( , { - x: { type: "string" }, + x: { type: Date }, } ) ); @@ -186,7 +182,7 @@ test(" - test max property - invalid", () => { usingUniformsContext( , { - x: { type: "string" }, + x: { type: Date }, } ) ); @@ -199,7 +195,7 @@ test(" - test min property - valid", () => { usingUniformsContext( , { - x: { type: "string" }, + x: { type: Date }, } ) ); @@ -212,7 +208,7 @@ test(" - test min property - invalid", () => { usingUniformsContext( , { - x: { type: "string" }, + x: { type: Date }, } ) );