Skip to content

Commit

Permalink
Merge pull request #138 from foysalit/master
Browse files Browse the repository at this point in the history
Checks 2nd parameter of the notify() method to be a function
  • Loading branch information
mikaelbr authored Sep 8, 2016
2 parents 6a4011c + 25e3bdf commit e84e351
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ module.exports.mapToMac = function (options) {
module.exports.actionJackerDecorator = function (emitter, options, fn, mapper) {
options = clone(options);
fn = fn || function (err, data) {};

if (typeof fn !== "function")
throw new TypeError('The second argument must be a function callback. You have passed '+ typeof fn);

return function (err, data) {

var resultantData = data;
Expand Down
3 changes: 3 additions & 0 deletions notifiers/notifysend.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ NotifySend.prototype.notify = function (options, callback) {
options = cloneDeep(options || {});
callback = callback || function () {};

if (typeof callback !== "function")
throw new TypeError('The second argument must be a function callback. You have passed '+ typeof callback);

if (typeof options === 'string') options = {
title: 'node-notifier',
message: options
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ describe('constructors', function(){
should(notifier.notify).be.ok;
});

it('should expect only a function callback as second parameter', function () {
var cb = function (err, data) {};
var data = { 'title': 'My notification' };

should(notifier.notify(data, cb)).be.ok;
});

it('should throw error when second parameter is not a function', function () {
var wrongParamOne = 200;
var wrongParamTwo = 'meaningless string';
var data = { 'title': 'My notification' };

notifier.notify.bind(notifier, data, wrongParamOne).should.throw(/^The second argument/);
notifier.notify.bind(notifier, data, wrongParamTwo).should.throw(/^The second argument/);
});

it('should expose a default selected constructor function', function () {
should(notifier instanceof notifier.Notification).be.ok;
});
Expand Down

0 comments on commit e84e351

Please sign in to comment.