cylon/test/specs/connection.spec.js

127 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2014-03-11 00:49:46 +08:00
"use strict";
2014-02-28 05:54:04 +08:00
var Loopback = source('test/loopback'),
Robot = source("robot"),
Logger = source('logger'),
Utils = source('utils');
2014-02-28 05:54:04 +08:00
describe("Connection", function() {
var robot, connection;
beforeEach(function() {
robot = new Robot({
name: "Robby",
connection: { name: 'loopback', adaptor: 'loopback', port: "/dev/null" }
});
connection = robot.connections.loopback;
2014-02-28 05:54:04 +08:00
});
2014-03-11 00:49:46 +08:00
describe("#constructor", function() {
it("sets @robot to the passed robot", function() {
expect(connection.robot).to.be.eql(robot);
});
2014-02-28 05:54:04 +08:00
2014-03-11 00:49:46 +08:00
it("sets @name to the passed name", function() {
expect(connection.name).to.be.eql('loopback');
});
2014-02-28 05:54:04 +08:00
2014-03-11 00:49:46 +08:00
it("sets @port to the passed port", function() {
expect(connection.port.toString()).to.be.eql("/dev/null");
});
it("proxies methods from the Adaptor", function() {
Loopback.prototype.test = function() { return "Test" };
robot = new Robot({
name: "Robby",
connection: { name: 'loopback', adaptor: 'loopback', port: "/dev/null" }
});
connection = robot.connections.loopback;
expect(connection.test()).to.be.eql("Test");
delete Loopback.prototype.test;
})
2014-02-28 05:54:04 +08:00
});
2014-03-04 04:24:29 +08:00
describe("#toJSON", function() {
var json;
beforeEach(function() {
json = connection.toJSON();
});
2014-03-04 04:24:29 +08:00
it("returns an object", function() {
expect(json).to.be.an('object');
2014-03-04 04:24:29 +08:00
});
it("contains the connection's name", function() {
expect(json.name).to.be.eql("loopback");
2014-03-04 04:24:29 +08:00
});
it("contains the connection's port", function() {
expect(json.details.port).to.be.eql("/dev/null");
2014-03-04 04:24:29 +08:00
});
it("contains the connection's adaptor name", function() {
expect(json.adaptor).to.be.eql("Loopback");
2014-03-04 04:24:29 +08:00
});
2014-03-11 00:49:46 +08:00
});
describe("#connect", function() {
var callback = function() { };
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-03-11 00:49:46 +08:00
stub(Logger, 'info').returns(true);
connection.adaptor.connect = stub().returns(true);
2014-03-11 00:49:46 +08:00
connection.connect(callback);
});
2014-07-05 00:49:29 +08:00
afterEach(function() {
2014-03-11 00:49:46 +08:00
Logger.info.restore();
});
it("logs that it's connecting the device", function() {
var message = "Connecting to 'loopback' on port /dev/null.";
2014-03-11 00:49:46 +08:00
expect(Logger.info).to.be.calledWith(message);
});
it("calls the adaptor's connect method with the provided callback", function() {
expect(connection.adaptor.connect).to.be.calledWith(callback);
});
});
describe("#disconnect", function() {
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-03-11 00:49:46 +08:00
stub(Logger, 'info').returns(true);
stub(connection, 'removeAllListeners');
2014-03-11 00:49:46 +08:00
2014-09-06 01:34:03 +08:00
connection.adaptor.disconnect = stub().returns(true);
2014-03-11 00:49:46 +08:00
connection.disconnect();
});
2014-07-05 00:49:29 +08:00
afterEach(function() {
connection.removeAllListeners.restore();
2014-03-11 00:49:46 +08:00
Logger.info.restore();
});
it("logs that it's disconnecting from the device", function() {
var message = "Disconnecting from 'loopback' on port /dev/null.";
2014-03-11 00:49:46 +08:00
expect(Logger.info).to.be.calledWith(message);
});
it("tells the adaptor to disconnect", function() {
expect(connection.adaptor.disconnect).to.be.called;
});
it("disconnects all event listeners", function() {
expect(connection.removeAllListeners).to.be.called;
});
2014-03-11 00:49:46 +08:00
});
2014-02-28 05:54:04 +08:00
});