-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathjest-setup.js
73 lines (69 loc) · 1.97 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import almostEqual from 'almost-equal';
// eslint-disable-next-line import/no-namespace
import * as immutableMatchers from 'jest-immutable-matchers';
import every from 'lodash-es/every';
import zip from 'lodash-es/zip';
jest.addMatchers(immutableMatchers);
expect.extend({
toHaveAlmostEqualElements(received, expected) {
const pass =
received.length === expected.length &&
every(zip(received, expected), ([value1, value2]) =>
almostEqual(value1, value2),
);
if (pass) {
return {
pass: true,
message: () =>
`expected ${received} not to contain approximately equal elements to ${expected}`,
};
}
return {
pass: false,
message: () =>
`expected ${received} to contain approximately equal elements to ${expected}`,
};
},
});
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;