Add specs for event proxying

This commit is contained in:
Andrew Stewart 2014-02-27 18:13:21 -08:00
parent c70ed66b77
commit 84f06afa5b
2 changed files with 62 additions and 4 deletions

View File

@ -48,7 +48,7 @@ namespace("Cylon", function() {
// target
// - target - object to proxy event to
// - source - object to proxy event from
// - update - whether or not to send an 'update' event
// - sendUpdate - whether or not to send an 'update' event
//
// Returns the source
Basestar.prototype.defineEvent = function(opts) {
@ -58,10 +58,10 @@ namespace("Cylon", function() {
sendUpdate = opts.sendUpdate || false;
opts.source.on(opts.eventName, function() {
var args, _ref, _ref1;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
(_ref = opts.target).emit.apply(_ref, [targetEventName].concat(__slice.call(args)));
args = 1 <= arguments.length ? [].slice.call(arguments, 0) : [];
(_ref = opts.target).emit.apply(_ref, [targetEventName].concat([].slice.call(args)));
if (sendUpdate) {
return (_ref1 = opts.target).emit.apply(_ref1, ['update', targetEventName].concat(__slice.call(args)));
return (_ref1 = opts.target).emit.apply(_ref1, ['update', targetEventName].concat([].slice.call(args)));
}
});
return opts.source;

View File

@ -64,4 +64,62 @@ describe('Basestar', function() {
expect(testclass.returnString("testString")).to.be.equal("testString");
});
});
describe("#defineEvent", function() {
var ProxyClass = (function(klass) {
subclass(ProxyClass, klass);
function ProxyClass() {}
return ProxyClass;
})(Cylon.Basestar);
var EmitterClass = (function(klass) {
subclass(EmitterClass, klass);
function EmitterClass(update) {
update || (update = false);
this.proxy = new ProxyClass();
this.defineEvent({
eventName: "testevent",
source: this,
target: this.proxy,
sendUpdate: update
});
}
return EmitterClass;
})(Cylon.Basestar);
it("proxies events from one class to another", function() {
var eventSpy = spy(),
testclass = new EmitterClass(),
proxy = testclass.proxy;
proxy.on('testevent', eventSpy);
testclass.emit('testevent', 'data');
assert(eventSpy.calledWith('data'))
});
it("emits an 'update' event if told to", function() {
var updateSpy = spy(),
testclass = new EmitterClass(true),
proxy = testclass.proxy;
proxy.on('update', updateSpy);
testclass.emit('testevent', 'data');
assert(updateSpy.calledWith('testevent', 'data'));
});
it("does not emit an 'update' event by default", function() {
var updateSpy = spy(),
testclass = new EmitterClass(),
proxy = testclass.proxy;
proxy.on('update', updateSpy);
testclass.emit('testevent', 'data');
assert(!updateSpy.calledWith('testevent', 'data'));
});
});
});