From e216322183c8b064ebfd748fea785b8c2fe78b16 Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Thu, 27 Feb 2020 10:23:03 -0700 Subject: [PATCH] fix(getCheckMessage): add API to return check message (#2066) * feat(getCheckMessage): add API to return check message * throw when invalid --- lib/core/utils/get-check-message.js | 21 ++++++++++ test/core/utils/get-check-message.js | 59 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 lib/core/utils/get-check-message.js create mode 100644 test/core/utils/get-check-message.js diff --git a/lib/core/utils/get-check-message.js b/lib/core/utils/get-check-message.js new file mode 100644 index 0000000000..461eed6035 --- /dev/null +++ b/lib/core/utils/get-check-message.js @@ -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); +}; diff --git a/test/core/utils/get-check-message.js b/test/core/utils/get-check-message.js new file mode 100644 index 0000000000..545a10da2e --- /dev/null +++ b/test/core/utils/get-check-message.js @@ -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'); + }); + }); +});