diff --git a/lib/factory/test/directChannel.js b/lib/factory/test/directChannel.js index 31e5dbd..a98f3df 100644 --- a/lib/factory/test/directChannel.js +++ b/lib/factory/test/directChannel.js @@ -2,7 +2,7 @@ const expect = require('chai').expect const directChannel = require('../directChannel') const basic = { - members:[ + members: [ 'username1', 'username2' ] diff --git a/lib/modules/test/fakedb.js b/lib/modules/test/fakedb.js new file mode 100644 index 0000000..110a440 --- /dev/null +++ b/lib/modules/test/fakedb.js @@ -0,0 +1,50 @@ +// +// Implements a fake db for testing +// +class FakeDB { + // + // Constructor + // + constructor(data) { + this.data = data + this.fetch = this.fetch.bind(this) + this.pipe = this.pipe.bind(this) + } + + // + // Simulate a db connection by just returning + // a promise + // + connect() { + return Promise.resolve() + } + + // + // Returns all results in one batch + // + fetch() { + return Promise.resolve({ + recordset: this.data + }) + } + + // + // Delivers results to the specified write + // stream + // + pipe(query, writable) { + + this.data.forEach(function(record) { + writable.write(record) + }) + + writable.end() + + return writable + } +} + +// +// Export the class +// +module.exports = FakeDB diff --git a/lib/modules/test/posts.js b/lib/modules/test/posts.js index c596b65..30c936f 100644 --- a/lib/modules/test/posts.js +++ b/lib/modules/test/posts.js @@ -1,13 +1,13 @@ const expect = require('chai').expect -const { spy } = require('sinon') -const { Writable } = require('stream') const posts = require('../posts') const Fixtures = require('./fixtures') +const FakeDB = require('./fakedb') +const sink = require('./sink') const context = require('./context')() describe('modules.posts', function() { - it('should process post objects', function(done) { + before(function() { // // Set up context values // @@ -43,26 +43,20 @@ describe('modules.posts', function() { } } } + }) + + beforeEach(function() { // - // Stub the data source + // Stub the output stream // - context.jabber.pipe.callsFake(function(query, writable) { - Fixtures.posts.ok.forEach(function(post) { - writable.write(post) - }) - writable.end() - return writable - }) + context.output = sink() + }) + + it('should process post objects', function(done) { // - // Stub the output stream + // Set up the DB // - context.output = new Writable({ - objectMode: true, - write(chunk, encoding, callback) { - return callback() - } - }) - spy(context.output, 'write') + context.jabber = new FakeDB(Fixtures.posts.ok) // // Process the posts // @@ -88,7 +82,37 @@ describe('modules.posts', function() { }) }) - afterEach(function() { - context.jabber.pipe.reset() + it('should fail on user not found', function(done) { + // + // Set up the DB + // + context.jabber = new FakeDB(Fixtures.posts.userNotFound) + // + // Process the posts + // + posts(context).then(function(c) { + expect(c).to.be.undefined + }).catch(function(e){ + expect(e).to.be.an('error') + expect(e.message).to.equal('user terrence.flynn@chat.hsinuat.dhs.gov not found') + done() + }) + }) + + it('should fail on body not found', function(done) { + // + // Set up the DB + // + context.jabber = new FakeDB(Fixtures.posts.bodyNotFound) + // + // Process the posts + // + posts(context).then(function(c) { + expect(c).to.be.undefined + }).catch(function(e){ + expect(e).to.be.an('error') + expect(e.message).to.equal('channel admin@conference.chat.hsinuat.dhs.gov not found') + done() + }) }) }) diff --git a/lib/modules/test/sink.js b/lib/modules/test/sink.js new file mode 100644 index 0000000..d8f2a2f --- /dev/null +++ b/lib/modules/test/sink.js @@ -0,0 +1,18 @@ +const { spy } = require('sinon') +const { Writable } = require('stream') + +// +// Returns a spied writable stream +// +module.exports = function() { + + var writable = new Writable({ + objectMode: true, + write(chunk, encoding, callback) { + return callback() + } + }) + spy(writable, 'write') + + return writable +}