Skip to content

Commit

Permalink
chore: improve test coverage re: Automattic/mongoose#3232
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 18, 2017
1 parent 16b44b5 commit 7b45cf0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
46 changes: 21 additions & 25 deletions index.js
Expand Up @@ -10,7 +10,7 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
callback = args;
args = [];
}
var pres = this._pres[name] || [];
var pres = get(this._pres, name, []);
var numPres = pres.length;
var numAsyncPres = pres.numAsync || 0;
var currentPre = 0;
Expand Down Expand Up @@ -106,7 +106,7 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
};

Kareem.prototype.execPreSync = function(name, context, args) {
var pres = this._pres[name] || [];
var pres = get(this._pres, name, []);
var numPres = pres.length;

for (var i = 0; i < numPres; ++i) {
Expand All @@ -119,7 +119,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
callback = options;
options = null;
}
var posts = this._posts[name] || [];
var posts = get(this._posts, name, []);
var numPosts = posts.length;
var currentPost = 0;

Expand Down Expand Up @@ -200,7 +200,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
};

Kareem.prototype.execPostSync = function(name, context, args) {
var posts = this._posts[name] || [];
var posts = get(this._posts, name, []);
var numPosts = posts.length;

for (var i = 0; i < numPosts; ++i) {
Expand Down Expand Up @@ -298,7 +298,7 @@ Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) {
isAsync = false;
}

this._pres[name] = this._pres[name] || [];
this._pres[name] = get(this._pres, name, []);
var pres = this._pres[name];

if (isAsync) {
Expand All @@ -316,7 +316,7 @@ Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) {
};

Kareem.prototype.post = function(name, fn, unshift) {
this._posts[name] = this._posts[name] || [];
this._posts[name] = get(this._posts, name, []);

if (unshift) {
this._posts[name].unshift(fn);
Expand All @@ -327,18 +327,13 @@ Kareem.prototype.post = function(name, fn, unshift) {
};

Kareem.prototype.clone = function() {
var n = new Kareem();
for (var key in this._pres) {
if (!this._pres.hasOwnProperty(key)) {
continue;
}
const n = new Kareem();

for (let key of Object.keys(this._pres)) {
n._pres[key] = this._pres[key].slice();
n._pres[key].numAsync = this._pres[key].numAsync;
}
for (var key in this._posts) {
if (!this._posts.hasOwnProperty(key)) {
continue;
}
for (let key of Object.keys(this._posts)) {
n._posts[key] = this._posts[key].slice();
}

Expand All @@ -347,21 +342,22 @@ Kareem.prototype.clone = function() {

Kareem.prototype.merge = function(other) {
var ret = this.clone();
for (var key in other._pres) {
if (!other._pres.hasOwnProperty(key)) {
continue;
}
ret._pres[key] = (ret._pres[key] || []).concat(other._pres[key].slice());
for (let key of Object.keys(other._pres)) {
ret._pres[key] = get(ret._pres, key, []).concat(other._pres[key].slice());
ret._pres[key].numAsync += other._pres[key].numAsync;
}
for (var key in other._posts) {
if (!other._posts.hasOwnProperty(key)) {
continue;
}
ret._posts[key] = (ret._posts[key] || []).concat(other._posts[key].slice());
for (let key of Object.keys(other._posts)) {
ret._posts[key] = get(ret._posts, key, []).concat(other._posts[key].slice());
}

return ret;
};

function get(obj, key, def) {
if (obj[key] != null) {
return obj[key];
}
return def;
}

module.exports = Kareem;
9 changes: 9 additions & 0 deletions test/post.test.js
Expand Up @@ -20,6 +20,15 @@ describe('execPost', function() {
});
});

it('unshift', function() {
var f1 = function() {};
var f2 = function() {};
hooks.post('cook', f1);
hooks.post('cook', f2, true);
assert.strictEqual(hooks._posts['cook'][0], f2);
assert.strictEqual(hooks._posts['cook'][1], f1);
});

it('multiple posts', function(done) {
hooks.post('cook', function(eggs, callback) {
setTimeout(
Expand Down
9 changes: 9 additions & 0 deletions test/pre.test.js
Expand Up @@ -35,6 +35,15 @@ describe('execPre', function() {
});
});

it('unshift', function() {
var f1 = function() {};
var f2 = function() {};
hooks.pre('cook', false, f1);
hooks.pre('cook', false, f2, null, true);
assert.strictEqual(hooks._pres['cook'][0].fn, f2);
assert.strictEqual(hooks._pres['cook'][1].fn, f1);
});

it('handles async errors', function(done) {
var execed = {};

Expand Down
21 changes: 21 additions & 0 deletions test/wrap.test.js
Expand Up @@ -318,4 +318,25 @@ describe('wrap()', function() {
},
25);
});

it('sync wrappers', function() {
var calledPre = 0;
var calledFn = 0;
var calledPost = 0;
hooks.pre('cook', function() {
++calledPre;
});

hooks.post('cook', function() {
++calledPost;
});

var wrapper = hooks.createWrapperSync('cook', function() { ++calledFn; });

wrapper();

assert.equal(calledPre, 1);
assert.equal(calledFn, 1);
assert.equal(calledPost, 1);
});
});

0 comments on commit 7b45cf0

Please sign in to comment.