-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluators.spec.js
89 lines (81 loc) · 2.73 KB
/
evaluators.spec.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const expect = require('chai').expect;
const jsdom = require('jsdom');
const evaluators = require('../src/evaluators');
const { XPathResult } = require('xpath');
function newDocument() {
const options = {};
const doc = jsdom.jsdom(null, options);
doc.parent = doc.defaultView;
return doc;
}
const document = newDocument();
const div = document.createElement('div');
const parentDiv = document.createElement('div');
const childDiv1 = document.createElement('div');
const childDiv2 = document.createElement('div');
childDiv1.innerHTML = 'Hello world.';
childDiv1.className = 'test';
childDiv2.id = 'test';
parentDiv.appendChild(childDiv1);
parentDiv.appendChild(childDiv2);
document.body.appendChild(parentDiv);
describe('evaluators', () => {
describe('evaluateXPath', () => {
it('should return STRING_TYPE from a simple DOM query.', () => {
const xpath = evaluators.evaluateXPath(
document,
'/html/body/div/div[1]',
document,
XPathResult.STRING_TYPE
);
expect(xpath).to.be.equal(
'Hello world.',
'Expected inner string to be returned'
);
});
it('should return Node from a simple DOM query.', () => {
const xpath = evaluators.evaluateXPath(
document,
'/html/body/div/div[1]',
document
);
expect(xpath).to.be.deep.equal(
[childDiv1],
'Expected array of matching elements to be returned'
);
});
});
describe('getElementsBySelector', () => {
it('should support a similar lookup by CSS rule', () => {
const result = evaluators.getElementsBySelector(document, '.test');
expect(result).to.have.length(1);
expect(result[0].className).to.be.eq('test');
});
});
describe('getElementsByXPath', () => {
it('should support a similar lookup by XPath rule', () => {
const result = evaluators.getElementsByXPath(document, '//*[@id="test"]');
expect(result).to.have.length(1);
expect(result[0].id).to.be.eq('test');
});
it('should work in the no results case', () => {
const result = evaluators.getElementsByXPath(document, '//*[@id="abc"]');
expect(result).to.have.length(0);
});
it('should work in the invalid selector', () => {
const result = evaluators.getElementsByXPath(document, '/1!@*#');
expect(result).to.have.length(0);
});
});
describe('getRuleMatchingElements', () => {
it('should support a similar lookup by CSS rule', () => {
// NOTE: opposite parameter order originates in Mozilla code.
const result = evaluators.getRuleMatchingElements(
{ selectorText: '.test' },
document
);
expect(result).to.have.length(1);
expect(result[0].className).to.be.eq('test');
});
});
});