add tests for processLauncher

This commit is contained in:
Jonathan Rainville 2018-05-16 14:06:34 -04:00
parent 441063dd10
commit c1f72061e3
2 changed files with 142 additions and 3 deletions

View File

@ -111,7 +111,7 @@ class ProcessLauncher {
*/ */
subscribeTo(key, value, callback) { subscribeTo(key, value, callback) {
if (this.subscriptions[key]) { if (this.subscriptions[key]) {
this.subscriptions[key].push(value); this.subscriptions[key].push({value, callback});
return; return;
} }
this.subscriptions[key] = [{value, callback}]; this.subscriptions[key] = [{value, callback}];
@ -131,7 +131,7 @@ class ProcessLauncher {
if (this.subscriptions[key]) { if (this.subscriptions[key]) {
this.subscriptions[key].filter((val, index) => { this.subscriptions[key].filter((val, index) => {
if (val.value === value) { 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 * Unsubscribes from all subscriptions
* @return {void} * @return {void}
*/ */
unsubsribeToAll() { unsubscribeToAll() {
this.subscriptions = {}; this.subscriptions = {};
} }

139
test/processLauncher.js Normal file
View File

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