Tidy up Cylon specs, add specs for exports

This commit is contained in:
Andrew Stewart 2014-05-13 19:48:50 -07:00
parent c22ef95239
commit 34e562207d
1 changed files with 75 additions and 59 deletions

View File

@ -1,37 +1,53 @@
"use strict";
var cylon = source("cylon");
var Cylon = source("cylon");
var Logger = source('logger'),
Adaptor = source('adaptor'),
Driver = source('driver');
describe("Cylon", function() {
describe("#constructor", function() {
it("sets @api_instance to null by default", function() {
expect(cylon.api_instance).to.be.eql(null);
describe("exports", function() {
it("sets Logger to the Logger module", function() {
expect(Cylon.Logger).to.be.eql(Logger);
});
it("sets @api_config to an object containing host/port info", function() {
var config = cylon.api_config;
expect(config).to.be.an('object');
expect(config.host).to.be.eql('127.0.0.1');
expect(config.port).to.be.eql('3000');
it("sets Adaptor to the Adaptor module", function() {
expect(Cylon.Adaptor).to.be.eql(Adaptor);
});
it("sets @robots to an empty array by default", function() {
expect(cylon.robots).to.be.eql([]);
it("sets Driver to the Driver module", function() {
expect(Cylon.Driver).to.be.eql(Driver);
});
});
it("sets @api_instance to null by default", function() {
expect(Cylon.api_instance).to.be.eql(null);
});
it("sets @api_config to an object containing host/port info", function() {
var config = Cylon.api_config;
expect(config).to.be.an('object');
expect(config.host).to.be.eql('127.0.0.1');
expect(config.port).to.be.eql('3000');
});
it("sets @robots to an empty array by default", function() {
expect(Cylon.robots).to.be.eql([]);
});
describe("#robot", function() {
after(function() {
cylon.robots = [];
Cylon.robots = [];
});
it("uses passed options to create a new Robot", function() {
var opts = { name: "Ultron" };
var robot = cylon.robot(opts);
var robot = Cylon.robot(opts);
expect(robot.toString()).to.be.eql("[Robot name='Ultron']")
expect(cylon.robots.pop()).to.be.eql(robot);
expect(Cylon.robots.pop()).to.be.eql(robot);
});
});
@ -49,13 +65,13 @@ describe("Cylon", function() {
// this is the shortest, cheapest way to dup an object in JS.
// I don't like it either.
cylon.api_config = JSON.parse(JSON.stringify(expectedConfig));
Cylon.api_config = JSON.parse(JSON.stringify(expectedConfig));
})
context("without arguments", function() {
it("returns the current API configuration", function() {
cylon.api();
expect(cylon.api_config).to.be.eql(expectedConfig);
Cylon.api();
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
@ -63,18 +79,18 @@ describe("Cylon", function() {
it("changes the port, but not the host", function() {
expectedConfig.port = "4000";
cylon.api({ port: "4000" });
Cylon.api({ port: "4000" });
expect(cylon.api_config).to.be.eql(expectedConfig);
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
context("only specifying host", function() {
it("changes the host, but not the port", function() {
expectedConfig.host = "0.0.0.0";
cylon.api({ host: "0.0.0.0" });
Cylon.api({ host: "0.0.0.0" });
expect(cylon.api_config).to.be.eql(expectedConfig);
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
@ -83,9 +99,9 @@ describe("Cylon", function() {
expectedConfig.host = "0.0.0.0";
expectedConfig.port = "4000";
cylon.api({ host: "0.0.0.0", port: "4000" });
Cylon.api({ host: "0.0.0.0", port: "4000" });
expect(cylon.api_config).to.be.eql(expectedConfig);
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
@ -94,14 +110,14 @@ describe("Cylon", function() {
expectedConfig.ssl.cert = "/path/to/cert/file";
expectedConfig.ssl.key = "/path/to/key/file";
cylon.api({
Cylon.api({
ssl: {
cert: "/path/to/cert/file",
key: "/path/to/key/file"
}
});
expect(cylon.api_config).to.be.eql(expectedConfig);
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
@ -109,9 +125,9 @@ describe("Cylon", function() {
it("changes the auth strategy", function() {
var auth = { type: 'basic', user: 'user', pass: 'pass'}
expectedConfig.auth = auth;
cylon.api({ auth: auth })
Cylon.api({ auth: auth })
expect(cylon.api_config).to.be.eql(expectedConfig);
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
@ -119,9 +135,9 @@ describe("Cylon", function() {
it("changes the CORS restrictions", function() {
var CORS = "https://localhost:4000";
expectedConfig.CORS = CORS;
cylon.api({ CORS: CORS })
Cylon.api({ CORS: CORS })
expect(cylon.api_config).to.be.eql(expectedConfig);
expect(Cylon.api_config).to.be.eql(expectedConfig);
});
});
});
@ -130,14 +146,14 @@ describe("Cylon", function() {
var bot;
before(function() {
bot = cylon.robot({ name: "Robby" })
bot = Cylon.robot({ name: "Robby" })
});
describe("async", function() {
context("looking for a robot that exists", function() {
it("calls the callback with the robot", function() {
var callback = spy();
cylon.findRobot("Robby", callback);
Cylon.findRobot("Robby", callback);
expect(callback).to.be.calledWith(undefined, bot);
});
});
@ -145,7 +161,7 @@ describe("Cylon", function() {
context("looking for a robot that does not exist", function(){
it("calls the callback with no robot and an error message", function() {
var callback = spy();
cylon.findRobot("Ultron", callback);
Cylon.findRobot("Ultron", callback);
var error = { error: "No Robot found with the name Ultron" };
expect(callback).to.be.calledWith(error, null);
});
@ -155,13 +171,13 @@ describe("Cylon", function() {
describe("sync", function() {
context("looking for a robot that exists", function() {
it("returns the robot", function() {
expect(cylon.findRobot("Robby")).to.be.eql(bot);
expect(Cylon.findRobot("Robby")).to.be.eql(bot);
});
});
context("looking for a robot that does not exist", function(){
it("returns null", function() {
expect(cylon.findRobot("Ultron")).to.be.eql(null);
expect(Cylon.findRobot("Ultron")).to.be.eql(null);
});
});
});
@ -171,7 +187,7 @@ describe("Cylon", function() {
var bot, device;
before(function() {
bot = cylon.robot({
bot = Cylon.robot({
name: "Ultron",
device: { name: "ping", driver: "ping" }
});
@ -183,7 +199,7 @@ describe("Cylon", function() {
context("looking for a valid robot/device", function() {
it("calls the callback with the device and no error message", function() {
var callback = spy();
cylon.findRobotDevice("Ultron", "ping", callback);
Cylon.findRobotDevice("Ultron", "ping", callback);
expect(callback).to.be.calledWith(undefined, device);
});
});
@ -191,7 +207,7 @@ describe("Cylon", function() {
context("looking for a valid robot and invalid device", function() {
it("calls the callback with no device and an error message", function() {
var callback = spy();
cylon.findRobotDevice("Ultron", "nope", callback);
Cylon.findRobotDevice("Ultron", "nope", callback);
var error = { error: "No device found with the name nope." };
expect(callback).to.be.calledWith(error, null);
});
@ -200,7 +216,7 @@ describe("Cylon", function() {
context("looking for an invalid robot", function() {
it("calls the callback with no device and an error message", function() {
var callback = spy();
cylon.findRobotDevice("Rob", "ping", callback);
Cylon.findRobotDevice("Rob", "ping", callback);
var error = { error: "No Robot found with the name Rob" };
expect(callback).to.be.calledWith(error, null);
});
@ -210,19 +226,19 @@ describe("Cylon", function() {
describe("synchronous", function() {
context("looking for a valid robot/device", function() {
it("returns the device", function() {
expect(cylon.findRobotDevice("Ultron", "ping")).to.be.eql(device);
expect(Cylon.findRobotDevice("Ultron", "ping")).to.be.eql(device);
});
});
context("looking for a valid robot and invalid device", function() {
it("returns null", function() {
expect(cylon.findRobotDevice("Ultron", "nope")).to.be.eql(null);
expect(Cylon.findRobotDevice("Ultron", "nope")).to.be.eql(null);
});
});
context("looking for an invalid robot", function() {
it("returns null", function() {
expect(cylon.findRobotDevice("Rob", "ping")).to.be.eql(null);
expect(Cylon.findRobotDevice("Rob", "ping")).to.be.eql(null);
});
});
});
@ -232,7 +248,7 @@ describe("Cylon", function() {
var bot, conn;
before(function() {
bot = cylon.robot({
bot = Cylon.robot({
name: "JARVIS",
connection: { name: "loopback", adaptor: "loopback" }
});
@ -244,7 +260,7 @@ describe("Cylon", function() {
context("looking for a valid robot/connection", function() {
it("calls the callback with the connection and no error message", function() {
var callback = spy();
cylon.findRobotConnection("JARVIS", "loopback", callback);
Cylon.findRobotConnection("JARVIS", "loopback", callback);
expect(callback).to.be.calledWith(undefined, conn);
});
});
@ -252,7 +268,7 @@ describe("Cylon", function() {
context("looking for a valid robot and invalid connection", function() {
it("calls the callback with no connection and an error message", function() {
var callback = spy();
cylon.findRobotConnection("JARVIS", "nope", callback);
Cylon.findRobotConnection("JARVIS", "nope", callback);
var error = { error: "No connection found with the name nope." };
expect(callback).to.be.calledWith(error, null);
});
@ -261,7 +277,7 @@ describe("Cylon", function() {
context("looking for an invalid robot", function() {
it("calls the callback with no connection and an error message", function() {
var callback = spy();
cylon.findRobotConnection("Rob", "loopback", callback);
Cylon.findRobotConnection("Rob", "loopback", callback);
var error = { error: "No Robot found with the name Rob" };
expect(callback).to.be.calledWith(error, null);
});
@ -271,19 +287,19 @@ describe("Cylon", function() {
describe("synchronous", function() {
context("looking for a valid robot/connection", function() {
it("returns the connection", function() {
expect(cylon.findRobotConnection("JARVIS", "loopback")).to.be.eql(conn);
expect(Cylon.findRobotConnection("JARVIS", "loopback")).to.be.eql(conn);
});
});
context("looking for a valid robot and invalid connection", function() {
it("returns null", function() {
expect(cylon.findRobotConnection("JARVIS", "nope")).to.be.eql(null);
expect(Cylon.findRobotConnection("JARVIS", "nope")).to.be.eql(null);
});
});
context("looking for an invalid robot", function() {
it("returns null", function() {
expect(cylon.findRobotConnection("Rob", "loopback")).to.be.eql(null);
expect(Cylon.findRobotConnection("Rob", "loopback")).to.be.eql(null);
});
});
});
@ -291,26 +307,26 @@ describe("Cylon", function() {
describe("#start", function() {
before(function() {
cylon.robots = [];
stub(cylon, 'startAPI').returns(true);
Cylon.robots = [];
stub(Cylon, 'startAPI').returns(true);
});
after(function() {
cylon.startAPI.restore();
Cylon.startAPI.restore();
});
it("starts the API", function() {
cylon.start();
expect(cylon.startAPI).to.be.called;
Cylon.start();
expect(Cylon.startAPI).to.be.called;
});
it("calls #start() on all robots", function() {
var bot1 = { start: spy() },
bot2 = { start: spy() };
cylon.robots = [bot1, bot2];
Cylon.robots = [bot1, bot2];
cylon.start();
Cylon.start();
expect(bot1.start).to.be.called;
expect(bot2.start).to.be.called;
@ -319,16 +335,16 @@ describe("Cylon", function() {
describe("#halt", function() {
before(function() {
cylon.robots = [];
Cylon.robots = [];
});
it("calls #halt() on all robots", function() {
var bot1 = { halt: spy() },
bot2 = { halt: spy() };
cylon.robots = [bot1, bot2];
Cylon.robots = [bot1, bot2];
cylon.halt();
Cylon.halt();
expect(bot1.halt).to.be.called;
expect(bot2.halt).to.be.called;