Add shortcut for defining adaptor/driver events

This commit is contained in:
Andrew Stewart 2014-02-27 21:17:44 -08:00
parent 473e0ab08f
commit 84916f69e3
2 changed files with 70 additions and 2 deletions

View File

@ -77,9 +77,11 @@ namespace("Cylon", function() {
//
// Returns @connector
Basestar.prototype.defineAdaptorEvent = function(opts) {
if (typeof opts === 'string') { opts = { eventName: opts } }
opts['source'] = this.connector;
opts['target'] = this.connection;
if (opts['sendUpdate'] == null) { opts['sendUpdate'] = false; }
return this.defineEvent(opts);
};
@ -91,9 +93,11 @@ namespace("Cylon", function() {
//
// Returns @connection
Basestar.prototype.defineDriverEvent = function(opts) {
if (typeof opts === 'string') { opts = { eventName: opts } }
opts['source'] = this.connection;
opts['target'] = this.device;
if (opts['sendUpdate'] == null) { opts['sendUpdate'] = true; }
return this.defineEvent(opts);
};

View File

@ -122,4 +122,68 @@ describe('Basestar', function() {
assert(!updateSpy.calledWith('testevent', 'data'));
});
});
describe("#defineAdaptorEvent", function() {
var basestar;
before(function() {
basestar = new Cylon.Basestar();
basestar.connector = new EventEmitter();
basestar.connection = new EventEmitter();
});
it("proxies events between the connector and connection", function() {
var eventSpy = spy();
basestar.connection.on('testevent', eventSpy);
basestar.defineAdaptorEvent({ eventName: "testevent" });
basestar.connector.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
context("when given a string", function() {
it("uses it as the eventName", function() {
var eventSpy = spy();
basestar.connection.on('testevent', eventSpy);
basestar.defineAdaptorEvent("testevent");
basestar.connector.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
});
});
describe("#defineDriverEvent", function() {
var basestar;
before(function() {
basestar = new Cylon.Basestar();
basestar.connection = new EventEmitter();
basestar.device = new EventEmitter();
});
it("proxies events between the connection and device", function() {
var eventSpy = spy();
basestar.device.on('testevent', eventSpy);
basestar.defineDriverEvent({ eventName: "testevent" });
basestar.connection.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
context("when given a string", function() {
it("uses it as the eventName", function() {
var eventSpy = spy();
basestar.device.on('testevent', eventSpy);
basestar.defineDriverEvent("testevent");
basestar.connection.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
});
});
});