JS - Testing code that uses an IntersectionObserver

前端 未结 6 1652
死守一世寂寞
死守一世寂寞 2020-12-18 17:57

I have a (rather poorly written) javascript component in my application that handles infinite scroll pagination, and i\'m trying to rewrite it to use the IntersectionO

6条回答
  •  攒了一身酷
    2020-12-18 18:51

    None of the posted answered worked for me because of our configuration of TypeScript and React (tsx) we're using. Here's what finally worked:

    beforeEach(() => {
      // IntersectionObserver isn't available in test environment
      const mockIntersectionObserver = jest.fn();
      mockIntersectionObserver.mockReturnValue({
        observe: () => null,
        unobserve: () => null,
        disconnect: () => null
      });
      window.IntersectionObserver = mockIntersectionObserver;
    });
    

提交回复
热议问题