I want to simulate a click on Button and check if onClick was called or not. Here I am not using JEST or SINON to mock or spy instead using my own spy.
const DeselectAll = (props: Props) => (
<div className="deselectall">
<Button
className="deselectall--button"
icon="minus"
onClick={props.clearItems}
text={translate("deselectall")}
/>
</div>
);
test
function MySpy() {
this.calls = 0;
}
MySpy.prototype.fn = function () {
return () => this.calls++;
}
When(/^Deselect button is clicked$/, function () {
const mySpy = new MySpy();
const mockCallBack = mySpy.fn();
const wrapper = mount(DeselectAll);
const button = enzyme.shallow(React.createElement(Button, { onClick: mockCallBack }));
//error: seems button is not yet
// available
button.find('.deselectall--button').simulate('click');
expect(mySpy.calls).to.be.equal(1);
});
ERROR: Cannot read property 'addEventListener' of null
Any help appreciated!