Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Add proposed update for choosing between before and beforeEach #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
var helpers = require('./lib/helpers');
exports.describe = helpers.describe;
exports.it = helpers.it;
exports.call = helpers.call;
exports.before = helpers.before;
exports.beforeEach = helpers.beforeEach;
exports.after = helpers.after;
exports.afterEach = helpers.afterEach;
exports.TestDataBuilder = require('./lib/test-data-builder');
177 changes: 128 additions & 49 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
var _describe = {};
var _it = {};
var _call = {};
var _before = {};
var _beforeEach = {};
var _after = {};
var _afterEach = {};
var helpers = exports = module.exports = {
describe: _describe,
it: _it,
beforeEach: _beforeEach
call: _call,
before: _before,
beforeEach: _beforeEach,
after: _after,
afterEach: _afterEach
};
var assert = require('assert');
var request = require('supertest');
var expect = require('chai').expect;

_beforeEach.withApp = function(app) {
var _mochaHooks = {
before: before,
beforeEach: beforeEach,
after: after,
afterEach: afterEach
};

var apply = function(hooks) {
return function(name, fn) {
hooks.forEach(function(hook) {
helpers[hook][name] = function() {
return fn.apply({
hook: _mochaHooks[hook],
hookName: hook
}, arguments);
};
});
};
};

apply(['before', 'beforeEach'])
('withApp', function(app) {
if (app.models.User) {
// Speed up the password hashing algorithm
app.models.User.settings.saltWorkFactor = 4;
}

beforeEach(function() {
this.hook(function() {
this.app = app;
var _request = this.request = request(app);
this.post = _request.post;
this.get = _request.get;
this.put = _request.put;
this.del = _request.del;
});
}
});

function mixin(obj, into) {
Object.keys(obj).forEach(function(key) {
Expand Down Expand Up @@ -54,14 +83,14 @@ _describe.instanceMethod = function(methodName, cb) {
});
}

_beforeEach.withArgs = function() {
apply(['before', 'beforeEach'])('withArgs', function() {
var args = Array.prototype.slice.call(arguments, 0);
beforeEach(function() {
this.hook(function() {
this.args = args;
});
}
});

_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
apply(['before', 'beforeEach'])('givenModel', function(modelName, attrs, optionalHandler) {
var modelKey = modelName;

if(typeof attrs === 'function') {
Expand All @@ -75,7 +104,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {

attrs = attrs || {};

beforeEach(function(done) {
this.hook(function(done) {
var test = this;
var app = this.app;
var model = app.models[modelName];
Expand Down Expand Up @@ -108,12 +137,16 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
});
}

_beforeEach.givenUser = function(attrs, optionalHandler) {
);

apply(['before', 'beforeEach'])
('givenUser', function(attrs, optionalHandler) {
_beforeEach.givenModel('user', attrs, optionalHandler);
}
});

_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
_beforeEach.givenUser(attrs, function (done) {
apply(['before', 'beforeEach'])
('givenUserWithRole', function (attrs, role, optionalHandler) {
helpers[this.hookName].givenUser(attrs, function (done) {
var test = this;
test.app.models.Role.create({name: role}, function (err, result) {
if(err) {
Expand Down Expand Up @@ -142,10 +175,11 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
});

if(typeof optionalHandler === 'function') {
beforeEach(optionalHandler);
this.hook(optionalHandler);
}

afterEach(function(done) {
var hook = this.hookName.match(/Each$/) ? afterEach : after;
hook(function(done) {
var test = this;
this.userRole.destroy(function(err) {
if(err) return done(err);
Expand All @@ -158,10 +192,11 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
});
});
});
}
});

_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
_beforeEach.givenUser(credentials, function(done) {
apply(['before', 'beforeEach'])
('givenLoggedInUser', function(credentials, optionalHandler) {
helpers[this.hookName].givenUser(credentials, function(done) {
var test = this;
this.user.constructor.login(credentials, function(err, token) {
if(err) {
Expand All @@ -173,18 +208,21 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
});
});

afterEach(function(done) {
var hook = this.hookName.match(/Each$/) ? afterEach : after;

hook(function(done) {
var test = this;
this.loggedInAccessToken.destroy(function(err) {
if(err) return done(err);
test.loggedInAccessToken = undefined;
done();
});
});
}
});

_beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){
_beforeEach.givenUserWithRole(credentials, role, function(done) {
apply(['before', 'beforeEach'])
('givenLoggedInUserWithRole', function(credentials, role, optionalHandler){
helpers[this.hookName].givenUserWithRole(credentials, role, function(done) {
var test = this;
this.user.constructor.login(credentials, function(err, token) {
if(err) {
Expand All @@ -196,23 +234,43 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
});
});

afterEach(function(done) {
var hook = this.hookName.match(/Each$/) ? afterEach : after;

hook(function(done) {
var test = this;
this.loggedInAccessToken.destroy(function(err) {
if(err) return done(err);
test.loggedInAccessToken = undefined;
done();
});
});
}
});

_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
_beforeEach.givenModel('accessToken', attrs, optionalHandler);
}
apply(['before', 'beforeEach'])
('givenAnUnauthenticatedToken', function(attrs, optionalHandler) {
helpers[this.hookName].givenModel('accessToken', attrs, optionalHandler);
});

_beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
}
apply(['before', 'beforeEach'])
('givenAnAnonymousToken', function(attrs, optionalHandler) {
helpers[this.hookName].givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
});

_call.before = function(fn) {
fn.call({hook: before});
};

_call.beforeEach = function(fn) {
fn.call({hook: beforeEach});
};

_call.after = function(fn) {
fn.call({hook: after});
};

_call.afterEach = function(fn) {
fn.call({hook: afterEach});
};

_describe.whenCalledRemotely = function(verb, url, data, cb) {
if (cb == undefined) {
Expand All @@ -226,7 +284,10 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
}

describe(verb.toUpperCase() + ' ' + urlStr, function() {
beforeEach(function(cb) {

if(!this.hook) this.hook = beforeEach;

this.hook(function(cb) {
if(typeof url === 'function') {
this.url = url.call(this);
}
Expand Down Expand Up @@ -268,43 +329,61 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {

_describe.whenLoggedInAsUser = function(credentials, cb) {
describe('when logged in as user', function () {
_beforeEach.givenLoggedInUser(credentials);

if(!this.hook) this.hook = beforeEach;

this.hook.givenLoggedInUser(credentials);
cb();
});
}

_describe.whenLoggedInAsUserWithRole = function(credentials, role, cb) {
describe('when logged in as user', function () {
_beforeEach.givenLoggedInUser(credentials, role);

if(!this.hook) this.hook = beforeEach;

this.hook.givenLoggedInUser(credentials, role);
cb();
});
}

_describe.whenCalledByUser = function(credentials, verb, url, data, cb) {
describe('when called by logged in user', function () {
_beforeEach.givenLoggedInUser(credentials);
_describe.whenCalledRemotely(verb, url, data, cb);

if(!this.hook) this.hook = beforeEach;

this.hook.givenLoggedInUser(credentials);
_describe.whenCalledRemotely.call(this, verb, url, data, cb);
});
}

_describe.whenCalledByUserWithRole = function (credentials, role, verb, url, data, cb) {
describe('when called by logged in user with role ' + role, function () {
_beforeEach.givenLoggedInUserWithRole(credentials, role);
_describe.whenCalledRemotely(verb, url, data, cb);

if(!this.hook) this.hook = beforeEach;

this.hook.givenLoggedInUserWithRole(credentials, role);
_describe.whenCalledRemotely.call(this, verb, url, data, cb);
});
}

_describe.whenCalledAnonymously = function(verb, url, data, cb) {
describe('when called anonymously', function () {
_beforeEach.givenAnAnonymousToken();
_describe.whenCalledRemotely(verb, url, data, cb);

if(!this.hook) this.hook = beforeEach;

this.hook.givenAnAnonymousToken();
_describe.whenCalledRemotely.call(this, verb, url, data, cb);
});
}

_describe.whenCalledUnauthenticated = function(verb, url, data, cb) {
describe('when called with unauthenticated token', function () {
_beforeEach.givenAnAnonymousToken();
_describe.whenCalledRemotely(verb, url, data, cb);

if(!this.hook) this.hook = beforeEach;

this.hook.givenAnAnonymousToken();
_describe.whenCalledRemotely.call(this, verb, url, data, cb);
});
}

Expand Down Expand Up @@ -336,56 +415,56 @@ _it.shouldNotBeFound = function() {

_it.shouldBeAllowedWhenCalledAnonymously =
function(verb, url, data) {
_describe.whenCalledAnonymously(verb, url, data, function() {
_describe.whenCalledAnonymously.call(this, verb, url, data, function() {
_it.shouldBeAllowed();
});
}

_it.shouldBeDeniedWhenCalledAnonymously =
function(verb, url) {
_describe.whenCalledAnonymously(verb, url, function() {
_describe.whenCalledAnonymously.call(this, verb, url, function() {
_it.shouldBeDenied();
});
}

_it.shouldBeAllowedWhenCalledUnauthenticated =
function(verb, url, data) {
_describe.whenCalledUnauthenticated(verb, url, data, function() {
_describe.whenCalledUnauthenticated.call(this, verb, url, data, function() {
_it.shouldBeAllowed();
});
}

_it.shouldBeDeniedWhenCalledUnauthenticated =
function(verb, url) {
_describe.whenCalledUnauthenticated(verb, url, function() {
_describe.whenCalledUnauthenticated.call(this, verb, url, function() {
_it.shouldBeDenied();
});
}

_it.shouldBeAllowedWhenCalledByUser =
function(credentials, verb, url, data) {
_describe.whenCalledByUser(credentials, verb, url, data, function() {
_describe.whenCalledByUser.call(this, credentials, verb, url, data, function() {
_it.shouldBeAllowed();
});
}

_it.shouldBeDeniedWhenCalledByUser =
function(credentials, verb, url) {
_describe.whenCalledByUser(credentials, verb, url, function() {
_describe.whenCalledByUser.call(this, credentials, verb, url, function() {
_it.shouldBeDenied();
});
}

_it.shouldBeAllowedWhenCalledByUserWithRole =
function(credentials, role, verb, url, data) {
_describe.whenCalledByUserWithRole(credentials, role, verb, url, data, function() {
_describe.whenCalledByUserWithRole.call(this, credentials, role, verb, url, data, function() {
_it.shouldBeAllowed();
});
}

_it.shouldBeDeniedWhenCalledByUserWithRole =
function(credentials, role, verb, url) {
_describe.whenCalledByUserWithRole(credentials, role, verb, url, function() {
_describe.whenCalledByUserWithRole.call(this, credentials, role, verb, url, function() {
_it.shouldBeDenied();
});
}
Loading