@schukai/monster
Version:
Monster is a simple library for creating fast, robust and lightweight websites.
69 lines (54 loc) • 1.63 kB
JavaScript
import {getGlobal} from "../../source/types/global.mjs";
const global = getGlobal();
export function setupIntersectionObserverMock(
{
root = null,
rootMargin = '',
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}) {
const savedImplementation = window.IntersectionObserver;
let lastObject;
class MockIntersectionObserver {
constructor(callback, options) {
this.root = root;
this.rootMargin = rootMargin;
this.thresholds = thresholds;
this.disconnect = disconnect;
this.observe = observe;
this.takeRecords = takeRecords;
this.unobserve = unobserve;
this.callback = callback;
this.options = options;
lastObject = this;
}
enterNode() {
const entries = [];
entries.push({
isIntersecting: true
})
this['callback'](entries, this);
}
}
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
});
Object.defineProperty(global, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
});
return {
restore: function () {
window.IntersectionObserver = savedImplementation;
},
getInstance: function () {
return lastObject;
}
}
}