Skip to content

Instantly share code, notes, and snippets.

@adamjarling
Created September 25, 2020 19:35
Show Gist options
  • Save adamjarling/4d8a4b3b30a93245d1feeb7cc5934d36 to your computer and use it in GitHub Desktop.
Save adamjarling/4d8a4b3b30a93245d1feeb7cc5934d36 to your computer and use it in GitHub Desktop.
/**
* Testing Library utility function to wrap tested component in React Hook Form
* @param {ReactElement} ui A React component
* @param objectParameters
* @param {Object} objectParameters.defaultValues Initial form values to pass into
* React Hook Form, which you can then assert against
*/
export function renderWithReactHookForm(
ui,
{ defaultValues = {} } = {}
) {
let reactHookFormMethods = {};
const Wrapper = ({ children }) => {
const methods = useForm({ defaultValues });
return <FormProvider {...methods}>{children}</FormProvider>;
};
return {
...render(ui, { wrapper: Wrapper })
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment