From 9db63dc3314c332460feb82e1ea4d4b15060d5c0 Mon Sep 17 00:00:00 2001 From: Marcy Sutton Date: Thu, 25 May 2017 11:34:16 -0700 Subject: [PATCH] feat: add fallback fallback message --- lib/core/utils/publish-metadata.js | 30 +++++--- test/core/utils/publish-metadata.js | 113 ++++++++++++++++++++++++++-- 2 files changed, 127 insertions(+), 16 deletions(-) diff --git a/lib/core/utils/publish-metadata.js b/lib/core/utils/publish-metadata.js index 250c944300..6bd47a7c76 100644 --- a/lib/core/utils/publish-metadata.js +++ b/lib/core/utils/publish-metadata.js @@ -7,20 +7,32 @@ * @private */ function getIncompleteReason(checkData, messages) { + function getDefaultMsg(messages) { + if (messages.incomplete && messages.incomplete.default) { + // fall back to the default message if no reason specified + return messages.incomplete.default; + } else { + // TODO: localize it + return 'aXe couldn\'t tell the reason. Time to break out the element inspector!'; + } + } if (checkData && checkData.missingData) { - var missingReason; try { - missingReason = checkData.missingData[0].reason; - } finally { + var msg = messages.incomplete[checkData.missingData[0].reason]; + if (!msg) { + throw new Error(); + } + return msg; + } catch (e) { if (typeof checkData.missingData === 'string') { - missingReason = checkData.missingData; + // return a string with the appropriate reason + return messages.incomplete[checkData.missingData]; + } else { + return getDefaultMsg(messages); } } - // return a function with the appropriate message - return messages.incomplete[missingReason]; } else { - // fall back to the default message if no reason specified - return messages.incomplete.default; + return getDefaultMsg(messages); } } /** @@ -41,7 +53,7 @@ function extender(checksData, shouldBeTrue) { if (typeof messages.incomplete === 'object') { data.message = function() { return getIncompleteReason(check.data, messages); }; } else { - // fall back to string message + // fall back to string function data.message = messages.incomplete; } } else { diff --git a/test/core/utils/publish-metadata.js b/test/core/utils/publish-metadata.js index 36e54ff37c..fa311a89de 100644 --- a/test/core/utils/publish-metadata.js +++ b/test/core/utils/publish-metadata.js @@ -184,7 +184,7 @@ describe('axe.utils.publishMetaData', function () { }); - it('should handle incomplete objects with no reason specified by the check', function () { + it('should return default incomplete message with no reason specified by the check', function () { axe._load({ rules: [], @@ -295,6 +295,105 @@ describe('axe.utils.publishMetaData', function () { }); + it('should fall back to a generic message if incomplete object fails', function () { + + axe._load({ + rules: [], + data: { + rules: { + cats: { + help: function () { + return 'cats-rule'; + } + } + }, + checks: { + 'cats-NONE': { + messages: { + fail: function () { + return 'fail-NONE'; + }, + pass: function () { + return 'pass-NONE'; + }, + incomplete: {} + } + }, + 'cats-ANY': { + messages: { + fail: function () { + return 'fail-ANY'; + }, + pass: function () { + return 'pass-ANY'; + }, + incomplete: {} + } + }, + 'cats-ALL': { + messages: { + fail: function () { + return 'fail-ALL'; + }, + pass: function () { + return 'pass-ALL'; + }, + incomplete: {} + } + } + } + } + }); + + var result = { + id: 'cats', + nodes: [{ + any: [{ + result: undefined, + id: 'cats-ANY', + data: {} + }], + none: [{ + result: undefined, + id: 'cats-NONE', + data: {} + }], + all: [{ + result: undefined, + id: 'cats-ALL', + data: {} + }] + }] + }; + axe.utils.publishMetaData(result); + assert.deepEqual(result, { + id: 'cats', + help: 'cats-rule', + tags: [], + nodes: [{ + any: [{ + result: undefined, + id: 'cats-ANY', + message: 'aXe couldn\'t tell the reason. Time to break out the element inspector!', + data: {} + }], + none: [{ + result: undefined, + id: 'cats-NONE', + message: 'aXe couldn\'t tell the reason. Time to break out the element inspector!', + data: {} + }], + all: [{ + result: undefined, + id: 'cats-ALL', + message: 'aXe couldn\'t tell the reason. Time to break out the element inspector!', + data: {} + }] + }] + }); + + }); + it('should handle incomplete reasons', function () { axe._load({ @@ -440,7 +539,7 @@ describe('axe.utils.publishMetaData', function () { return 'pass-NONE'; }, incomplete: { - 'incomplete-NONE-reason1': 'We couldn\'t tell because of some reason', + 'incomplete-NONE-reason1': 'We couldn\'t tell because of reason #1', 'incomplete-NONE-reason2': 'Some other reason', 'default': 'Fallback message for no reason' } @@ -455,7 +554,7 @@ describe('axe.utils.publishMetaData', function () { return 'pass-ANY'; }, incomplete: { - 'incomplete-ANY-reason1': 'We couldn\'t tell because of some reason', + 'incomplete-ANY-reason1': 'We couldn\'t tell because of reason #1', 'incomplete-ANY-reason2': 'Some other reason', 'default': 'Fallback message for no reason' } @@ -470,7 +569,7 @@ describe('axe.utils.publishMetaData', function () { return 'pass-ALL'; }, incomplete: { - 'incomplete-ALL-reason1': 'We couldn\'t tell because of some reason', + 'incomplete-ALL-reason1': 'We couldn\'t tell because of reason #1', 'incomplete-ALL-reason2': 'Some other reason', 'default': 'Fallback message for no reason' } @@ -521,7 +620,7 @@ describe('axe.utils.publishMetaData', function () { any: [{ result: undefined, id: 'cats-ANY', - message: 'We couldn\'t tell because of some reason', + message: 'We couldn\'t tell because of reason #1', data: { missingData: [{ reason: 'incomplete-ANY-reason1' @@ -531,7 +630,7 @@ describe('axe.utils.publishMetaData', function () { none: [{ result: undefined, id: 'cats-NONE', - message: 'We couldn\'t tell because of some reason', + message: 'We couldn\'t tell because of reason #1', data: { missingData: [{ reason: 'incomplete-NONE-reason1' @@ -541,7 +640,7 @@ describe('axe.utils.publishMetaData', function () { all: [{ result: undefined, id: 'cats-ALL', - message: 'We couldn\'t tell because of some reason', + message: 'We couldn\'t tell because of reason #1', data: { missingData: [{ reason: 'incomplete-ALL-reason1'