JS - Testing code that uses an IntersectionObserver

前端 未结 6 1667
死守一世寂寞
死守一世寂寞 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:38

    Had a similar stack issue as @Kevin Brotcke, except using their solution resulted in a further TypeScript error:

    Function expression, which lacks return-type annotation, implicitly has an 'any' return type.

    Here's a tweaked solution that worked for me:

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

提交回复
热议问题