How to mock e.preventDefault in react component's child
Try this
link.simulate('click', { preventDefault: () => { } });
test('simulates click events', () => { const e = { stopPropagation: jest.fn() }; const component = shallow(<ListItem{...props} />); const li = component.find('li').at(0).childAt(0) li.props().onClick(e) expect(); });
Just to note that this is an issue only when using shallow
enzyme renderer. In case of full DOM renderer mount
, the event object contains the preventDefault
method, therefore you don't have to mock it.