2020-08-20 14:14:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, screen, act } from '@testing-library/react';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { usePositionRelativeToViewport } from '../use-position-relative-to-viewport';
|
|
|
|
|
|
|
|
describe( 'usePositionRelativeToViewport', () => {
|
|
|
|
function setup() {
|
|
|
|
const TestComponent = () => {
|
2022-06-15 09:56:52 +00:00
|
|
|
const [ referenceElement, positionRelativeToViewport ] =
|
|
|
|
usePositionRelativeToViewport();
|
2020-08-20 14:14:12 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ referenceElement }
|
|
|
|
{ positionRelativeToViewport === 'below' && (
|
|
|
|
<p data-testid="below"></p>
|
|
|
|
) }
|
|
|
|
{ positionRelativeToViewport === 'visible' && (
|
|
|
|
<p data-testid="visible"></p>
|
|
|
|
) }
|
|
|
|
{ positionRelativeToViewport === 'above' && (
|
|
|
|
<p data-testid="above"></p>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return render( <TestComponent /> );
|
|
|
|
}
|
|
|
|
|
|
|
|
it( "calls IntersectionObserver's `observe` and `unobserve` events", async () => {
|
|
|
|
const observe = jest.fn();
|
|
|
|
const unobserve = jest.fn();
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
IntersectionObserver = jest.fn( () => ( {
|
|
|
|
observe,
|
|
|
|
unobserve,
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
const { unmount } = setup();
|
|
|
|
|
|
|
|
expect( observe ).toHaveBeenCalled();
|
|
|
|
unmount();
|
|
|
|
expect( unobserve ).toHaveBeenCalled();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it.each`
|
2020-09-07 17:31:10 +00:00
|
|
|
position | isIntersecting | top
|
|
|
|
${ 'visible' } | ${ true } | ${ 0 }
|
|
|
|
${ 'below' } | ${ false } | ${ 10 }
|
|
|
|
${ 'above' } | ${ false } | ${ 0 }
|
|
|
|
${ 'above' } | ${ false } | ${ -10 }
|
2020-08-20 14:14:12 +00:00
|
|
|
`(
|
|
|
|
"position relative to viewport is '$position' with isIntersecting=$isIntersecting and top=$top",
|
|
|
|
( { position, isIntersecting, top } ) => {
|
|
|
|
let intersectionObserverCallback = ( entries ) => entries;
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
IntersectionObserver = jest.fn( ( callback ) => {
|
|
|
|
// @ts-ignore
|
|
|
|
intersectionObserverCallback = callback;
|
|
|
|
|
|
|
|
return {
|
|
|
|
observe: () => void null,
|
|
|
|
unobserve: () => void null,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
setup();
|
|
|
|
|
|
|
|
act( () => {
|
|
|
|
intersectionObserverCallback( [
|
|
|
|
{ isIntersecting, boundingClientRect: { top } },
|
|
|
|
] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
expect( screen.getAllByTestId( position ) ).toHaveLength( 1 );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|