I don't really know how to mock inline function in react component's child
My stack: sinon
, chai
, enzyme
;
Component usage:
<ListItem onClick={() => someFn()} />
Component's render:
render() {
return (
<li>
<a href="#" onClick={e => {
e.preventDefault();
this.props.onClick();
}}
> whatever </a>
</li>
);
}
Here we have onClick
that calls e.preventDefault()
. How to tell for <a href>
(link
) to not call e.preventDefault()
? How can I mock that onClick
?
Below what I have try in tests:
Shallow copy setup
function setup() {
const someFn = sinon.stub();
const component = shallow(
<ListItem
onClick={() => {
someFn();
}}
/>
);
return {
component: component,
actions: someFn,
link: component.find('a'),
listItem: component.find('li'),
}
}
And the test
it('simulates click events', () => {
const { link, actions } = setup();
link.simulate('click'); //Click on <a href>
expect(actions).to.have.property('callCount', 1); //would be good if we'll remove e.preventDefault()
});
Test's output error:
TypeError: Cannot read property 'preventDefault' of undefined