-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.intersection-observer.js
More file actions
42 lines (40 loc) · 1.35 KB
/
Copy pathjest.setup.intersection-observer.js
File metadata and controls
42 lines (40 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable no-underscore-dangle */
// jsdom does not implement IntersectionObserver; stub it so hooks that use it don't throw.
// Plain JS to avoid TypeScript/ESLint restrictions on type assertions and class rules.
//
// The stub records every constructed observer and the elements it observes on `global.ioInstances`
// so tests can drive intersections deterministically. `triggerIntersection(el, isIntersecting)`
// finds the observer watching `el` and invokes its callback with a minimal entry.
global.ioInstances = [];
global.IntersectionObserver = function IntersectionObserver(callback, options) {
const targets = new Set();
const instance = {
callback,
options,
targets,
observe(el) {
targets.add(el);
},
unobserve(el) {
targets.delete(el);
},
disconnect() {
targets.clear();
const i = global.ioInstances.indexOf(instance);
if (i !== -1) global.ioInstances.splice(i, 1);
},
takeRecords() {
return [];
},
};
global.ioInstances.push(instance);
return instance;
};
// Fires an intersection for `el` on whichever observer is watching it.
global.triggerIntersection = function triggerIntersection(el, isIntersecting) {
global.ioInstances.forEach((instance) => {
if (instance.targets.has(el)) {
instance.callback([{ target: el, isIntersecting }], instance);
}
});
};