How can I mock the following hook in Jest?
I use this hook in another hook, When I want to test the other hook, I need to mock useScrollDetect and remove the throttle part, than means I need to immediately call onScroll on scroll events
For educational purpose, I don't want to mock lodash.throttle
use-scroll-detect.ts
import { useCallback, useEffect, useRef } from 'react';
import { throttle } from 'lodash';
/**
* A hook to listen to the scroll event
* @param onScroll Scroll listener
* @param throttleTime Throttling time of the events
* @param enable Enable or disable the listener
*/
const useScrollDetect = (
onScroll: (e: Event) => void,
throttleTime = 100,
enable = true
) => {
const handleScrollThrottle = useCallback(
throttle(onScroll, throttleTime, {
leading: false,
trailing: true,
}),
[onScroll, throttleTime]
);
useEffect(() => {
if (enable) {
window.addEventListener('scroll', handleScrollThrottle, {
passive: true,
});
}
return () => {
if (enable) {
window.removeEventListener('scroll', handleScrollThrottle);
}
};
}, [enable, handleScrollThrottle]);
};
export { useScrollDetect };