UNPKG

eventory

Version:
204 lines (137 loc) 5.18 kB
import EventHub from '../index.es6'; var chai = require('chai'); var should = chai.should(); console.log(EventHub); var hub = new EventHub(); hub.timeout = 1000; describe('Hub', function () { before(function(){ }); it('has', function (next) { hub.on('key0', function(params, cb) { cb(null,params) }); should.equal(hub.has('key0'),true); next(); }); it('simple', function (next) { hub.on('key0', function(params, cb) { cb(null,params) }); hub.on('key1', function(params, cb) { cb(null,params) }); hub.emit('key1', {a:1}, function (err, r1, r2){ console.log('>>',arguments); should.equal(r1.a, 1); next(); }); }); it('query', function (next) { hub.emit('key1?a=1&b=2', {}, function (err, r1, r2){ console.log('>>',arguments); should.equal(r1.a, "1"); should.equal(r1.b, "2"); next(); }); }); it('multiple', function (next) { hub.on('key1', function(params, cb) { setTimeout(function(){cb(null,params)},20)}, 'o1'); hub.on('key1', function(params, cb) { cb(null,params) }); hub.emit('key1', {a:1}, function (err, r1, r2){ console.log('>>',arguments); should.equal(r1.a, r2.a); next(); }); }); it('placeholder', function (next) { hub.on('key1:*', function(params, cb) { setTimeout(function(){cb(null,params)},20)}, 'o1'); hub.emit('key1:a1.a2', {a:1}, function (err, r){ console.log('>>',arguments); should.equal(r.key, 'a1.a2'); next(); }); }); it('offOwner', function (next) { hub.offOwner('o1'); hub.emit('key1', {a:1}, function (err, r1, r2, r3){ console.log('>>',arguments); should.equal(r1.a, r2.a); should.equal(r3, undefined); next(); }); }); it('off', function (next) { hub.off('key1'); hub.emit('key1', {a:1}, function (err, r1, r2){ console.log('>>',arguments); should.exist(err); should.equal(err.code, 404); next(); }); }); it('defered', function (next) { hub.emit('defered', {a:'defered'}, function (err, r1, r2){ console.log('defered >>',arguments); should.equal(r1.a, 'defered'); next(); }); console.log('defered >>>>>'); hub.once('defered', function(params, cb) { setTimeout(function(){cb(null,params)},200)}); console.log('defered 2>>>>>'); }); it('non-defered', function (next) { hub.emit('non-defered', {a:1, deferTimeout:0}, function (err, r1, r2){ console.log('>>',arguments); should.exist(err); should.equal(err.code, 404); next(); }); hub.once('non-defered', function(params, cb) { setTimeout(function(){cb(null,params)},10)}); }); it('timeout', function (next) { hub.once('timeout', function(params, cb) { setTimeout(function(){cb(null,params)},2000)}); hub.emit('timeout', {a:1, timeout:1000}, function (err, r1){ console.log('>>',arguments); should.exist(err); should.equal(err.code, 504); next(); }); }); it('key-as-function', function (next) { hub.once('keyFn', function(params, cb) { setTimeout(function(){cb(null,params)},100)}); hub.emit(function(params){params.a=2; return 'keyFn'}, {a:1}, function (err, r1){ console.log('>>',arguments); should.equal(r1.a, 2); next(); }); }); it('key-as-object', function (next) { hub.once('keyObj', function(params, cb) { setTimeout(function(){cb(null,params)},100)}); hub.emit({key:'keyObj', params:{a:2} }, {a:1}, function (err, r1){ console.log('>>',arguments); should.equal(r1.a, 2); next(); }); }); it('key-as-fn-object', function (next) { hub.once('keyFnObj', function(params, cb) { setTimeout(function(){cb(null,params)},100)}); hub.emit(function(){return {key:'keyFnObj', params:{a:2} }}, {a:3}, function (err, r1){ console.log('>>',arguments); should.equal(r1.a, 2); next(); }); }); it('404', function (next) { hub.emit('404', {}, function (err, r1, r2){ console.log('>>',arguments); should.exist(err); should.equal(err.code, 404); next(); }); }); it('400', function (next) { hub.emit(null, {}, function (err, r1, r2){ console.log('>>',arguments); should.exist(err); should.equal(err.code, 400); next(); }); }); after(function(){ }); });