Testing a React component that uses jQuery & windo

2019-05-05 03:32发布

问题:

My React component has to respond to resize events, for which I am using jQuery, ie:

//...
componentDidMount: function() {
  $(window).on("resize", function);
}
//..

However, this causes issues with my Jest test, specifically:

- TypeError: Cannot read property 'on' of undefined
    at RadiumEnhancer.React.createClass.componentDidMount (bundles/Opportunities/OpportunityPartial.jsx:21:14)

When stepping through the test it looks like window is defined in a jest test, but $(window) doesn't appear to return anything.

I was under the impression that Jest would examine the required module (jQuery)'s API to construct its mock, but if I'm understanding the problem correctly it seems like this isn't happening?

I can get around this by not mocking jQuery, but obviously that isn't totally preferable.