-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util): add object property assert with throw
- Loading branch information
Showing
2 changed files
with
58 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,13 @@ | ||
function checkDep (obj, propName) { | ||
if (!obj) { | ||
throw new Error('Object cannot be undefined'); | ||
} | ||
|
||
if(!obj[propName]) { | ||
throw new Error(propName + ' cannot be undefined'); | ||
} | ||
|
||
return obj[propName]; | ||
}; | ||
|
||
module.exports.checkDep = checkDep; |
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,45 @@ | ||
var expect = require('chai').expect; | ||
|
||
describe('graffiti util', function() { | ||
|
||
var util = require('./'); | ||
|
||
describe('#checkDep', function() { | ||
|
||
it('throws if first argument is undefined', function() { | ||
|
||
try { | ||
util.checkDep() | ||
} catch (ex) { | ||
expect(ex.message).to.eql('Object cannot be undefined'); | ||
return; | ||
} | ||
|
||
throw new Error('Error should have been thrown'); | ||
}); | ||
|
||
it('throws property does not exists', function() { | ||
|
||
try { | ||
util.checkDep({}, 'colour'); | ||
} catch (ex) { | ||
expect(ex.message).to.eql('colour cannot be undefined'); | ||
return; | ||
} | ||
|
||
throw new Error('Error should have been thrown'); | ||
}); | ||
|
||
it('returns the property', function () { | ||
|
||
var prop = util.checkDep({ | ||
colour: 'red' | ||
}, 'colour'); | ||
|
||
expect(prop).to.eql('red'); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |