Skip to content

Commit 5e6320f

Browse files
Add tests for formik Select initial value handling (#2503)
1 parent 8d514e1 commit 5e6320f

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

tests/Select.test.jsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import React from "react";
22

33
import { render, screen } from "@testing-library/react";
44
import userEvent from "@testing-library/user-event";
5+
import { useField, Formik } from "formik";
56
import { evolve } from "ramda";
67

78
import { Select } from "components";
9+
import SelectField from "formikcomponents/Select";
810

911
const options = [
1012
{ label: "Option 1", value: "option-1" },
@@ -251,4 +253,82 @@ describe("Select", () => {
251253
expect(screen.getByText("Group 2 - Option 1")).toBeInTheDocument();
252254
expect(screen.queryByText("Group 2 - Option 2")).not.toBeInTheDocument();
253255
});
256+
257+
it("should correctly fetch initial value for non-grouped options", () => {
258+
const FormikSelectWrapper = () => {
259+
const [field] = useField("test-select");
260+
261+
return (
262+
<div>
263+
<span data-testid="field-value">{field.value}</span>
264+
<SelectField
265+
{...{ options }}
266+
label="Test Select"
267+
name="test-select"
268+
/>
269+
</div>
270+
);
271+
};
272+
273+
const TestComponent = () => (
274+
<Formik initialValues={{ "test-select": "option-2" }} onSubmit={() => {}}>
275+
<FormikSelectWrapper />
276+
</Formik>
277+
);
278+
279+
render(<TestComponent />);
280+
281+
expect(screen.getByText("Option 2")).toBeInTheDocument();
282+
expect(screen.getByTestId("field-value")).toHaveTextContent("option-2");
283+
});
284+
285+
it("should correctly fetch initial value for grouped options", () => {
286+
const groupedOptions = [
287+
{
288+
label: "Group 1",
289+
options: [
290+
{ label: "Group 1 - Option 1", value: "group1-option1" },
291+
{ label: "Group 1 - Option 2", value: "group1-option2" },
292+
],
293+
},
294+
{
295+
label: "Group 2",
296+
options: [
297+
{ label: "Group 2 - Option 1", value: "group2-option1" },
298+
{ label: "Group 2 - Option 2", value: "group2-option2" },
299+
],
300+
},
301+
];
302+
303+
const FormikSelectWrapper = () => {
304+
const [field] = useField("test-grouped-select");
305+
306+
return (
307+
<div>
308+
<span data-testid="field-value">{field.value}</span>
309+
<SelectField
310+
label="Test Grouped Select"
311+
name="test-grouped-select"
312+
options={groupedOptions}
313+
/>
314+
</div>
315+
);
316+
};
317+
318+
const TestComponent = () => (
319+
<Formik
320+
initialValues={{ "test-grouped-select": "group2-option1" }}
321+
onSubmit={() => {}}
322+
>
323+
<FormikSelectWrapper />
324+
</Formik>
325+
);
326+
327+
render(<TestComponent />);
328+
329+
expect(screen.getByText("Group 2 - Option 1")).toBeInTheDocument();
330+
expect(screen.getByTestId("field-value")).toHaveTextContent(
331+
"group2-option1"
332+
);
333+
});
254334
});

0 commit comments

Comments
 (0)