Aljullu / react-lazy-load-image-component

React Component to lazy load images and components using a HOC to track window scroll position.
https://www.npmjs.com/package/react-lazy-load-image-component
MIT License
1.46k stars 110 forks source link

Error when test rendering component using React Test Library #112

Open wisnuvb opened 2 years ago

wisnuvb commented 2 years ago

I run the test (RTL) getting error TypeError: Cannot read properties of undefined (reading 'prototype') while rendering component.

Screen Shot 2022-09-28 at 15 21 58

I've tried looking everywhere but can't find a solution. I post here to get enlightenment on how to test this component. The screenshot above shows that I immediately called the component, although not directly, the result was still an error. Thanks 🙏

nextrie commented 1 year ago

I had the same error, found solution in src/utils/intersection-observer.spec.js: Adding

window.IntersectionObserver = {};
window.IntersectionObserverEntry = {
  prototype: {
    isIntersecting: () => null,
  },
};

at the beginning of your test or in setupTests fixes the problem.

washingtonsoares commented 1 year ago

@wisnuvb one other idea is that you can actually mock it in your setupTests.

jest.mock("react-lazy-load-image-component", () => ({
  LazyLoadImage: ({ src, alt }) => <img alt={alt} src={src} />
}));