import { act } from 'react-dom/test-utils' ; import { IntersectionOptions } from 'react-intersection-observer' ; const observerMap = new Map ( ) ; const instanceMap = new Map ( ) ; function IntersectionObserver ( cb , options ) { const instance = { thresholds : Array . isArray ( options . threshold ) ? options . threshold : [ options . threshold ] , root : options . root , rootMargin : options . rootMargin , observe : ( element ) => { instanceMap . set ( element , instance ) ; observerMap . set ( element , cb ) ; } , unobserve : ( element ) => { instanceMap . delete ( element ) ; observerMap . delete ( element ) ; } , disconnect : ( ) => null , } ; return instance ; } param isIntersecting { boolean } function mockAllIsIntersecting ( isIntersecting ) { observerMap . forEach ( ( onChange , element ) => { mockIsIntersecting ( element , isIntersecting ) ; } ) ; } param element { Element } param isIntersecting { boolean } function mockIsIntersecting ( element , isIntersecting ) { const cb = observerMap . get ( element ) ; if ( cb ) { const entry = [ { isIntersecting , target : element , intersectionRatio : isIntersecting ? 1 : 0 , } , ] ; if ( act ) act ( ( ) => cb ( entry ) ) ; else cb ( entry ) ; } else { throw new Error ( 'No IntersectionObserver instance found for element. Is it still mounted in the DOM?' ) ; } } param element { Element } return function intersectionMockInstance ( element ) { return instanceMap . get ( element ) ; } /**
 * Provide our mock implementation of the IntersectionObserver API to the window object
 **/ window . IntersectionObserver = jest . fn ( ) . mockImplementation ( IntersectionObserver ) ; export { IntersectionObserver , mockAllIsIntersecting , mockIsIntersecting , intersectionMockInstance , observerMap , instanceMap , } ;