Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/components/mui/__tests__/mui-formik-datepicker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Formik, Form } from "formik";
import "@testing-library/jest-dom";
import MuiFormikDatepicker from "../formik-inputs/mui-formik-datepicker";

const renderWithFormik = (props, initialValues = { test_date: null }) =>
render(
<Formik
initialValues={initialValues}
validate={(values) => {
const errors = {};
if (!values.test_date) {
errors.test_date = "This field is required";
}
return errors;
}}
onSubmit={jest.fn()}
>
<Form>
<MuiFormikDatepicker name="test_date" label="Test Date" {...props} />
</Form>
</Formik>
);

describe("MuiFormikDatepicker", () => {
test("shows required marker in label", () => {
renderWithFormik({ required: true });

expect(screen.getByLabelText("Test Date *")).toBeInTheDocument();
});

test("shows validation error when blurring without value", async () => {
renderWithFormik({ required: true });

expect(
screen.queryByText("This field is required")
).not.toBeInTheDocument();

const user = userEvent.setup();
const input = screen.getByLabelText("Test Date *");

Choose a reason for hiding this comment

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

You only asserts the error appears. It does not prove the blur caused it.

expect(screen.queryByText("This field is required")).not.toBeInTheDocument();

Before the interaction to be explicit.

await user.click(input);
await user.tab();

await waitFor(() => {
expect(screen.getByText("This field is required")).toBeInTheDocument();
});
});
});
6 changes: 6 additions & 0 deletions src/components/mui/formik-inputs/mui-formik-datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ import { useField } from "formik";
const MuiFormikDatepicker = ({ name, label, required, ...props }) => {
const [field, meta, helpers] = useField(name);
const requiredLabel = `${label} *`;
const handleBlur = () => {
helpers.setTouched(true, true);
};

return (
<LocalizationProvider dateAdapter={AdapterMoment}>
<DatePicker
value={field.value}
onChange={helpers.setValue}
onClose={handleBlur}
slotProps={{

Choose a reason for hiding this comment

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

NIT: Since this is out of the scope of this PR I will just call it out:

If a caller passes slotProps through ...props, it will override this entire object, breaking the wrapper.

@caseylocker @smarcet For you consideration.

textField: {
name,
label: required ? requiredLabel : label,
onBlur: handleBlur,
error: meta.touched && Boolean(meta.error),
helperText: meta.touched && meta.error,
fullWidth: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const CustomizedForm = ({
label={T.translate(
"edit_sponsor.forms_tab.customized_form.opens_at"
)}
required
/>
</Grid2>
<Grid2 size={4}>
Expand All @@ -142,6 +143,7 @@ const CustomizedForm = ({
label={T.translate(
"edit_sponsor.forms_tab.customized_form.expires_at"
)}
required
/>
</Grid2>
<Grid2 size={12}>
Expand Down