Skip to content

Commit

Permalink
Better test coverage for execPost
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 31, 2014
1 parent 825a91c commit d9ad539
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
4 changes: 0 additions & 4 deletions index.js
Expand Up @@ -97,7 +97,6 @@ Kareem.prototype.execPost = function(name, context, args, callback) {
var posts = this._posts[name] || [];
var numPosts = posts.length;
var currentPost = 0;
var done = false;

if (!numPosts) {
return process.nextTick(function() {
Expand All @@ -111,9 +110,6 @@ Kareem.prototype.execPost = function(name, context, args, callback) {
if (post.length > args.length) {
post.apply(context, args.concat(function(error) {
if (error) {
if (done) {
return;
}
return callback(error);
}

Expand Down
27 changes: 27 additions & 0 deletions test/examples.test.js
Expand Up @@ -151,6 +151,33 @@ describe('post hooks', function() {
done();
});
});

it('can use synchronous post hooks', function(done) {
var execed = {};

hooks.post('cook', function(eggs, bacon) {
execed.first = true;
assert.equal(1, eggs);
assert.equal(2, bacon);
});

hooks.post('cook', function(eggs, bacon, callback) {
execed.second = true;
assert.equal(1, eggs);
assert.equal(2, bacon);
callback();
});

hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
assert.ifError(error);
assert.equal(2, Object.keys(execed).length);
assert.ok(execed.first);
assert.ok(execed.second);
assert.equal(1, eggs);
assert.equal(2, bacon);
done();
});
});
});

describe('wrap()', function() {
Expand Down
46 changes: 46 additions & 0 deletions test/post.test.js
@@ -0,0 +1,46 @@
var assert = require('assert');
var Kareem = require('../');

describe('execPost', function() {
var hooks;

beforeEach(function() {
hooks = new Kareem();
});

it('handles errors', function(done) {
hooks.post('cook', function(eggs, callback) {
callback('error!');
});

hooks.execPost('cook', null, [4], function(error, eggs) {
assert.equal('error!', error);
assert.ok(!eggs);
done();
});
});

it('multiple posts', function(done) {
hooks.post('cook', function(eggs, callback) {
setTimeout(
function() {
callback();
},
5);
});

hooks.post('cook', function(eggs, callback) {
setTimeout(
function() {
callback();
},
5);
});

hooks.execPost('cook', null, [4], function(error, eggs) {
assert.ifError(error);
assert.equal(4, eggs);
done();
});
});
});
20 changes: 13 additions & 7 deletions test/pre.test.js
Expand Up @@ -39,23 +39,25 @@ describe('execPre', function() {
var execed = {};

hooks.pre('cook', true, function(next, done) {
next();
execed.first = true;
setTimeout(
function() {
done('error!');
},
5);

next();
});

hooks.pre('cook', true, function(next, done) {
next();
execed.second = true;
setTimeout(
function() {
done('other error!');
},
10);

next();
});

hooks.execPre('cook', null, function(err) {
Expand All @@ -72,12 +74,13 @@ describe('execPre', function() {

hooks.pre('cook', true, function(next, done) {
execed.first = true;
next();
setTimeout(
function() {
done('other error!');
},
15);

next();
});

hooks.pre('cook', true, function(next, done) {
Expand All @@ -104,12 +107,13 @@ describe('execPre', function() {

hooks.pre('cook', true, function(next, done) {
execed.first = true;
next();
setTimeout(
function() {
done('other error!');
},
5);

next();
});

hooks.pre('cook', true, function(next, done) {
Expand All @@ -119,7 +123,7 @@ describe('execPre', function() {
next('error!');
done('another error!');
},
15);
25);
});

hooks.execPre('cook', null, function(err) {
Expand All @@ -136,12 +140,13 @@ describe('execPre', function() {

hooks.pre('cook', true, function(next, done) {
execed.first = true;
next();
setTimeout(
function() {
done('other error!');
},
5);

next();
});

hooks.pre('cook', function(next) {
Expand All @@ -167,12 +172,13 @@ describe('execPre', function() {

hooks.pre('cook', true, function(next, done) {
execed.first = true;
next();
setTimeout(
function() {
done();
},
5);

next();
});

hooks.pre('cook', function() {
Expand Down

0 comments on commit d9ad539

Please sign in to comment.