Skip to content

Commit a072ed2

Browse files
authored
feat: add AbstractVirtualNode for linting (#1627)
* feat: add AbstractVirtualNode for linting * create typescript interface and test * remove typescript for now * forgot one
1 parent 3e807f0 commit a072ed2

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lib/core/base/virtual-node.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
const whitespaceRegex = /[\t\r\n\f]/g;
22

3+
class AbstractVirtualNode {
4+
constructor() {
5+
this.children = [];
6+
this.parent = null;
7+
}
8+
9+
get props() {
10+
throw new Error(
11+
'VirtualNode class must have a "props" object consisting ' +
12+
'of "nodeType" and "nodeName" properties'
13+
);
14+
}
15+
16+
hasClass() {
17+
throw new Error('VirtualNode class must have a "hasClass" function');
18+
}
19+
20+
attr() {
21+
throw new Error('VirtualNode class must have a "attr" function');
22+
}
23+
24+
hasAttr() {
25+
throw new Error('VirtualNode class must have a "hasAttr" function');
26+
}
27+
}
28+
329
// class is unused in the file...
430
// eslint-disable-next-line no-unused-vars
5-
class VirtualNode {
31+
class VirtualNode extends AbstractVirtualNode {
632
/**
733
* Wrap the real node and provide list of the flattened children
834
* @param {Node} node the node in question
935
* @param {VirtualNode} parent The parent VirtualNode
1036
* @param {String} shadowId the ID of the shadow DOM to which this node belongs
1137
*/
1238
constructor(node, parent, shadowId) {
39+
super();
1340
this.shadowId = shadowId;
1441
this.children = [];
1542
this.actualNode = node;
@@ -105,3 +132,5 @@ class VirtualNode {
105132
return this._cache.tabbableElements;
106133
}
107134
}
135+
136+
axe.AbstractVirtualNode = AbstractVirtualNode;

test/core/base/virtual-node.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,56 @@ describe('VirtualNode', function() {
77
node = document.createElement('div');
88
});
99

10+
describe('AbstractVirtualNode', function() {
11+
it('should be a function', function() {
12+
assert.isFunction(axe.AbstractVirtualNode);
13+
});
14+
15+
it('should throw an error when accessing props', function() {
16+
function fn() {
17+
var abstractNode = new axe.AbstractVirtualNode();
18+
if (abstractNode.props.nodeType === 1) {
19+
return;
20+
}
21+
}
22+
23+
assert.throws(fn);
24+
});
25+
26+
it('should throw an error when accessing hasClass', function() {
27+
function fn() {
28+
var abstractNode = new axe.AbstractVirtualNode();
29+
if (abstractNode.hasClass('foo')) {
30+
return;
31+
}
32+
}
33+
34+
assert.throws(fn);
35+
});
36+
37+
it('should throw an error when accessing attr', function() {
38+
function fn() {
39+
var abstractNode = new axe.AbstractVirtualNode();
40+
if (abstractNode.attr('foo') === 'bar') {
41+
return;
42+
}
43+
}
44+
45+
assert.throws(fn);
46+
});
47+
48+
it('should throw an error when accessing hasAttr', function() {
49+
function fn() {
50+
var abstractNode = new axe.AbstractVirtualNode();
51+
if (abstractNode.hasAttr('foo')) {
52+
return;
53+
}
54+
}
55+
56+
assert.throws(fn);
57+
});
58+
});
59+
1060
it('should be a function', function() {
1161
assert.isFunction(VirtualNode);
1262
});

0 commit comments

Comments
 (0)