Skip to content

Commit

Permalink
feat(audit): add lang query paramenter to help urls (#1794)
Browse files Browse the repository at this point in the history
* feat(audit): add lang query paramenter to help urls

* fix test

* encode uri
  • Loading branch information
straker authored Sep 5, 2019
1 parent 0bdaa2b commit 2d1c8a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Audit(audit) {
this.brand = 'axe';
this.application = 'axeAPI';
this.tagExclude = ['experimental'];
this.lang = 'en';

this.defaultConfig = audit;
this._init();
Expand All @@ -60,7 +61,8 @@ Audit.prototype._setDefaultLocale = function() {

const locale = {
checks: {},
rules: {}
rules: {},
lang: this.lang
};

// XXX: unable to use `for-of` here, as doing so would
Expand Down Expand Up @@ -223,6 +225,10 @@ Audit.prototype.applyLocale = function(locale) {
if (locale.rules) {
this._applyRuleLocale(locale.rules);
}

if (locale.lang) {
this.lang = locale.lang;
}
};

/**
Expand Down Expand Up @@ -642,7 +648,7 @@ Audit.prototype.setBranding = function(branding) {
/**
* For all the rules, create the helpUrl and add it to the data for that rule
*/
function getHelpUrl({ brand, application }, ruleId, version) {
function getHelpUrl({ brand, application, lang }, ruleId, version) {
return (
axe.constants.helpUrlBase +
brand +
Expand All @@ -651,7 +657,8 @@ function getHelpUrl({ brand, application }, ruleId, version) {
'/' +
ruleId +
'?application=' +
application
encodeURIComponent(application) +
(lang && lang !== 'en' ? '&lang=' + encodeURIComponent(lang) : '')
);
}

Expand Down
28 changes: 28 additions & 0 deletions test/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ describe('Audit', function() {
'https://dequeuniversity.com/rules/axe/1.0/target?application=axeAPI'
);
});
it('sets the lang query if locale has been set', function() {
var audit = new Audit();
audit.addRule({
id: 'target',
matches: 'function () {return "hello";}',
selector: 'bob'
});
audit.applyLocale({
lang: 'de'
});
assert.lengthOf(audit.rules, 1);
assert.equal(audit.data.rules.target, undefined);
audit._constructHelpUrls();
assert.deepEqual(audit.data.rules.target, {
helpUrl:
'https://dequeuniversity.com/rules/axe/x.y/target?application=axeAPI&lang=de'
});
});
});

describe('Audit#setBranding', function() {
Expand Down Expand Up @@ -368,6 +386,16 @@ describe('Audit', function() {
audit.resetRulesAndChecks();
assert.equal(audit.checks.target, undefined);
});
it('should reset locale', function() {
var audit = new Audit();
assert.equal(audit.lang, 'en');
audit.applyLocale({
lang: 'de'
});
assert.equal(audit.lang, 'de');
audit.resetRulesAndChecks();
assert.equal(audit.lang, 'en');
});
});

describe('Audit#addCheck', function() {
Expand Down
16 changes: 16 additions & 0 deletions test/core/public/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ describe('axe.configure', function() {
});
});

it('sets the lang property', function() {
axe.configure({
locale: {
lang: 'lol',
rules: { greeting: { description: 'hello' } },
checks: {
banana: {
fail: 'icecream'
}
}
}
});

assert.equal(axe._audit.lang, 'lol');
});

describe('only given checks', function() {
it('should not error', function() {
assert.doesNotThrow(function() {
Expand Down

0 comments on commit 2d1c8a6

Please sign in to comment.