Skip to content

Commit

Permalink
Rollback DateField to Date type
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmotta committed Sep 19, 2024
1 parent 52c105d commit e1199c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
5 changes: 2 additions & 3 deletions packages/uniforms-patternfly/src/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,19 @@ 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()}`;
}
}
if (props.max) {
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()}`;
}
}
Expand Down
42 changes: 19 additions & 23 deletions packages/uniforms-patternfly/tests/DateField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,67 +23,63 @@ import { render, screen, fireEvent } from "@testing-library/react";
import { usingUniformsContext } from "./test-utils";

test("<DateField> - renders an input", () => {
render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
});

test("<DateField> - renders a input with correct id (inherited)", () => {
render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
});

test("<DateField> - renders a input with correct id (specified)", () => {
render(usingUniformsContext(<DateField name="x" id="y" />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" id="y" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
expect(screen.getByTestId("date-field").getAttribute("id")).toBe("y");
});

test("<DateField> - renders a input with correct name", () => {
render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
expect(screen.getByTestId("date-field").getAttribute("name")).toBe("x");
});

test("<DateField> - renders an input with correct disabled state", () => {
render(usingUniformsContext(<DateField name="x" disabled />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" disabled />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
expect(screen.getByTestId("date-field") as HTMLInputElement).toBeDisabled();
});

test("<DateField> - renders a input with correct label (specified)", () => {
render(
usingUniformsContext(<DateField required={false} name="x" label="DateFieldLabel" />, { x: { type: "string" } })
);
render(usingUniformsContext(<DateField required={false} name="x" label="DateFieldLabel" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
expect(screen.getByText("DateFieldLabel")).toBeInTheDocument();
});

test("<DateField> - renders a input with correct label (specified)", () => {
render(
usingUniformsContext(<DateField required={true} name="x" label="DateFieldLabel" />, { x: { type: "string" } })
);
render(usingUniformsContext(<DateField required={true} name="x" label="DateFieldLabel" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
expect(screen.getByText("DateFieldLabel")).toBeInTheDocument();
expect(screen.getByText("*")).toBeInTheDocument();
});

test("<DateField> - renders a input with correct value (default)", () => {
render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }));

expect(screen.getByTestId("date-field")).toBeInTheDocument();
expect((screen.getByTestId("date-field") as HTMLInputElement).value).toBe("");
});

test("<DateField> - renders a input with correct value (model)", () => {
const now = new Date();
render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }, { model: { x: now } }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }, { model: { x: now } }));

const stringfyDate = now.toISOString().split(":").slice(0, 2).join(":");
expect(screen.getByTestId("date-field")).toBeInTheDocument();
Expand All @@ -93,7 +89,7 @@ test("<DateField> - renders a input with correct value (model)", () => {
test("<DateField> - renders a input which correctly reacts on change", () => {
const onChange = jest.fn();

render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }, { onChange }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }, { onChange }));

const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: "2000-04-04T10:20" } });
Expand All @@ -107,7 +103,7 @@ test("<DateField> - renders a input which correctly reacts on change (empty valu
render(
usingUniformsContext(
<DateField name="x" value={new Date("2000-04-04T00:00:00.000Z")} />,
{ x: { type: "string" } },
{ x: { type: Date } },
{ onChange }
)
);
Expand All @@ -121,7 +117,7 @@ test("<DateField> - renders a input which correctly reacts on change (empty valu
test("<DateField> - renders a input which correctly reacts on change (empty)", () => {
const onChange = jest.fn();

render(usingUniformsContext(<DateField name="x" onChange={onChange} />, { x: { type: "string" } }));
render(usingUniformsContext(<DateField name="x" onChange={onChange} />, { x: { type: Date } }));

const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: "" } });
Expand All @@ -132,7 +128,7 @@ test("<DateField> - renders a input which correctly reacts on change (empty)", (
test("<DateField> - renders a input which correctly reacts on change (invalid)", () => {
const onChange = jest.fn();

render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }, { onChange }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }, { onChange }));

const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: "10:00" } });
Expand All @@ -146,7 +142,7 @@ test("<DateField> - renders a input which correctly reacts on change (valid)", (
render(
usingUniformsContext(
<DateField name="x" value={new Date("2000-04-04T00:00:00.000Z")} />,
{ x: { type: "string" } },
{ x: { type: Date } },
{ onChange }
)
);
Expand All @@ -160,7 +156,7 @@ test("<DateField> - renders a input which correctly reacts on change (valid)", (
test("<DateField> - renders a input which correctly reacts on change (year bigger than 9999)", () => {
const onChange = jest.fn();

render(usingUniformsContext(<DateField name="x" />, { x: { type: "string" } }, { onChange }));
render(usingUniformsContext(<DateField name="x" />, { x: { type: Date } }, { onChange }));

const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: "121212-12-12T12:12" } });
Expand All @@ -173,7 +169,7 @@ test("<DateField> - test max property - valid", () => {
usingUniformsContext(
<DateField name="x" max={new Date("1999-01-01T00:00:00Z")} value={new Date("1998-12-31T00:00:00Z")} />,
{
x: { type: "string" },
x: { type: Date },
}
)
);
Expand All @@ -186,7 +182,7 @@ test("<DateField> - test max property - invalid", () => {
usingUniformsContext(
<DateField name="x" max={new Date("1999-01-01T00:00:00.000Z")} value={new Date("1999-01-02T00:00:00.000Z")} />,
{
x: { type: "string" },
x: { type: Date },
}
)
);
Expand All @@ -199,7 +195,7 @@ test("<DateField> - test min property - valid", () => {
usingUniformsContext(
<DateField name="x" min={new Date("1999-01-01T00:00:00Z")} value={new Date("1999-01-02T00:00:00Z")} />,
{
x: { type: "string" },
x: { type: Date },
}
)
);
Expand All @@ -212,7 +208,7 @@ test("<DateField> - test min property - invalid", () => {
usingUniformsContext(
<DateField name="x" min={new Date("1999-01-01T00:00:00.000Z")} value={new Date("1998-12-31T00:00:00.000Z")} />,
{
x: { type: "string" },
x: { type: Date },
}
)
);
Expand Down

0 comments on commit e1199c3

Please sign in to comment.