From a1b230c62dc524ac367b6fe958d98509eab3484a Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Sun, 9 Mar 2014 19:06:06 -0700 Subject: [PATCH] Added specs for Cylon#findRobotConnection --- lib/cylon.js | 2 +- test/specs/cylon.spec.js | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/cylon.js b/lib/cylon.js index b5ee375..e6257d5 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -159,7 +159,7 @@ var Cylon = (function() { var error, connection = null; - if (err) { return callback(err, robot); } + if (err) { return callback ? callback(err, robot) : robot } if (robot.connections[connid]) { connection = robot.connections[connid]; } diff --git a/test/specs/cylon.spec.js b/test/specs/cylon.spec.js index 730f04d..954ecf1 100644 --- a/test/specs/cylon.spec.js +++ b/test/specs/cylon.spec.js @@ -178,4 +178,65 @@ describe("Cylon", function() { }); }); }); + + describe("#findRobotConnection", function() { + var bot, conn; + + before(function() { + bot = cylon.robot({ + name: "JARVIS", + connection: { name: "loopback", adaptor: "loopback" } + }); + + conn = bot.connections.loopback; + }); + + describe("async", 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); + expect(callback).to.be.calledWith(undefined, conn); + }); + }); + + 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); + var error = { error: "No connection found with the name nope." }; + expect(callback).to.be.calledWith(error, null); + }); + }); + + 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); + var error = { error: "No Robot found with the name Rob" }; + expect(callback).to.be.calledWith(error, null); + }); + }); + }); + + 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); + }); + }); + + context("looking for a valid robot and invalid connection", function() { + it("returns null", function() { + 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); + }); + }); + }); + }); });