From dd642c95d4aa160ae9152eeefd1db4c197ae5618 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Thu, 12 Dec 2013 09:27:24 -0500 Subject: [PATCH] Add a JS disconnect test and mock hardware disconnect command. --- test/serialport-basic.js | 29 +++++++++++++++++++---------- test_mocks/linux-hardware.js | 12 ++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/test/serialport-basic.js b/test/serialport-basic.js index c764537..bcb2ead 100644 --- a/test/serialport-basic.js +++ b/test/serialport-basic.js @@ -8,14 +8,15 @@ var MockedSerialPort = require('../test_mocks/linux-hardware'); var SerialPort = MockedSerialPort.SerialPort; var hardware = MockedSerialPort.hardware; -// Create a port for fun and profit -hardware.createPort('/dev/exists'); - describe('SerialPort', function () { var sandbox; beforeEach(function () { sandbox = sinon.sandbox.create(); + + // Create a port for fun and profit + hardware.reset(); + hardware.createPort('/dev/exists'); }); afterEach(function () { @@ -54,7 +55,6 @@ describe('SerialPort', function () { describe('reading data', function () { it('emits data events by default', function (done) { - hardware.createPort('/dev/exists'); // clears out any previously written data var testData = new Buffer("I am a really short string"); var port = new SerialPort('/dev/exists', function () { port.once('data', function(recvData) { @@ -66,7 +66,6 @@ describe('SerialPort', function () { }); it('calls the dataCallback if set', function (done) { - hardware.createPort('/dev/exists'); // clears out any previously written data var testData = new Buffer("I am a really short string"); var opt = { dataCallback: function (recvData) { @@ -94,7 +93,6 @@ describe('SerialPort', function () { }); it('calls back an error when opening an invalid port', function (done) { - var port = new SerialPort('/dev/unhappy', {}, false); port.open(function (err) { expect(err).to.be.ok; @@ -103,22 +101,33 @@ describe('SerialPort', function () { }); it("emits data after being reopened", function (done) { - hardware.createPort('/dev/fun'); - var data = new Buffer("Howdy!"); - var port = new SerialPort('/dev/fun', function () { + var port = new SerialPort('/dev/exists', function () { port.close(); port.open(function () { port.once('data', function (res) { expect(res).to.eql(data); done(); }); - hardware.emitData('/dev/fun', data); + hardware.emitData('/dev/exists', data); }); }); }); }); + describe('disconnect', function (done) { + it("fires a disconnect event", function (done) { + var port = new SerialPort('/dev/exists', { + disconnectedCallback: function (err) { + expect(err).to.not.be.ok; + done(); + } + }, function () { + hardware.disconnect('/dev/exists'); + }); + }); + }); + }); diff --git a/test_mocks/linux-hardware.js b/test_mocks/linux-hardware.js index 4f26e46..46aecc4 100644 --- a/test_mocks/linux-hardware.js +++ b/test_mocks/linux-hardware.js @@ -39,6 +39,10 @@ var Hardware = function () { }; }; +Hardware.prototype.reset = function () { + this.ports = {}; +}; + Hardware.prototype.createPort = function (path) { this.ports[path] = { data: new Buffer(0), @@ -66,6 +70,14 @@ Hardware.prototype.emitData = function (path, data) { port.poller && port.poller.detectRead(); }; +Hardware.prototype.disconnect = function (path) { + var port = this.ports[path]; + if (!port) { + throw new Error(path + " does not exist - please call hardware.createPort(path) first"); + } + port.openOpt.disconnectedCallback(); +}; + Hardware.prototype.list = function (cb) { var ports = this.ports; var info = Object.keys(ports).map(function (path) {