cylon/spec/lib/driver.spec.js

170 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-12-15 07:21:28 +08:00
"use strict";
2013-12-19 05:02:18 +08:00
var Driver = lib("driver"),
Utils = lib("utils");
2014-03-22 03:17:39 +08:00
2014-02-28 06:52:25 +08:00
describe("Driver", function() {
2014-12-16 01:37:52 +08:00
var connection, driver;
2014-08-12 05:10:59 +08:00
beforeEach(function() {
connection = {
2014-12-15 07:21:28 +08:00
connection: "connection"
2014-11-01 04:48:05 +08:00
};
2014-08-12 05:10:59 +08:00
driver = new Driver({
2014-12-15 07:21:28 +08:00
name: "driver",
connection: connection,
2014-08-12 05:10:59 +08:00
});
2014-02-28 06:52:25 +08:00
});
2013-12-19 05:02:18 +08:00
2014-03-22 03:17:39 +08:00
describe("#constructor", function() {
it("sets @name to the provided name", function() {
2014-12-15 07:21:28 +08:00
expect(driver.name).to.be.eql("driver");
2014-02-28 06:52:25 +08:00
});
it("sets @connection to the provided connection", function() {
expect(driver.connection).to.be.eql(connection);
});
2014-08-12 05:10:59 +08:00
it("sets @commands to an empty object by default", function() {
2014-08-06 09:41:57 +08:00
expect(driver.commands).to.be.eql({});
2014-03-22 03:17:39 +08:00
});
2014-09-17 05:42:45 +08:00
it("sets @events to an empty array by default", function() {
expect(driver.events).to.be.eql([]);
});
2014-09-17 05:42:45 +08:00
it("sets @interval to 10ms by default, or the provided value", function() {
expect(driver.interval).to.be.eql(10);
2014-11-01 04:48:05 +08:00
2014-09-19 03:03:37 +08:00
driver = new Driver({
2014-12-15 07:21:28 +08:00
name: "driver",
connection: connection,
2014-11-01 04:50:22 +08:00
interval: 2000,
2014-09-19 03:03:37 +08:00
});
2014-11-01 04:48:05 +08:00
2014-09-17 05:42:45 +08:00
expect(driver.interval).to.be.eql(2000);
});
2014-02-28 06:52:25 +08:00
});
describe("#interface methods", function() {
var child;
var Child = function Child() {};
Utils.subclass(Child, Driver);
beforeEach(function() {
child = new Child();
});
describe("#start", function() {
it("throws an error unless overwritten", function() {
expect(child.start).to.throw();
child.start = function() {};
expect(child.start).to.not.throw();
});
});
describe("#halt", function() {
it("throws an error unless overwritten", function() {
expect(child.halt).to.throw();
child.halt = function() {};
expect(child.halt).to.not.throw();
});
});
});
describe("#toJSON", function() {
2015-04-15 12:49:12 +08:00
var json;
beforeEach(function() {
driver = new Driver({
connection: { name: "conn" },
name: "name",
port: "3000"
});
json = driver.toJSON();
});
it("returns an object", function() {
expect(json).to.be.a("object");
});
it("contains the driver's name", function() {
expect(json.name).to.eql("name");
});
it("contains the driver's constructor name", function() {
expect(json.driver).to.eql("Driver");
});
it("contains the driver's connection name", function() {
expect(json.connection).to.eql("conn");
});
it("contains the driver's commands", function() {
expect(json.commands).to.eql(Object.keys(driver.commands));
});
it("contains the driver's events, or an empty array", function() {
expect(json.events).to.eql([]);
driver.events = ["hello", "world"];
expect(driver.toJSON().events).to.be.eql(["hello", "world"]);
});
});
2014-08-12 05:10:59 +08:00
describe("#setupCommands", function() {
beforeEach(function() {
driver.proxyMethods = spy();
});
it("snake_cases and proxies methods to @commands for the API", function() {
var commands = ["helloWorld", "otherTestCommand"],
snake_case = ["hello_world", "other_test_command"];
2015-04-15 12:49:12 +08:00
commands.forEach(function(name) {
driver[name] = spy();
2014-08-12 05:10:59 +08:00
});
driver.setupCommands(commands);
for (var i = 0; i < commands.length; i++) {
var cmd = commands[i],
snake = snake_case[i];
2014-08-12 06:55:47 +08:00
expect(driver.commands[snake]).to.be.eql(driver[cmd]);
2014-08-12 05:10:59 +08:00
}
});
it("handles edge cases", function() {
var commands = [
"HelloWorld",
"getPNGStream",
"getHSetting",
"getRGB",
"convertRGBToHSL",
"getTemperatureInF"
];
var snake_case = [
"hello_world",
"get_png_stream",
"get_h_setting",
"get_rgb",
"convert_rgb_to_hsl",
"get_temperature_in_f"
];
2014-08-12 05:10:59 +08:00
commands.forEach(function(cmd) {
driver[cmd] = function() {};
});
driver.setupCommands(commands);
expect(Object.keys(driver.commands)).to.be.eql(snake_case);
});
});
2014-02-28 06:52:25 +08:00
});