8
我真的不知道該怎麼嘲笑inline函數中反應組件的孩子如何嘲笑e.preventDefault在反應組分的孩子
我的堆棧:sinon
,chai
,enzyme
;
部件使用情況:
<ListItem onClick={() => someFn()} />
組件的渲染:
render() {
return (
<li>
<a href="#" onClick={e => {
e.preventDefault();
this.props.onClick();
}}
> whatever </a>
</li>
);
}
在這裏,我們有onClick
調用e.preventDefault()
。如何辨別<a href>
(link
)不要致電e.preventDefault()
?我怎樣才能嘲笑onClick
?
下面我有什麼嘗試測試:
淺複製編排
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'),
}
}
而且測試
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()
});
測試的輸出錯誤:
TypeError: Cannot read property 'preventDefault' of undefined
謝謝,夥計。對我來說真的很有幫助 –
很高興幫助你:) – WitVault