Skip to content

Commit aeae955

Browse files
dawsontoththebuilder
authored andcommitted
fix: Mock additional intersection properties (#286)
All of the IntersectionObserverEntry properties will now be reasonably mocked. https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry
1 parent 9348aab commit aeae955

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/test-utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ beforeAll(() => {
1212
: [options.threshold],
1313
root: options.root,
1414
rootMargin: options.rootMargin,
15+
time: Date.now(),
1516
observe: jest.fn((element: Element) => {
1617
instanceMap.set(element, instance)
1718
observerMap.set(element, cb)
@@ -50,12 +51,17 @@ export function mockAllIsIntersecting(isIntersecting: boolean) {
5051
*/
5152
export function mockIsIntersecting(element: Element, isIntersecting: boolean) {
5253
const cb = observerMap.get(element)
53-
if (cb) {
54+
const instance = instanceMap.get(element)
55+
if (cb && instance) {
5456
const entry = [
5557
{
58+
boundingClientRect: element.getBoundingClientRect(),
59+
intersectionRatio: isIntersecting ? 1 : 0,
60+
intersectionRect: isIntersecting ? element.getBoundingClientRect() : {},
5661
isIntersecting,
62+
rootBounds: instance.root ? instance.root.getBoundingClientRect() : {},
5763
target: element,
58-
intersectionRatio: isIntersecting ? 1 : 0,
64+
time: Date.now() - instance.time,
5965
},
6066
]
6167
if (act) act(() => cb(entry))

0 commit comments

Comments
 (0)