diff --git a/lib/core/base/audit.js b/lib/core/base/audit.js index 4f56c27f52..81796f9207 100644 --- a/lib/core/base/audit.js +++ b/lib/core/base/audit.js @@ -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; @@ -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 @@ -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(); + }; \ No newline at end of file diff --git a/lib/core/public/reset.js b/lib/core/public/reset.js new file mode 100644 index 0000000000..0cb75f0c30 --- /dev/null +++ b/lib/core/public/reset.js @@ -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(); +}; diff --git a/lib/core/public/run-rules.js b/lib/core/public/run-rules.js index 1624dce857..5f0c35eaaf 100644 --- a/lib/core/public/run-rules.js +++ b/lib/core/public/run-rules.js @@ -64,3 +64,4 @@ axe.a11yCheck = function (context, options, callback) { reporter(results, callback); }, function () {}); }; + diff --git a/test/core/base/audit.js b/test/core/base/audit.js index 3ced07e410..5cc3984a15 100644 --- a/test/core/base/audit.js +++ b/test/core/base/audit.js @@ -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(); diff --git a/test/core/public/reset.js b/test/core/public/reset.js new file mode 100644 index 0000000000..f473d0fb09 --- /dev/null +++ b/test/core/public/reset.js @@ -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'); + }); +});