Skip to content

Commit

Permalink
fix(Field): fix Select is not updating its value
Browse files Browse the repository at this point in the history
  • Loading branch information
mfal committed Nov 15, 2024
1 parent 80449fa commit 4b5bd4a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { beforeEach, expect, vitest } from "vitest";
import { useForm } from "react-hook-form";
import { Form, typedField } from "@/integrations/react-hook-form";
import { Option, Select } from "@/components/Select";
import React from "react";
import { Button } from "@/components/Button";
import { Label } from "@/components/Label";
import { act, fireEvent, render, screen } from "@testing-library/react";

const handleSubmit = vitest.fn();

beforeEach(() => {
vitest.resetAllMocks();
});

describe("Select field", () => {
interface Values {
select: "baz" | "bar" | "bam";
}

const TestForm = () => {
const form = useForm<Values>();
const Field = typedField(form);

return (
<Form form={form} onSubmit={(values) => handleSubmit(values)}>
<Button onPress={() => form.setValue("select", "bar")}>
Set select
</Button>
<Field name="select">
<Select data-testid="select">
<Label>Select</Label>
<Option value="baz">Baz</Option>
<Option value="bar">Bar</Option>
<Option value="bam">Bam</Option>
</Select>
</Field>
<Button type="submit">Submit</Button>
</Form>
);
};

test("set value is displayed in button", async () => {
render(<TestForm />);
await act(() => fireEvent.click(screen.getByText("Set select")));
expect(screen.getByTestId("select")).toHaveTextContent("Bar");
});

test("set value is used in submit handler", async () => {
render(<TestForm />);

await act(() => fireEvent.click(screen.getByText("Set select")));
await act(() => fireEvent.submit(screen.getByText("Submit")));

expect(handleSubmit).toHaveBeenCalledWith({
select: "bar",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function Field<T extends FieldValues>(props: Props<T>) {
TextField: formControlProps,
Select: {
...formControlProps,
defaultSelectedKey: formControlProps.value,
selectedKey: formControlProps.value,
},
Slider: formControlProps,
DatePicker: formControlProps,
Expand Down

0 comments on commit 4b5bd4a

Please sign in to comment.