-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(getCheckMessage): add API to return check message (#2066)
* feat(getCheckMessage): add API to return check message * throw when invalid
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |