Skip to content

Commit

Permalink
feat: add fallback fallback message
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcy Sutton committed May 25, 2017
1 parent 04efeca commit 9db63dc
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 16 deletions.
30 changes: 21 additions & 9 deletions lib/core/utils/publish-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
/**
Expand All @@ -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 {
Expand Down
113 changes: 106 additions & 7 deletions test/core/utils/publish-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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'
}
Expand All @@ -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'
}
Expand All @@ -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'
}
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit 9db63dc

Please sign in to comment.