Add specs for event proxying
This commit is contained in:
parent
c70ed66b77
commit
84f06afa5b
|
@ -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;
|
||||
|
|
|
@ -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'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue