Skip to content

Commit

Permalink
fix(getCheckMessage): add API to return check message (#2066)
Browse files Browse the repository at this point in the history
* feat(getCheckMessage): add API to return check message

* throw when invalid
  • Loading branch information
straker committed Mar 6, 2020
1 parent 635445b commit e216322
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/core/utils/get-check-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* global axe*/

/**
* Get the pass, fail, or incomplete message for a check.
* @param {String} checkId The check id
* @param {String} type The message type ('pass', 'fail', or 'incomplete')
* @param {Object} [data] The check data
* @return {String}
*/
axe.utils.getCheckMessage = function getCheckMessage(checkId, type, data) {
const check = axe._audit.data.checks[checkId];

if (!check) {
throw new Error(`Cannot get message for unknown check: ${checkId}.`);
}
if (!check.messages[type]) {
throw new Error(`Check "${checkId}"" does not have a "${type}" message.`);
}

return axe.utils.processMessage(check.messages[type], data);
};
59 changes: 59 additions & 0 deletions test/core/utils/get-check-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
describe('axe.utils.getCheckMessage', function() {
var getCheckMessage = axe.utils.getCheckMessage;

beforeEach(function() {
axe._audit = {
data: {
checks: {
'my-check': {
messages: {
pass: 'Pass message',
fail: 'Fail message',
incomplete: 'Incomplete message'
}
}
}
}
};
});

afterEach(function() {
axe._audit = undefined;
});

it('should return the pass message', function() {
assert.equal(getCheckMessage('my-check', 'pass'), 'Pass message');
});

it('should return the fail message', function() {
assert.equal(getCheckMessage('my-check', 'fail'), 'Fail message');
});

it('should return the incomplete message', function() {
assert.equal(
getCheckMessage('my-check', 'incomplete'),
'Incomplete message'
);
});

it('should handle data', function() {
axe._audit.data.checks['my-check'].messages.pass =
'Pass message with ${data.message}';
assert.equal(
getCheckMessage('my-check', 'pass', { message: 'hello world!' }),
'Pass message with hello world!'
);
});

it('should error when check does not exist', function() {
assert.throws(function() {
getCheckMessage('invalid-check', 'pass');
});
});

it('should error when check message does not exist', function() {
assert.throws(function() {
getCheckMessage('invalid-check', 'invalid');
});
});
});

0 comments on commit e216322

Please sign in to comment.