Skip to content

Commit 0fa8c3f

Browse files
committed
add tests to select node
1 parent a94a5ec commit 0fa8c3f

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

test/core/utils/get-xpath.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ describe('axe.utils.getXpath', () => {
33

44
const fixture = document.getElementById('fixture');
55

6+
// @see https://stackoverflow.com/a/14284815/2124254
7+
function getElementByXPath(path) {
8+
return document.evaluate(
9+
path,
10+
document,
11+
() => 'http://www.w3.org/1998/Math/MathML',
12+
XPathResult.FIRST_ORDERED_NODE_TYPE,
13+
null
14+
).singleNodeValue;
15+
}
16+
617
it('should be a function', () => {
718
assert.isFunction(axe.utils.getXpath);
819
});
@@ -14,16 +25,19 @@ describe('axe.utils.getXpath', () => {
1425
const sel = axe.utils.getXpath(node);
1526

1627
assert.equal(sel, "//div[@id='fixture']/div");
28+
assert.equal(node, getElementByXPath(sel));
1729
});
1830

1931
it('should handle special characters', () => {
2032
const node = document.createElement('div');
2133
node.id = 'monkeys#are.animals\\ok';
2234
fixture.appendChild(node);
23-
assert.equal(
24-
axe.utils.getXpath(node),
25-
"//div[@id='monkeys#are.animals\\ok']"
26-
);
35+
36+
const sel = axe.utils.getXpath(node);
37+
38+
assert.equal(sel, "//div[@id='monkeys#are.animals\\ok']");
39+
40+
assert.equal(node, getElementByXPath(sel));
2741
});
2842

2943
it('should stop on unique ID', () => {
@@ -33,6 +47,7 @@ describe('axe.utils.getXpath', () => {
3347

3448
const sel = axe.utils.getXpath(node);
3549
assert.equal(sel, "//div[@id='monkeys']");
50+
assert.equal(node, getElementByXPath(sel));
3651
});
3752

3853
it('should use the nearest unique ID', () => {
@@ -51,6 +66,7 @@ describe('axe.utils.getXpath', () => {
5166

5267
const sel = axe.utils.getXpath(node);
5368
assert.equal(sel, "//div[@id='monkeys']/div");
69+
assert.equal(node, getElementByXPath(sel));
5470
});
5571

5672
it('should not use ids if they are not unique', () => {
@@ -65,6 +81,7 @@ describe('axe.utils.getXpath', () => {
6581
const sel = axe.utils.getXpath(node);
6682

6783
assert.equal(sel, "//div[@id='fixture']/div[2]");
84+
assert.equal(node, getElementByXPath(sel));
6885
});
6986

7087
it('should properly calculate number when siblings are of different type', () => {
@@ -91,23 +108,27 @@ describe('axe.utils.getXpath', () => {
91108
const sel = axe.utils.getXpath(target);
92109

93110
assert.equal(sel, "//div[@id='fixture']/div[2]");
111+
assert.equal(target, getElementByXPath(sel));
94112
});
95113

96114
it('should work on the documentElement', () => {
97115
const sel = axe.utils.getXpath(document.documentElement);
98116
assert.equal(sel, '/html');
117+
assert.equal(document.documentElement, getElementByXPath(sel));
99118
});
100119

101120
it('should work on the body', () => {
102121
const sel = axe.utils.getXpath(document.body);
103122
assert.equal(sel, '/html/body');
123+
assert.equal(document.body, getElementByXPath(sel));
104124
});
105125

106-
it('should work on namespaced elements', () => {
126+
it('should work on namespaced elements', function () {
107127
fixture.innerHTML = '<hx:include>Hello</hx:include>';
108-
const node = fixture.firstChild;
109-
const sel = axe.utils.getXpath(node);
128+
var node = fixture.firstChild;
129+
var sel = axe.utils.getXpath(node);
110130

111131
assert.equal(sel, "//div[@id='fixture']/hx:include");
132+
// couldn't figure out how to use document.evaluate to select an element with namespace
112133
});
113134
});

0 commit comments

Comments
 (0)