Skip to content

Commit

Permalink
feat: add AbstractVirtualNode for linting (#1627)
Browse files Browse the repository at this point in the history
* feat: add AbstractVirtualNode for linting

* create typescript interface and test

* remove typescript for now

* forgot one
  • Loading branch information
straker committed Jun 12, 2019
1 parent 3e807f0 commit a072ed2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/core/base/virtual-node.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
const whitespaceRegex = /[\t\r\n\f]/g;

class AbstractVirtualNode {
constructor() {
this.children = [];
this.parent = null;
}

get props() {
throw new Error(
'VirtualNode class must have a "props" object consisting ' +
'of "nodeType" and "nodeName" properties'
);
}

hasClass() {
throw new Error('VirtualNode class must have a "hasClass" function');
}

attr() {
throw new Error('VirtualNode class must have a "attr" function');
}

hasAttr() {
throw new Error('VirtualNode class must have a "hasAttr" function');
}
}

// class is unused in the file...
// eslint-disable-next-line no-unused-vars
class VirtualNode {
class VirtualNode extends AbstractVirtualNode {
/**
* Wrap the real node and provide list of the flattened children
* @param {Node} node the node in question
* @param {VirtualNode} parent The parent VirtualNode
* @param {String} shadowId the ID of the shadow DOM to which this node belongs
*/
constructor(node, parent, shadowId) {
super();
this.shadowId = shadowId;
this.children = [];
this.actualNode = node;
Expand Down Expand Up @@ -105,3 +132,5 @@ class VirtualNode {
return this._cache.tabbableElements;
}
}

axe.AbstractVirtualNode = AbstractVirtualNode;
50 changes: 50 additions & 0 deletions test/core/base/virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,56 @@ describe('VirtualNode', function() {
node = document.createElement('div');
});

describe('AbstractVirtualNode', function() {
it('should be a function', function() {
assert.isFunction(axe.AbstractVirtualNode);
});

it('should throw an error when accessing props', function() {
function fn() {
var abstractNode = new axe.AbstractVirtualNode();
if (abstractNode.props.nodeType === 1) {
return;
}
}

assert.throws(fn);
});

it('should throw an error when accessing hasClass', function() {
function fn() {
var abstractNode = new axe.AbstractVirtualNode();
if (abstractNode.hasClass('foo')) {
return;
}
}

assert.throws(fn);
});

it('should throw an error when accessing attr', function() {
function fn() {
var abstractNode = new axe.AbstractVirtualNode();
if (abstractNode.attr('foo') === 'bar') {
return;
}
}

assert.throws(fn);
});

it('should throw an error when accessing hasAttr', function() {
function fn() {
var abstractNode = new axe.AbstractVirtualNode();
if (abstractNode.hasAttr('foo')) {
return;
}
}

assert.throws(fn);
});
});

it('should be a function', function() {
assert.isFunction(VirtualNode);
});
Expand Down

0 comments on commit a072ed2

Please sign in to comment.