Ad
Mock Module Function With Jest/enzyme
following that answer: Jest -- Mock a function called inside a React Component
I have this test (trying to mock fireAnalytics
imported function, like so:
mytest.test.js
`.import { fireAnalytics } from "@module/js-utils/lib";
jest.mock('@module/js-utils/lib', () => ({ fireAnalytics: jest.fn(() => 'hello!') }))`
`
it('renders component', () => {
const WrappedMyComponent = hoc(Foo);
const props = {};
const wrapper = mount(
<WrappedMyComponent {...props} />
);
const event = {
// target: {
// id: 'test'
// }
};
// simulate click should call `handleInputClick`
wrapper.find('input').props().onClick(event);`
component
import { fireAnalytics } from "@module/js-utils/lib";
handleInputClick(e) {
// I need to mock this function. But currently the test
goes here and asks for the e.target.id
fireAnalytics({
event: "",
category: "",
action: `Clicked on the ${e.target.id} input`,
label: ""
});
// rest of the code ....
}
Error when test running
TypeError: Cannot read property 'id' of undefined
248 | event: "",
249 | category: "",
> 250 | action: `Clicked on the ${e.target.id} input`,
251 | label: ""
252 | });
253 |
Any help would be appreciated.
Ad
Answer
You need to use enzyme simulate function. Simulate a click in your input test:
...
wrapper.find('input').simulate('click')
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad