Skip to content

Instantly share code, notes, and snippets.

@sheppardjm
Created October 26, 2021 16:13
Show Gist options
  • Save sheppardjm/d268f7e9c653bcffa25a2387c85180dc to your computer and use it in GitHub Desktop.
Save sheppardjm/d268f7e9c653bcffa25a2387c85180dc to your computer and use it in GitHub Desktop.
Mocking Formik connect HOC and useFormikContext for Jest
const mockInitialValue = { test: "" };
const mockSetValue = { test: "" };
const mockErrors = { test: "" };
jest.mock("../path/to/formik", () => ({
useFormikContext: jest.fn().mockImplementation(() => {
return {
setFieldValue: () => {
return mockSetValue;
},
values: mockInitialValue, //allowed if named ^mock{LowerCamel}
errors: mockErrors,
};
}),
connect: (Component) => (props) => {
return <Component {...props} />;
},
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment