Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): add support for runOnly. #101

Merged
merged 7 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react/cypress/integration/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('React-axe', function () {
cy.spy(win.console, 'groupCollapsed');
cy.spy(win.console, 'groupEnd');

axe(React, ReactDOM, 0).then(function () {
axe(React, ReactDOM, 0, {}).then(function () {
expect(win.console.group).to.be.calledWith(
'%cNew axe issues',
'color:#d93251;font-weight:normal;'
Expand All @@ -52,7 +52,7 @@ describe('React-axe', function () {
serviceChooser = node[0];
});

axe(React, ReactDOM, 0).then(function () {
axe(React, ReactDOM, 0, {}).then(function () {
expect(filterLogs(groupCollapsed.args, colorMessage)).to.equal(
colorMessage
);
Expand Down
25 changes: 20 additions & 5 deletions packages/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const cache: { [key: string]: string } = {};
// @see https://davidwalsh.name/javascript-debounce-function
function debounce(func: Function, wait: number, immediate?: boolean): Function {
let _timeout;
return function(...args): void {
return function (...args): void {
const later = (): void => {
_timeout = null;
if (!immediate) func.apply(this, args);
Expand Down Expand Up @@ -203,7 +203,7 @@ function checkAndReport(node: Node, timeout: number): Promise<void> {
n = document;
}
}
axeCore.run(n, { reporter: 'v2' }, function(
axeCore.run(n, { reporter: 'v2' }, function (
error: Error,
results: axeCore.AxeResults
) {
Expand Down Expand Up @@ -328,34 +328,49 @@ function addComponent(component: any): void {
}
}

/**
* To support paramater of type runOnly
*/
interface ReactSpec extends axeCore.Spec {
runOnly?: string[];
}

/**
* Run axe against all changes made in a React app.
* @parma {React} _React React instance
* @param {ReactDOM} _ReactDOM ReactDOM instance
* @param {Number} _timeout debounce timeout in milliseconds
* @parma {Spec} conf axe.configure Spec object
* @parma {Spec} conf React axe.configure Spec object
* @param {ElementContext} _context axe ElementContent object
*/
function reactAxe(
_React: typeof React,
_ReactDOM: typeof ReactDOM,
_timeout: number,
conf?: axeCore.Spec,
conf?: ReactSpec,
_context?: axeCore.ElementContext
): Promise<void> {
React = _React;
ReactDOM = _ReactDOM;
timeout = _timeout;
context = _context;

const runOnly = conf['runOnly'];
if (runOnly) {
conf['rules'] = axeCore
.getRules(runOnly)
.map(rule => ({ ...rule, id: rule.ruleId, enabled: true }));
conf['disableOtherRules'] = true;
}

if (conf) {
axeCore.configure(conf);
}

if (!_createElement) {
_createElement = React.createElement;

React.createElement = function(...args): React.Component {
React.createElement = function (...args): React.Component {
const reactEl = _createElement.apply(this, args);

if (reactEl._owner && reactEl._owner._instance) {
Expand Down
5 changes: 5 additions & 0 deletions packages/react/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ reactAxe(React, ReactDOM, 1000, {

const context = document.createElement('div');

// readOnly feature
reactAxe(React, ReactDOM, 1000, {
runOnly: ['wcag2aa', 'wcag2a']
});

// axe-core context: Node
reactAxe(React, ReactDOM, 1000, {}, context);

Expand Down