node-switchback
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -6,2 +6,3 @@ /** | ||
var util = require('util'); | ||
var constants = require('./constants'); | ||
@@ -24,3 +25,3 @@ | ||
module.exports = function () { | ||
module.exports = function (callbackContext) { | ||
@@ -38,4 +39,7 @@ var _switch = function( /* err, arg1, arg2, ..., argN */ ) { | ||
// Mark switchback function so it can be identified for tests | ||
_switch[constants.telltale.key] = constants.telltale.value; | ||
return _switch; | ||
}; | ||
@@ -10,5 +10,5 @@ /** | ||
var wildcard = require('./wildcard'); | ||
var constants = require('./constants'); | ||
/** | ||
@@ -30,5 +30,5 @@ * `switchback` | ||
module.exports = function switchback( callback, defaultHandlers, callbackContext ) { | ||
var switchback = function ( callback, defaultHandlers, callbackContext ) { | ||
var Switchback = factory(); | ||
var Switchback = factory(callbackContext); | ||
@@ -104,1 +104,15 @@ // Normalize `callback` to a switchback definition object. | ||
}; | ||
/** | ||
* `isSwitchback` | ||
* | ||
* @param {*} something | ||
* @return {Boolean} [whether `something` is a valid switchback instance] | ||
*/ | ||
switchback.isSwitchback = function (something) { | ||
return _.isObject(something) && something[constants.telltale.key] === constants.telltale.value; | ||
}; | ||
module.exports = switchback; |
@@ -22,3 +22,3 @@ /** | ||
*/ | ||
callback: function _normalizeCallback (callback) { | ||
callback: function _normalizeCallback (callback, callbackContext) { | ||
@@ -25,0 +25,0 @@ if (_.isFunction(callback)) { |
{ | ||
"name": "node-switchback", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Normalize callback fns to switchbacks and vice versa", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
var should = require('should'); | ||
var EventEmitter = require('events').EventEmitter; | ||
describe('switcher(Function)', function (){ | ||
var switchback = require('../lib'); | ||
// Set up event bus to provide access to callback/switchback handler outcomes | ||
var Bus = function () { | ||
this.afterFixture = function (fixtureID, cbOutcome, fnToCall) { | ||
return Bus.once(fixtureID, function (_outcome) { | ||
// cbOutcome should be an empty object at this point (passed by reference) | ||
cbOutcome.args = _outcome.args; | ||
cbOutcome.ctx = _outcome.ctx; | ||
fnToCall(); | ||
}); | ||
}; | ||
}; | ||
require('util').inherits(Bus, EventEmitter); | ||
Bus = new Bus(); | ||
// Fixtures | ||
var fixtures = { | ||
fn: function () { | ||
// Sniff call/caller data from this callback function by emitting event on cbReporter | ||
Bus.emit('fn', { | ||
args: Array.prototype.slice.call(arguments), | ||
ctx: this | ||
}); | ||
}, | ||
handlers: { | ||
success: function (data) {}, | ||
error: function (err) {} | ||
} | ||
}; | ||
// Suites | ||
describe('switchback(Function)', function (){ | ||
var sb; | ||
var cb = fixtures.fn; | ||
it('should return a switchback', function () { | ||
sb = switchback(cb); | ||
sb.should.be.a.Function; | ||
(switchback.isSwitchback(sb)).should.be.ok; | ||
}); | ||
describe('when calling returned switchback as sb(null, 14, 23),', function () { | ||
var cbOutcome = {}; | ||
_listenForCallback(cbOutcome, function () { | ||
sb(null, 14, 23); | ||
}); | ||
_testThatCallbackHasNoError(cbOutcome); | ||
_testThatOtherArgumentsExist(cbOutcome); | ||
}); | ||
describe('when calling returned switchback as sb("some error", 14, 23),', function () { | ||
var cbOutcome = {}; | ||
_listenForCallback(cbOutcome, function () { | ||
sb('some error', 14, 23); | ||
}); | ||
_testThatCallbackHasError(cbOutcome); | ||
_testThatOtherArgumentsExist(cbOutcome); | ||
}); | ||
describe('when calling returned switchback as sb.error("some error", 14, 23),', function () { | ||
var cbOutcome = {}; | ||
_listenForCallback(cbOutcome, function () { | ||
sb.error('some error', 14, 23); | ||
}); | ||
_testThatCallbackHasError(cbOutcome); | ||
_testThatOtherArgumentsExist(cbOutcome); | ||
}); | ||
describe('when calling returned switchback as sb.error(),', function () { | ||
var cbOutcome = {}; | ||
_listenForCallback(cbOutcome, function () { | ||
sb.error(); | ||
}); | ||
_testThatCallbackHasError(cbOutcome); | ||
}); | ||
describe('when calling returned switchback as sb.success("should not be an error", 14, 23),', function () { | ||
var cbOutcome = {}; | ||
_listenForCallback(cbOutcome, function () { | ||
sb.success('should not be an error', 14, 23); | ||
}); | ||
_testThatCallbackHasNoError(cbOutcome); | ||
_testThatOtherArgumentsExist(cbOutcome); | ||
}); | ||
describe('when calling returned switchback as sb.success(),', function () { | ||
var cbOutcome = {}; | ||
_listenForCallback(cbOutcome, function () { | ||
sb.success(); | ||
}); | ||
_testThatCallbackHasNoError(cbOutcome); | ||
}); | ||
}); | ||
describe('switcher(Handlers)', function (){ | ||
describe('switchback(Handlers)', function (){ | ||
}); | ||
describe('switcher(Function, Object)', function (){ | ||
describe('switchback(Function, Object)', function (){ | ||
}); | ||
describe('switcher(Handlers, Object)', function (){ | ||
describe('switchback(Handlers, Object)', function (){ | ||
@@ -22,7 +133,7 @@ }); | ||
describe('switcher(Function, Object, Object)', function (){ | ||
describe('switchback(Function, Object, Object)', function (){ | ||
}); | ||
describe('switcher(Handlers, Object, Object)', function (){ | ||
describe('switchback(Handlers, Object, Object)', function (){ | ||
@@ -32,1 +143,42 @@ }); | ||
// Helpers | ||
function _testThatCallbackHasError (cbOutcome) { | ||
it('should NOT receive error argument in original cb function', function () { | ||
should(cbOutcome.args).be.ok; | ||
should(cbOutcome.ctx).be.ok; | ||
cbOutcome.args.should.be.an.Array; | ||
cbOutcome.args.length.should.be.above(0); | ||
should(cbOutcome.args[0]).be.ok; | ||
}); | ||
} | ||
function _testThatCallbackHasNoError (cbOutcome) { | ||
it('should NOT receive error argument in original cb function', function () { | ||
should(cbOutcome.args).be.ok; | ||
should(cbOutcome.ctx).be.ok; | ||
cbOutcome.args.should.be.an.Array; | ||
should(cbOutcome.args[0]).not.be.ok; | ||
}); | ||
} | ||
function _testThatOtherArgumentsExist (cbOutcome) { | ||
it('should receive the two subsequent arguments in original cb function', function () { | ||
should(cbOutcome.args).be.ok; | ||
should(cbOutcome.ctx).be.ok; | ||
cbOutcome.args.should.be.an.Array; | ||
cbOutcome.args.length.should.be.above(1); | ||
should(cbOutcome.args[1]).be.ok; | ||
should(cbOutcome.args[2]).be.ok; | ||
}); | ||
} | ||
function _listenForCallback (cbOutcome, fnThatRunsCallback) { | ||
before(function (done) { | ||
// When callback fires, proceed | ||
Bus.afterFixture('fn', cbOutcome, done); | ||
fnThatRunsCallback(); | ||
}); | ||
} |
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
16867
11
306