Replies: 8 comments 3 replies
-
What do you mean by "mock"? What's your use-case? |
Beta Was this translation helpful? Give feedback.
-
Any luck here? |
Beta Was this translation helpful? Give feedback.
-
I ended up getting around this by wrapping the component I want to test in another component that can call useForm and pass control down.
and now in your jest / rtl tests you can render your component like this:
|
Beta Was this translation helpful? Give feedback.
-
I found a hack that works for my case
|
Beta Was this translation helpful? Give feedback.
-
I had a problem similar to yours (receive control by props). Then I applied a workaround such like this:
|
Beta Was this translation helpful? Give feedback.
-
any solution? |
Beta Was this translation helpful? Give feedback.
-
Do you need to mock it? Is the code below not a suitable solution? import { useForm } from "react-hook-form";
import { render } from "@testing-library/react";
describe("Input", () => {
it("should render truthy", () => {
const form = useForm();
const { baseElement } = render(
<Input control={form.control} name="fullName" />
);
expect(baseElement).toBeTruthy();
});
}); After testing it out I managed to get this code working: const InputInjected = () => {
const form = useForm();
return <Input control={form.control} name="test" />;
};
describe('Input', () => {
it('should render successfully', () => {
const { baseElement } = render(<InputInjected />);
expect(baseElement).toBeTruthy();
});
}); |
Beta Was this translation helpful? Give feedback.
-
The tests used internally have some good examples of how this can be done https://github.com/react-hook-form/react-hook-form/blob/master/src/__tests__/useController.test.tsx This is around L34
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have created custom component for form, and I am passing control from parent component. I am trying to write unit test case for this component using jest. How do I mock this control object?
Beta Was this translation helpful? Give feedback.
All reactions