Comparing version 0.1.6 to 0.1.7
@@ -39,3 +39,4 @@ // TODO Add in pre and post skipping options | ||
, preArgs; | ||
if (_args.length) hookArgs = _args; | ||
if (_args.length && !(arguments[0] === null && typeof lastArg === 'function')) | ||
hookArgs = _args; | ||
if (++_current < _total) { | ||
@@ -42,0 +43,0 @@ currPre = pres[_current] |
{ | ||
"name": "hooks", | ||
"description": "Adds pre and post hook functionality to your JavaScript methods.", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"keywords": [ "node", "hooks", "middleware", "pre", "post" ], | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/bnoguchi/hooks-js/", |
52
test.js
@@ -194,6 +194,23 @@ var hooks = require('./hooks') | ||
}, | ||
'should default to the hook method as the error handler': function () { | ||
"should fall back first to the hook method's last argument as the error handler if it is a function of arity 1 or 2": function () { | ||
var A = function () {}; | ||
_.extend(A, hooks); | ||
var counter = 0; | ||
A.hook('save', function (callback) { | ||
this.value = 1; | ||
}); | ||
A.pre('save', true, function (next, done) { | ||
next(new Error()); | ||
}); | ||
var a = new A(); | ||
a.save( function (err) { | ||
if (err instanceof Error) counter++; | ||
}); | ||
counter.should.equal(1); | ||
should.deepEqual(undefined, a.value); | ||
}, | ||
'should fall back last to the hook method as the error handler': function () { | ||
var A = function () {}; | ||
_.extend(A, hooks); | ||
var counter = 0; | ||
A.hook('save', function (err) { | ||
@@ -211,2 +228,34 @@ if (err instanceof Error) counter++; | ||
}, | ||
"should proceed without mutating arguments if `next(null)` is called in a serial pre, and the last argument of the target method is a callback with node-like signature function (err, obj) {...} or function (err) {...}": function () { | ||
var A = function () {}; | ||
_.extend(A, hooks); | ||
var counter = 0; | ||
A.prototype.save = function (callback) { | ||
this.value = 1; | ||
callback(); | ||
}; | ||
A.pre('save', function (next) { | ||
next(null); | ||
}); | ||
var a = new A(); | ||
a.save( function (err) { | ||
if (err instanceof Error) counter++; | ||
else counter--; | ||
}); | ||
counter.should.equal(-1); | ||
a.value.should.eql(1); | ||
}, | ||
"should proceed with mutating arguments if `next(null)` is callback in a serial pre, and the last argument of the target method is not a function": function () { | ||
var A = function () {}; | ||
_.extend(A, hooks); | ||
A.prototype.set = function (v) { | ||
this.value = v; | ||
}; | ||
A.pre('set', function (next) { | ||
next(null); | ||
}); | ||
var a = new A(); | ||
a.set(1); | ||
should.strictEqual(null, a.value); | ||
}, | ||
'should not run any posts if a pre fails': function () { | ||
@@ -229,2 +278,3 @@ var A = function () {}; | ||
}, | ||
"can pass the hook's arguments verbatim to pres": function () { | ||
@@ -231,0 +281,0 @@ var A = function () {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40100
882