Skip to content

Commit

Permalink
getConfigBoolean: add test case for when key doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
norrisng-bc committed Jan 31, 2024
1 parent c81652c commit b42f1fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ const utils = {
getConfigBoolean(key) {
try {
const getConfig = config.get(key);

// isTruthy() can't handle undefined / null, so we have to do that here
// @see {@link https://github.com/node-config/node-config/wiki/Common-Usage#using-config-values}
if (getConfig === undefined || getConfig === null) return false;
else return utils.isTruthy(getConfig);
}
Expand Down
13 changes: 7 additions & 6 deletions app/tests/unit/components/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ describe('getConfigBoolean', () => {
[true, true],
[false, false],
[false, null],
[false, undefined]
// [false, new Error('key does not exist!')],
[false, undefined],
[false, 'exception']
])('should return %s when config.get() returns %s', (expected, getConfigOutput) => {

// if (getConfigOutput instanceof Error)
// getConfigBooleanSpy.mockRejectedValue(getConfigOutput);
// else
config.get.mockReturnValueOnce(getConfigOutput);
// config.get() throws exception if the requested key doesn't exist
if (getConfigOutput === 'exception')
config.get.mockImplementation(() => { throw Error('key does not exist!'); });
else
config.get.mockReturnValueOnce(getConfigOutput);
isTruthySpy.mockReturnValueOnce(getConfigOutput);

const output = utils.getConfigBoolean('some.key');
Expand Down

0 comments on commit b42f1fa

Please sign in to comment.