forked from popcodeorg/popcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-setup.js
48 lines (45 loc) · 1.3 KB
/
jest-setup.js
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
43
44
45
46
47
48
// eslint-disable-next-line import/no-namespace
import * as immutableMatchers from 'jest-immutable-matchers';
jest.addMatchers(immutableMatchers);
function createRange() {
// createRange is always called with a Document context
/* eslint-disable no-invalid-this, consistent-this */
const context = this;
let end;
return {
setEndBefore(el) {
end = el;
},
// Finds all text nodes up to the target node, and returns them
// This is used to count newlines for the error reporting
toString() {
let hasFoundNode = false;
const newLines = [];
const iterator = context.createNodeIterator(
context.documentElement,
NodeFilter.SHOW_ALL,
{
acceptNode(node) {
if (hasFoundNode) {
return NodeFilter.FILTER_REJECT;
}
if (node.isEqualNode(end)) {
hasFoundNode = true;
}
if (node.nodeType === Node.TEXT_NODE) {
newLines.push(node.textContent);
}
return NodeFilter.FILTER_ACCEPT;
},
},
);
const nodes = [];
let node;
while ((node = iterator.nextNode())) {
nodes.push(node);
}
return newLines.join('');
},
};
}
global.Document.prototype.createRange = createRange;