cylon/spec/lib/basestar.spec.js

166 lines
4.5 KiB
JavaScript
Raw Normal View History

2014-02-28 09:43:34 +08:00
"use strict";
var Basestar = source('basestar'),
Utils = source('utils');
2013-11-23 07:12:30 +08:00
var EventEmitter = require('events').EventEmitter;
2013-11-23 07:12:30 +08:00
describe('Basestar', function() {
describe('#proxyMethods', function() {
2014-02-28 09:43:34 +08:00
var methods = ['asString', 'toString', 'returnString'];
2014-02-28 09:39:02 +08:00
2014-04-25 06:31:47 +08:00
var ProxyClass = function ProxyClass() {}
2013-11-23 07:12:30 +08:00
2014-04-25 06:31:47 +08:00
ProxyClass.prototype.asString = function() {
return "[object ProxyClass]";
};
2013-11-23 07:12:30 +08:00
2014-04-25 06:31:47 +08:00
ProxyClass.prototype.toString = function() {
return "[object ProxyClass]";
};
2013-11-23 07:12:30 +08:00
2014-04-25 06:31:47 +08:00
ProxyClass.prototype.returnString = function(string) {
return string;
};
2013-11-23 07:12:30 +08:00
2014-04-25 06:31:47 +08:00
var TestClass = function TestClass() {
this.testInstance = new ProxyClass;
this.proxyMethods(methods, this.testInstance, this, true);
}
2013-11-23 07:12:30 +08:00
Utils.subclass(TestClass, Basestar);
it('can alias methods', function() {
2014-02-28 09:43:34 +08:00
var testclass = new TestClass;
expect(testclass.asString).to.be.a('function')
expect(testclass.asString()).to.be.equal("[object ProxyClass]");
2013-11-23 07:12:30 +08:00
});
it('can alias existing methods if forced to', function() {
2014-02-28 09:43:34 +08:00
var testclass = new TestClass;
expect(testclass.toString).to.be.a('function')
expect(testclass.toString()).to.be.equal("[object ProxyClass]");
});
it('can alias methods with arguments', function() {
2014-02-28 09:43:34 +08:00
var testclass = new TestClass;
expect(testclass.returnString).to.be.a('function')
expect(testclass.returnString("testString")).to.be.equal("testString");
});
});
2014-02-28 10:13:21 +08:00
describe("#defineEvent", function() {
2014-04-25 06:31:47 +08:00
var ProxyClass = function ProxyClass() {};
var EmitterClass = function EmitterClass(update) {
update || (update = false);
this.proxy = new ProxyClass();
this.defineEvent({
eventName: "testevent",
source: this,
target: this.proxy,
sendUpdate: update
});
}
Utils.subclass(ProxyClass, Basestar);
Utils.subclass(EmitterClass, Basestar);
2014-02-28 10:13:21 +08:00
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'));
});
});
describe("#defineAdaptorEvent", function() {
var basestar;
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-04-25 06:31:47 +08:00
basestar = new Basestar();
basestar.connector = new EventEmitter();
});
it("proxies events between the connector and connection", function() {
var eventSpy = spy();
2014-11-12 03:41:18 +08:00
basestar.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();
2014-11-12 03:41:18 +08:00
basestar.on('testevent', eventSpy);
basestar.defineAdaptorEvent("testevent");
basestar.connector.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
});
});
describe("#defineDriverEvent", function() {
var basestar;
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-04-25 06:31:47 +08:00
basestar = new Basestar();
basestar.connection = new EventEmitter();
});
it("proxies events between the connection and device", function() {
var eventSpy = spy();
2014-11-12 03:41:18 +08:00
basestar.on('testevent', eventSpy);
basestar.defineDriverEvent({ eventName: "testevent" });
2014-11-12 03:41:18 +08:00
basestar.adaptor.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
context("when given a string", function() {
it("uses it as the eventName", function() {
var eventSpy = spy();
2014-11-12 03:41:18 +08:00
basestar.on('testevent', eventSpy);
basestar.defineDriverEvent("testevent");
2014-11-12 03:41:18 +08:00
basestar.adaptor.emit("testevent", "data");
assert(eventSpy.calledWith('data'));
});
});
});
});