From c1f72061e3dbc4bd10d74a26b201e6afc2520dae Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 16 May 2018 14:06:34 -0400 Subject: [PATCH] add tests for processLauncher --- lib/process/processLauncher.js | 6 +- test/processLauncher.js | 139 +++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 test/processLauncher.js diff --git a/lib/process/processLauncher.js b/lib/process/processLauncher.js index 7086d6963..e58db305e 100644 --- a/lib/process/processLauncher.js +++ b/lib/process/processLauncher.js @@ -111,7 +111,7 @@ class ProcessLauncher { */ subscribeTo(key, value, callback) { if (this.subscriptions[key]) { - this.subscriptions[key].push(value); + this.subscriptions[key].push({value, callback}); return; } this.subscriptions[key] = [{value, callback}]; @@ -131,7 +131,7 @@ class ProcessLauncher { if (this.subscriptions[key]) { this.subscriptions[key].filter((val, index) => { if (val.value === value) { - this.subscriptions[key] = this.subscriptions[key].splice(index, 1); + this.subscriptions[key].splice(index, 1); } }); } @@ -141,7 +141,7 @@ class ProcessLauncher { * Unsubscribes from all subscriptions * @return {void} */ - unsubsribeToAll() { + unsubscribeToAll() { this.subscriptions = {}; } diff --git a/test/processLauncher.js b/test/processLauncher.js new file mode 100644 index 000000000..e75d2b4ed --- /dev/null +++ b/test/processLauncher.js @@ -0,0 +1,139 @@ +/*global describe, it, before, beforeEach*/ +const assert = require('assert'); +const sinon = require('sinon'); +const TestLogger = require('../lib/tests/test_logger.js'); +const ProcessLauncher = require('../lib/process/processLauncher'); + +describe('ProcessWrapper', () => { + let processLauncher; + + before(() => { + sinon.stub(ProcessLauncher.prototype, '_subscribeToMessages'); + processLauncher = new ProcessLauncher({ + logger: new TestLogger({}) + }); + }); + + describe('subscribeTo', () => { + + beforeEach(() => { + processLauncher.subscriptions = {}; + }); + + it('should create an array for the key value', function () { + processLauncher.subscribeTo('test', 'value', 'myCallback'); + assert.deepEqual(processLauncher.subscriptions, { + "test": [ + { + "callback": "myCallback", + "value": "value" + } + ] + }); + }); + + it('should add another value to the key', () => { + processLauncher.subscribeTo('test', 'value', 'myCallback'); + processLauncher.subscribeTo('test', 'value2', 'myCallback2'); + assert.deepEqual(processLauncher.subscriptions, { + "test": [ + { + "callback": "myCallback", + "value": "value" + }, + { + "callback": "myCallback2", + "value": "value2" + } + ] + }); + }); + }); + + describe('unsubscribeTo', () => { + it('should remove the value for the key', () => { + processLauncher.subscriptions = { + "test": [ + { + "callback": "myCallback", + "value": "value" + }, + { + "callback": "myCallback2", + "value": "value2" + } + ] + }; + + processLauncher.unsubscribeTo('test', 'value2'); + assert.deepEqual(processLauncher.subscriptions, { + "test": [ + { + "callback": "myCallback", + "value": "value" + } + ] + }); + }); + + it('should remove the whole key', () => { + processLauncher.subscriptions = { + "test": [ + { + "callback": "myCallback", + "value": "value" + } + ] + }; + + processLauncher.unsubscribeTo('test'); + assert.deepEqual(processLauncher.subscriptions, {test: []}); + }); + }); + + describe('unsubscribeToAll', () => { + it('clears every subscriptions', () => { + processLauncher.subscriptions = { + "test": [ + { + "callback": "myCallback", + "value": "value" + } + ] + }; + + processLauncher.unsubscribeToAll(); + assert.deepEqual(processLauncher.subscriptions, {}); + }); + }); + + describe('_checkSubscriptions', function () { + it('should not do anything if not in subscription', function () { + const callback = sinon.stub(); + processLauncher.subscriptions = { + "test": [ + { + "callback": callback, + "value": "value" + } + ] + }; + processLauncher._checkSubscriptions({does: 'nothing', for: 'real'}); + assert.strictEqual(callback.callCount, 0); + }); + + it('should call the callback', function () { + const callback = sinon.stub(); + processLauncher.subscriptions = { + "test": [ + { + "callback": callback, + "value": "value" + } + ] + }; + processLauncher._checkSubscriptions({test: 'value'}); + assert.strictEqual(callback.callCount, 1); + }); + }); +});