From e1edc2f5eff1d6ccd5b4974aac188a51830c2ef1 Mon Sep 17 00:00:00 2001 From: Daniel Wippermann Date: Sat, 15 Feb 2014 19:04:51 +0100 Subject: [PATCH] Complete SerialDataSourceProvider tests. --- .../specs/serial-data-source-provider.spec.js | 101 +++++++++++++++++- 1 file changed, 98 insertions(+), 3 deletions(-) diff --git a/test/specs/serial-data-source-provider.spec.js b/test/specs/serial-data-source-provider.spec.js index 735e8006..294a698a 100644 --- a/test/specs/serial-data-source-provider.spec.js +++ b/test/specs/serial-data-source-provider.spec.js @@ -3,7 +3,17 @@ -var SerialDataSourceProvider = require('./resol-vbus').SerialDataSourceProvider; +var Q = require('q'); +var serialport = require('serialport'); + + +var vbus = require('./resol-vbus'); +var testUtils = require('./test-utils'); + + + +var SerialDataSource = vbus.SerialDataSource; +var SerialDataSourceProvider = vbus.SerialDataSourceProvider; @@ -12,12 +22,97 @@ describe('SerialDataSourceProvider', function() { describe('constructor', function() { it('should be a constructor function', function() { - expect(SerialDataSourceProvider).to.be.a('function'); + expect(SerialDataSourceProvider) + .to.be.a('function') + .that.has.a.property('extend') + .that.is.a('function'); + }); + + }); + + describe('#discoverDataSources', function() { + + it('should be a method', function() { + expect(SerialDataSourceProvider.prototype) + .to.have.a.property('discoverDataSources') + .that.is.a('function'); + }); + + it('should work correctly', function(done) { + var originalList = serialport.list; + + var ports = [ + { comName: 'SERIALPORT1' }, + { comName: 'SERIALPORT2' }, + ]; + + serialport.list = sinon.spy(function(callback) { + callback(null, ports); + }); + + var dsp = new SerialDataSourceProvider(); + + testUtils.performAsyncTest(done, function() { + return Q.fcall(function() { + var promise = dsp.discoverDataSources(); + + return testUtils.expectPromise(promise); + }).then(function(dataSources) { + expect(dataSources) + .to.be.an('array') + .lengthOf(ports.length); + }).finally(function() { + serialport.list = originalList; + }); + }); + }); + + it('should reject if an error occurs', function(done) { + var originalList = serialport.list; + + serialport.list = sinon.spy(function(callback) { + callback(new Error('ERROR')); + }); + + var dsp = new SerialDataSourceProvider(); + + var callback = function(err) { + if (err) { + done(); + } else { + done(new Error('Should have thrown')); + } + }; + + testUtils.performAsyncTest(callback, function() { + return Q.fcall(function() { + var promise = dsp.discoverDataSources(); + + return testUtils.expectPromise(promise); + }).finally(function() { + serialport.list = originalList; + }); + }); }); }); - xit('should perform tests...', function() { + describe('#createDataSource', function() { + + it('should be a method', function() { + expect(SerialDataSourceProvider.prototype) + .to.have.a.property('createDataSource') + .that.is.a('function'); + }); + + it('should work correctly', function() { + var dsp = new SerialDataSourceProvider(); + + var ds = dsp.createDataSource(); + + expect(ds) + .to.be.instanceOf(SerialDataSource); + }); });