Como zombar do e.preventDefault no filho do componente react
Eu realmente não sei como zombar da função embutida no filho do componente reagir
Minha pilha:sinon
, chai
, enzyme
;
Uso de componentes:
<ListItem onClick={() => someFn()} />
Renderização do componente:
render() {
return (
<li>
<a href="#" onClick={e => {
e.preventDefault();
this.props.onClick();
}}
> whatever </a>
</li>
);
}
Aqui temosonClick
que chamae.preventDefault()
. Como contar para<a href>
(link
) para não ligare.preventDefault()
? Como posso zombar dissoonClick
?
Abaixo o que tenho tentado nos testes:
Configuração de cópia rasa
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'),
}
}
E o teste
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()
});
Erro de saída do teste:
TypeError: Cannot read property 'preventDefault' of undefined