Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #47 from dequelabs/axe-run-migration
Browse files Browse the repository at this point in the history
Migrate to axe.run for 2x
  • Loading branch information
WilcoFiers authored Dec 20, 2017
2 parents d6e6518 + f500305 commit a9bc505
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
32 changes: 20 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AxeBuilder.prototype.exclude = function(selector) {
};

/**
* Options to directly pass to `axe.a11yCheck`. See API documentation for axe-core for use. Will override any other configured options, including calls to `withRules` and `withTags`.
* Options to directly pass to `axe.run`. See API documentation for axe-core for use. Will override any other configured options, including calls to `withRules` and `withTags`.
* @param {Object} options Options object
* @return {AxeBuilder}
*/
Expand Down Expand Up @@ -114,7 +114,7 @@ AxeBuilder.prototype.configure = function(config) {

/**
* Perform analysis and retrieve results. *Does not chain.*
* @param {Function} callback Function to execute when analysis completes; recieves one argument, the results object of analysis
* @param {Function} callback Function to execute when analysis completes; receives one argument, the results object of analysis
*/
AxeBuilder.prototype.analyze = function(callback) {
var context = normalizeContext(this._includes, this._excludes),
Expand All @@ -123,16 +123,24 @@ AxeBuilder.prototype.analyze = function(callback) {
config = this._config,
source = this._source;

inject(driver, source, config, function() {
driver
.executeAsyncScript(function(context, options, config) {
/*global document, axe */
if (config !== null) {
window.axe.configure(config);
}
window.axe.a11yCheck(context || document, options, arguments[arguments.length - 1]);
}, context, options, config)
.then(callback);
return new Promise(function(resolve, reject) {
inject(driver, source, config, function() {
driver
.executeAsyncScript(function(context, options, config) {
/*global document, axe */
if (config !== null) {
window.axe.configure(config);
}
window.axe.run(context || document, options || {})
.then(arguments[arguments.length - 1]);
}, context, options, config)
.then(function(results) {
if (callback) {
callback(results);
}
resolve(results);
});
});
});
};

Expand Down
55 changes: 54 additions & 1 deletion test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('Builder', function () {

});

it('should call axe.a11yCheck with given parameters', function (done) {
it('should call axe.run with given parameters', function (done) {
var config = {};
var Builder = proxyquire('../../lib/index', {
'./inject': function (driver, source, config, cb) {
Expand Down Expand Up @@ -255,5 +255,58 @@ describe('Builder', function () {
done();
});
});

it('should pass results to .then() instead of a callback', function (done) {
var config = {};
var Builder = proxyquire('../../lib/index', {
'./inject': function (driver, source, config, cb) {
cb(null, 'source-code');
}
});

new Builder({
executeAsyncScript: function (callback, context, options) {
return {
then: function (cb) {
cb('results');
}
};
}
})
.analyze()
.then(function (results) {
assert.equal(results, 'results');
done();
});
});

it('should execute callback before .then()', function (done) {
var config = {};
var Builder = proxyquire('../../lib/index', {
'./inject': function (driver, source, config, cb) {
cb(null, 'source-code');
}
});
var called = false;

new Builder({
executeAsyncScript: function (callback, context, options) {
return {
then: function (cb) {
cb('results');
}
};
}
})
.analyze(function (results) {
assert.equal(results, 'results');
assert.equal(called, false);
called = true;
})
.then(function (results) {
assert.equal(called, true);
done();
});
});
});
});

0 comments on commit a9bc505

Please sign in to comment.