Add a JS disconnect test and mock hardware disconnect command.

This commit is contained in:
Francis Gulotta 2013-12-12 09:27:24 -05:00
parent 82397f5f38
commit dd642c95d4
2 changed files with 31 additions and 10 deletions

View file

@ -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');
});
});
});
});

View file

@ -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) {