Skip to content

Commit

Permalink
add new axe.reset function
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanb committed Mar 17, 2016
1 parent 833360b commit ff893ed
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ function Audit(audit) {
// defaults
this.brand = 'axe';
this.application = 'axeAPI';
this.defaultConfig = audit;
this._init();
}

audit = setDefaultConfiguration(audit);
/**
* Initializes the rules and checks
*/
Audit.prototype._init = function () {
'use strict';
var audit = setDefaultConfiguration(this.defaultConfig);

axe.commons = commons = audit.commons;

Expand All @@ -49,8 +57,8 @@ function Audit(audit) {
this.data.rules = (audit.data && audit.data.rules) || {};
this.data.failureSummaries = (audit.data && audit.data.failureSummaries) || {};
this._constructHelpUrls(); // create default helpUrls
}

};

/**
* Adds a new command to the audit
Expand Down Expand Up @@ -186,3 +194,13 @@ Audit.prototype._constructHelpUrls = function () {
'application=' + that.application;
});
};


/**
* Reset the default rules, checkas and meta data
*/

Audit.prototype.resetRulesAndChecks = function () {
'use strict';
this._init();
};
10 changes: 10 additions & 0 deletions lib/core/public/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*global axe */

axe.reset = function () {
'use strict';
var audit = axe._audit;
if (!audit) {
throw new Error('No audit configured');
}
audit.resetRulesAndChecks();
};
1 change: 1 addition & 0 deletions lib/core/public/run-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ axe.a11yCheck = function (context, options, callback) {
reporter(results, callback);
}, function () {});
};

16 changes: 16 additions & 0 deletions test/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ describe('Audit', function () {

});

describe('Audit#resetRulesAndChecks', function () {
it('should override newly created check', function () {
var audit = new Audit();
assert.equal(audit.checks.target, undefined);
audit.addCheck({
id: 'target',
selector: 'bob',
options: 'jane'
});
assert.ok(audit.checks.target);
assert.equal(audit.checks.target.selector, 'bob');
audit.resetRulesAndChecks();
assert.equal(audit.checks.target, undefined);
});
});

describe('Audit#addCheck', function () {
it('should create a new check', function () {
var audit = new Audit();
Expand Down
45 changes: 45 additions & 0 deletions test/core/public/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*global Rule*/
describe('axe.reset', function () {
'use strict';

it('should throw if no audit is configured', function () {
axe._audit = null;

assert.throws(function () {
axe.reset();
}, Error, /^No audit configured/);
});

it('should restore the default configuration', function () {
axe._load({
data: {
rules: [{
bob: 'not-joe'
}]
},
rules: [{
id: 'bob',
selector: 'fail'
}]
});
assert.lengthOf(axe._audit.rules, 1);
assert.instanceOf(axe._audit.rules[0], Rule);
assert.equal(axe._audit.rules[0].id, 'bob');
assert.equal(axe._audit.rules[0].selector, 'fail');
axe.configure({
rules: [{
id: 'bob',
selector: 'pass',
}]
});
assert.lengthOf(axe._audit.rules, 1);
assert.instanceOf(axe._audit.rules[0], Rule);
assert.equal(axe._audit.rules[0].id, 'bob');
assert.equal(axe._audit.rules[0].selector, 'pass');
axe.reset();
assert.lengthOf(axe._audit.rules, 1);
assert.instanceOf(axe._audit.rules[0], Rule);
assert.equal(axe._audit.rules[0].id, 'bob');
assert.equal(axe._audit.rules[0].selector, 'fail');
});
});

0 comments on commit ff893ed

Please sign in to comment.