0

I have been trying to write unit test case for the following case. Adding a scroll listener under componentDidMount to call infinity scroll method (Method covers under lodash throttle) Code as follows: Component

componentDidMount() {
  window.addEventListener(
      'scroll',
      _.throttle(this.onScroll, 100)
  );
}
this.onScroll(){
 // something to trigger api
}

Test

window.addEventListener = jest.fn();
wrapper = setup(); // Mount a component
expect(window.addEventListener).toHaveBeenCalledWith('scroll', wrapper.instance().onScroll);

My intention is to test whether listener is created or not on mounting. Above case is working when listener created without 'Throttle'. But failing if throttle added.

It would be helpful if somebody can answer my question

stumps_k2001
  • 45
  • 1
  • 5

0 Answers0