Skip to content

Commit

Permalink
feat(util): add object property assert with throw
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyke authored and Peter Marton committed Jul 27, 2015
1 parent 5e47ec5 commit 2cfb447
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/util/index.js
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;
45 changes: 45 additions & 0 deletions src/util/index.spec.js
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');

});

});

});

0 comments on commit 2cfb447

Please sign in to comment.