-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXPathNode.spec.js
78 lines (67 loc) · 2.32 KB
/
XPathNode.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
const expect = require('chai').expect;
const XPathNode = require('../src/types/XPathNode');
describe('XPathNode', () => {
it('should be constructable', () => {
expect(
new XPathNode({
tag: 'html',
})
).to.be.an.instanceOf(XPathNode);
});
describe('from string', () => {
it('should be constructable from string', () => {
const nodeFromString = XPathNode.fromString('html[1]');
expect(nodeFromString).to.be.an.instanceOf(XPathNode);
expect(nodeFromString.index).to.be.eq(1);
expect(nodeFromString.tag).to.be.eq('html');
expect(nodeFromString.toString()).to.be.eq('html');
});
it('should support from string attributes', () => {
const nodeFromString = XPathNode.fromString("html[@lang='en']");
expect(nodeFromString).to.be.an.instanceOf(XPathNode);
expect(nodeFromString.attributes).to.be.deep.eq({
lang: 'en',
});
});
it('should support from string attributes with existence selector', () => {
const nodeFromString = XPathNode.fromString('html[@lang]');
expect(nodeFromString).to.be.an.instanceOf(XPathNode);
expect(nodeFromString.attributes).to.be.deep.eq({
lang: true,
});
});
it('should support from string attributes with subset class selector', () => {
const nodeFromString = XPathNode.fromString(
"html[contains(concat(' ', normalize-space(@class), ' '), ' world ')]"
);
expect(nodeFromString).to.be.an.instanceOf(XPathNode);
expect(nodeFromString.attributes).to.be.deep.eq({
class: ['world'],
});
});
it('should fail on fromString from an invalid selector node', () => {
expect(() => {
XPathNode.fromString('/h/t/m/l');
}).to.throw(
'4 XPath nodes. Expected 1.'
);
});
});
describe('to string', () => {
it('should be serializable to string', () => {
expect(
new XPathNode({
tag: 'html',
}).toString()
).to.eq('html');
});
it('should support constructor attributes', () => {
const node = new XPathNode({ tag: 'html', attributes: { lang: 'en' } });
expect(node).to.be.an.instanceOf(XPathNode);
expect(node.attributes).to.be.deep.equal({
lang: 'en',
});
expect(node.toString()).to.be.eq("html[@lang='en']");
});
});
});