Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(getCheckMessage): add API to return check message #2066

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/core/utils/get-check-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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 || !check.messages[type]) {
return '';
straker marked this conversation as resolved.
Show resolved Hide resolved
}

return axe.utils.processMessage(check.messages[type], data);
};
55 changes: 55 additions & 0 deletions test/core/utils/get-check-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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('returns empty string when check does not exist', function() {
assert.equal(getCheckMessage('invalid-check', 'pass'), '');
});

it('returns empty string when check message does not exist', function() {
assert.equal(getCheckMessage('invalid-check', 'invalid'), '');
});
});