UNPKG

5.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var IntersectionObserverMock = /** @class */ (function () {
5 function IntersectionObserverMock() {
6 this.observers = [];
7 this.isUsingMockIntersectionObserver = false;
8 this.originalIntersectionObserver = global.IntersectionObserver;
9 this.originalIntersectionObserverEntry = global
10 .IntersectionObserverEntry;
11 }
12 IntersectionObserverMock.prototype.simulate = function (entry) {
13 var e_1, _a;
14 this.ensureMocked();
15 var arrayOfEntries = Array.isArray(entry) ? entry : [entry];
16 var targets = arrayOfEntries.map(function (_a) {
17 var target = _a.target;
18 return target;
19 });
20 var noCustomTargets = targets.every(function (target) { return target == null; });
21 var _loop_1 = function (observer) {
22 if (noCustomTargets || targets.includes(observer.target)) {
23 observer.callback(arrayOfEntries.map(function (entry) { return normalizeEntry(entry, observer.target); }), observer);
24 }
25 };
26 try {
27 for (var _b = tslib_1.__values(this.observers), _c = _b.next(); !_c.done; _c = _b.next()) {
28 var observer = _c.value;
29 _loop_1(observer);
30 }
31 }
32 catch (e_1_1) { e_1 = { error: e_1_1 }; }
33 finally {
34 try {
35 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
36 }
37 finally { if (e_1) throw e_1.error; }
38 }
39 };
40 IntersectionObserverMock.prototype.mock = function () {
41 var _this = this;
42 if (this.isUsingMockIntersectionObserver) {
43 throw new Error('IntersectionObserver is already mocked, but you tried to mock it again.');
44 }
45 this.isUsingMockIntersectionObserver = true;
46 var setObservers = function (setter) {
47 return (_this.observers = setter(_this.observers));
48 };
49 global.IntersectionObserverEntry = /** @class */ (function () {
50 function IntersectionObserverEntry() {
51 }
52 return IntersectionObserverEntry;
53 }());
54 Object.defineProperty(IntersectionObserverEntry.prototype, 'intersectionRatio', {
55 get: function () {
56 return 0;
57 },
58 });
59 global.IntersectionObserver = /** @class */ (function () {
60 function FakeIntersectionObserver(callback, options) {
61 this.callback = callback;
62 this.options = options;
63 }
64 FakeIntersectionObserver.prototype.observe = function (target) {
65 var _this = this;
66 setObservers(function (observers) { return tslib_1.__spread(observers, [
67 {
68 source: _this,
69 target: target,
70 callback: _this.callback,
71 options: _this.options,
72 },
73 ]); });
74 };
75 FakeIntersectionObserver.prototype.disconnect = function () {
76 var _this = this;
77 setObservers(function (observers) {
78 return observers.filter(function (observer) { return observer.source !== _this; });
79 });
80 };
81 FakeIntersectionObserver.prototype.unobserve = function (target) {
82 var _this = this;
83 setObservers(function (observers) {
84 return observers.filter(function (observer) {
85 return !(observer.target === target && observer.source === _this);
86 });
87 });
88 };
89 return FakeIntersectionObserver;
90 }());
91 };
92 IntersectionObserverMock.prototype.restore = function () {
93 if (!this.isUsingMockIntersectionObserver) {
94 throw new Error('IntersectionObserver is already real, but you tried to restore it again.');
95 }
96 global.IntersectionObserver = this.originalIntersectionObserver;
97 global.IntersectionObserverEntry = this.originalIntersectionObserverEntry;
98 this.isUsingMockIntersectionObserver = false;
99 this.observers.length = 0;
100 };
101 IntersectionObserverMock.prototype.isMocked = function () {
102 return this.isUsingMockIntersectionObserver;
103 };
104 IntersectionObserverMock.prototype.ensureMocked = function () {
105 if (!this.isUsingMockIntersectionObserver) {
106 throw new Error('You must call intersectionObserver.mock() before interacting with the fake IntersectionObserver.');
107 }
108 };
109 return IntersectionObserverMock;
110}());
111exports.default = IntersectionObserverMock;
112function normalizeEntry(entry, target) {
113 var isIntersecting = entry.isIntersecting == null
114 ? Boolean(entry.intersectionRatio)
115 : entry.isIntersecting;
116 var intersectionRatio = entry.intersectionRatio || (isIntersecting ? 1 : 0);
117 return {
118 boundingClientRect: entry.boundingClientRect || target.getBoundingClientRect(),
119 intersectionRatio: intersectionRatio,
120 intersectionRect: entry.intersectionRect || target.getBoundingClientRect(),
121 isIntersecting: isIntersecting,
122 rootBounds: entry.rootBounds || document.body.getBoundingClientRect(),
123 target: target,
124 time: entry.time || Date.now(),
125 };
126}