I have a simple React hook:
const useHook = () => {
useEffect(() => {
window.addEventListener('click', handleClick);
}, []);
const handleClick = () => {};
return null;
};
Am I able to somehow test if the handleClick function was called on click event?
I can of course simulate click event in Jest. However how to check if this fn was called?
How about using a count wrapper ?
function countFn(fn) {
let wrapper = function() {
wrapper.called ++;
return fn.apply(this, arguments);
}
wrapper.called = 0;
return wrapper;
}
const handleClick = countFn((e) => {console.log(e)});
handleClick(1);
handleClick(2);
handleClick(3);
console.log(`handleClick called ${handleClick.called} times`);
1
2
3
handleClick called 3 times