embark-area-51/test/processLauncher.js

176 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-05-16 18:06:34 +00:00
/*global describe, it, before, beforeEach*/
const assert = require('assert');
const sinon = require('sinon');
2018-07-11 15:38:58 +00:00
const TestLogger = require('../lib/tests/test_logger');
2018-05-16 18:06:34 +00:00
const ProcessLauncher = require('../lib/process/processLauncher');
const path = require('path');
2018-05-16 18:06:34 +00:00
describe('ProcessWrapper', () => {
let processLauncher;
before(() => {
sinon.stub(ProcessLauncher.prototype, '_subscribeToMessages');
processLauncher = new ProcessLauncher({
2018-05-23 15:31:20 +00:00
logger: new TestLogger({}),
modulePath: path.join(__dirname, 'test.js')
2018-05-16 18:06:34 +00:00
});
});
2018-05-18 18:11:29 +00:00
describe('on', () => {
2018-05-16 18:06:34 +00:00
beforeEach(() => {
processLauncher.subscriptions = {};
});
it('should create an array for the key value', function () {
2018-05-18 18:11:29 +00:00
processLauncher.on('test', 'value', 'myCallback');
2018-05-16 18:06:34 +00:00
assert.deepEqual(processLauncher.subscriptions, {
"test": [
{
"callback": "myCallback",
"value": "value"
}
]
});
});
it('should add another value to the key', () => {
2018-05-18 18:11:29 +00:00
processLauncher.on('test', 'value', 'myCallback');
processLauncher.on('test', 'value2', 'myCallback2');
2018-05-16 18:06:34 +00:00
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);
});
2018-05-18 18:11:29 +00:00
it('should call the callback and remove the sub', function () {
const callback = sinon.stub();
processLauncher.subscriptions = {
"test": [
{
"callback": callback,
"value": "value",
"once": true
}
]
};
processLauncher._checkSubscriptions({test: 'value'});
assert.strictEqual(callback.callCount, 1);
assert.deepEqual(processLauncher.subscriptions, {test: []});
});
it('should call the callback twice', function () {
const callback = sinon.stub();
processLauncher.subscriptions = {
"test": [
{
"callback": callback,
"value": "value"
},
{
"callback": callback,
"value": "value"
}
]
};
processLauncher._checkSubscriptions({test: 'value'});
assert.strictEqual(callback.callCount, 2);
});
2018-05-16 18:06:34 +00:00
});
});