Skip to content

Commit

Permalink
fix: add shadow dom coverage to all checks
Browse files Browse the repository at this point in the history
Closes #690
  • Loading branch information
Marcy Sutton committed Feb 13, 2018
1 parent 6145f7e commit 0e48413
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/checks/navigation/header-present.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
return !!node.querySelector('h1, h2, h3, h4, h5, h6, [role="heading"]');
return !!axe.utils.querySelectorAll(virtualNode, 'h1, h2, h3, h4, h5, h6, [role="heading"]')[0];
3 changes: 2 additions & 1 deletion lib/checks/navigation/internal-link-present.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
return !!node.querySelector('a[href^="#"]');
const links = axe.utils.querySelectorAll(virtualNode, 'a[href]');
return links.some(vLink => vLink.actualNode.getAttribute('href')[0] === '#');
22 changes: 21 additions & 1 deletion test/checks/aria/errormessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ describe('aria-errormessage', function () {
'use strict';

var fixture = document.getElementById('fixture');

var shadowSupported = axe.testUtils.shadowSupport.v1;
var shadowCheckSetup = axe.testUtils.shadowCheckSetup;
var checkContext = axe.testUtils.MockCheckContext();

afterEach(function () {
Expand Down Expand Up @@ -46,4 +47,23 @@ describe('aria-errormessage', function () {
target.setAttribute('aria-describedby', 'plain');
assert.isTrue(checks['aria-errormessage'].evaluate.call(checkContext, target));
});

(shadowSupported ? it : xit)
('should return false if aria-errormessage value crosses shadow boundary', function () {
var params = shadowCheckSetup(
'<div id="target" aria-errormessage="live"></div>',
'<div id="live" aria-live="assertive"></div>'
);
assert.isFalse(checks['aria-errormessage'].evaluate.apply(checkContext, params));
});

(shadowSupported ? it : xit)
('should return true if aria-errormessage and value are inside shadow dom', function () {
var params = shadowCheckSetup(
'<div></div>',
'<div id="target" aria-errormessage="live"</div>' +
'<div id="live" aria-live="assertive"></div>'
);
assert.isTrue(checks['aria-errormessage'].evaluate.apply(checkContext, params));
});
});
7 changes: 7 additions & 0 deletions test/checks/keyboard/has-at-least-one-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ describe('has-at-least-one-main', function () {
var fixture = document.getElementById('fixture');
var checkContext = new axe.testUtils.MockCheckContext();
var checkSetup = axe.testUtils.checkSetup;
var shadowSupported = axe.testUtils.shadowSupport.v1;
var shadowCheckSetup = axe.testUtils.shadowCheckSetup;

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

it('should return false if no div has role property', function() {
Expand Down Expand Up @@ -44,6 +47,10 @@ describe('has-at-least-one-main', function () {
assert.isTrue(mainIsFound);
assert.equal(checkContext._data, mainIsFound);
});

(shadowSupported ? it : xit)
('should return true if main is inside of shadow dom', function() {
var params = shadowCheckSetup('<div id="target"></div>', '<main>main landmark</main>');
var mainIsFound = checks['has-at-least-one-main'].evaluate.apply(checkContext, params);
assert.isTrue(mainIsFound);
assert.equal(checkContext._data, mainIsFound);
Expand Down
10 changes: 10 additions & 0 deletions test/checks/navigation/header-present.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ describe('header-present', function () {

var fixture = document.getElementById('fixture');
var checkSetup = axe.testUtils.checkSetup;
var shadowSupported = axe.testUtils.shadowSupport.v1;
var checkContext = axe.testUtils.MockCheckContext();
var shadowCheckSetup = axe.testUtils.shadowCheckSetup;

afterEach(function () {
fixture.innerHTML = '';
Expand Down Expand Up @@ -41,4 +43,12 @@ describe('header-present', function () {
assert.isFalse(checks['header-present'].evaluate.apply(checkContext, params));
});

(shadowSupported ? it : xit)
('should return true if heading is in shadow dom', function () {
var params = shadowCheckSetup(
'<div id="target"><div>',
'<h1></h1>'
);
assert.isTrue(checks['header-present'].evaluate.apply(checkContext, params));
});
});
10 changes: 10 additions & 0 deletions test/checks/navigation/internal-link-present.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ describe('internal-link-present', function () {
'use strict';

var fixture = document.getElementById('fixture');
var shadowSupported = axe.testUtils.shadowSupport.v1;
var checkContext = axe.testUtils.MockCheckContext();
var checkSetup = axe.testUtils.checkSetup;
var shadowCheckSetup = axe.testUtils.shadowCheckSetup;

afterEach(function () {
fixture.innerHTML = '';
Expand All @@ -21,4 +23,12 @@ describe('internal-link-present', function () {
assert.isFalse(checks['internal-link-present'].evaluate.apply(checkContext, params));
});

(shadowSupported ? it : xit)
('should return true when internal link is found in shadow dom', function () {
var params = shadowCheckSetup(
'<div id="target"></div>',
'<a href="#haha">hi</a>'
);
assert.isTrue(checks['internal-link-present'].evaluate.apply(checkContext, params));
});
});
16 changes: 16 additions & 0 deletions test/checks/navigation/p-as-heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,20 @@ describe('p-as-heading', function () {
assert.isTrue(checks['p-as-heading'].evaluate.apply(checkContext, params));
});
});

(shadowSupported ? it : xit)
('returns undefined instead of false if the element is inside a blockquote in light dom', function () {
var params = shadowCheckSetup('<blockquote></blockquote>',
'<p style="font-weight:bold" id="target">elm 1</p> <p>elm 2</p>',
testOptions);
assert.isUndefined(checks['p-as-heading'].evaluate.apply(checkContext, params));
});

(shadowSupported ? it : xit)
('returns true over undefined from within a blockquote in light dom', function () {
var params = shadowCheckSetup('<blockquote></blockquote>',
'<p id="target">elm 1</p> <p>elm 2</p>',
testOptions);
assert.isTrue(checks['p-as-heading'].evaluate.apply(checkContext, params));
});
});
19 changes: 13 additions & 6 deletions test/testutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,32 @@ testUtils.checkSetup = function (content, options, target) {
*
* @param Node|String Stuff to go into the fixture (html string or DOM Node)
* @param Node|String Stuff to go into the shadow boundary (html or node)
* @param String Target selector for the check, can be inside or outside of Shadow DOM (default: '#target')
* @param Object Options argument for the check (optional, default: {})
* @param String Target selector for the check, can be inside or outside of Shadow DOM (optional, default: '#target')
* @return Array
*/
testUtils.shadowCheckSetup = function (content, shadowContent, targetSelector, options) {
testUtils.shadowCheckSetup = function (content, shadowContent, options, targetSelector) {
'use strict';

// Normalize the params
// Normalize target, allow it to be the provided string or use '#target' to query composed tree
if (typeof targetSelector !== 'string') {
targetSelector = '#target';
}

// Normalize the object params
if (typeof options !== 'object') {
options = {};
}
// Normalize target, allow it to be the provided string or use '#target' to query composed tree
targetSelector = targetSelector || '#target';

var fixture = testUtils.fixtureSetup(content);
var targetCandidate = fixture.querySelector(targetSelector);
var container = targetCandidate;
if (!targetCandidate) {
container = fixture.firstChild;
// check if content specifies a shadow container
container = fixture.querySelector('#shadow');
if (!container) {
container = fixture.firstChild;
}
}
// attach a shadowRoot with the content provided
var shadowRoot = container.attachShadow({ mode: 'open' });
Expand Down

0 comments on commit 0e48413

Please sign in to comment.