Skip to content

Commit

Permalink
fix(input-button-name): work with virtual nodes (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Jul 9, 2020
1 parent 70b10b2 commit 63ca388
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
10 changes: 5 additions & 5 deletions lib/checks/shared/non-empty-if-present-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function nonEmptyIfPresentEvaluate(node) {
function nonEmptyIfPresentEvaluate(node, options, virtualNode) {
// Check for 'default' names, which are given to reset and submit buttons
let nodeName = node.nodeName.toUpperCase();
let type = (node.getAttribute('type') || '').toLowerCase();
let label = node.getAttribute('value');
let nodeName = virtualNode.props.nodeName;
let type = (virtualNode.attr('type') || '').toLowerCase();
let label = virtualNode.attr('value');

if (label) {
this.data({
messageKey: 'has-label'
});
}

if (nodeName === 'INPUT' && ['submit', 'reset'].includes(type)) {
if (nodeName === 'input' && ['submit', 'reset'].includes(type)) {
return label === null;
}
return false;
Expand Down
41 changes: 15 additions & 26 deletions test/checks/shared/non-empty-if-present.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,70 @@ describe('non-empty-if-present', function() {
var isEdgeOrIe = typeof input.getAttribute('value') === 'string';

var checkContext = axe.testUtils.MockCheckContext();
var queryFixture = axe.testUtils.queryFixture;

afterEach(function() {
fixture.innerHTML = '';
checkContext.reset();
});

it('should return false if a value is present', function() {
var node = document.createElement('input');
node.setAttribute('type', 'submit');
node.setAttribute('value', 'woohoo');
fixture.appendChild(node);
var vNode = queryFixture(
'<input id="target" type="submit" value="woohoo" />'
);

assert.isFalse(
axe.testUtils
.getCheckEvaluate('non-empty-if-present')
.call(checkContext, node)
.call(checkContext, null, {}, vNode)
);
assert.equal(checkContext._data.messageKey, 'has-label');
});

(isEdgeOrIe ? xit : it)(
'should return true if a value is not present',
function() {
var node = document.createElement('input');
node.setAttribute('type', 'submit');
fixture.appendChild(node);
var vNode = queryFixture('<input id="target" type="submit" />');

assert.isTrue(
axe.testUtils
.getCheckEvaluate('non-empty-if-present')
.call(checkContext, node)
.call(checkContext, null, {}, vNode)
);
assert.isNull(checkContext._data);
}
);

it('should return false if an value is present, but empty', function() {
var node = document.createElement('input');
node.setAttribute('type', 'submit');
node.setAttribute('value', '');
fixture.appendChild(node);
var vNode = queryFixture('<input id="target" type="submit" value="" />');

assert.isFalse(
axe.testUtils
.getCheckEvaluate('non-empty-if-present')
.call(checkContext, node)
.call(checkContext, null, {}, vNode)
);
});

it('should return false if the element is not a submit or reset input', function() {
var node = document.createElement('input');
node.setAttribute('type', 'text');
fixture.appendChild(node);
var vNode = queryFixture('<input id="target" type="text" />');
assert.isFalse(
axe.testUtils
.getCheckEvaluate('non-empty-if-present')
.call(checkContext, node)
.call(checkContext, null, {}, vNode)
);

node = document.createElement('input');
node.setAttribute('type', 'button');
fixture.appendChild(node);
var vNode = queryFixture('<input id="target" type="button" />');
assert.isFalse(
axe.testUtils
.getCheckEvaluate('non-empty-if-present')
.call(checkContext, node)
.call(checkContext, null, {}, vNode)
);

node = document.createElement('button');
node.setAttribute('type', 'submit');
fixture.appendChild(node);
var vNode = queryFixture('<button id="target" type="submit"></button');
assert.isFalse(
axe.testUtils
.getCheckEvaluate('non-empty-if-present')
.call(checkContext, node)
.call(checkContext, null, {}, vNode)
);
});
});

0 comments on commit 63ca388

Please sign in to comment.