/ test / processLauncher.js
processLauncher.js
  1  /*global describe, it, before, beforeEach*/
  2  const assert = require('assert');
  3  const sinon = require('sinon');
  4  const TestLogger = require('../lib/utils/test_logger');
  5  const path = require('path');
  6  const ProcessLauncher = require('../lib/core/processes/processLauncher');
  7  
  8  describe('ProcessWrapper', () => {
  9    let processLauncher;
 10  
 11    before(() => {
 12      sinon.stub(ProcessLauncher.prototype, '_subscribeToMessages');
 13      processLauncher = new ProcessLauncher({
 14        logger: new TestLogger({}),
 15        modulePath: path.join(__dirname, 'test.js')
 16      });
 17    });
 18  
 19    describe('on', () => {
 20  
 21      beforeEach(() => {
 22        processLauncher.subscriptions = {};
 23      });
 24  
 25      it('should create an array for the key value', function () {
 26        processLauncher.on('test', 'value', 'myCallback');
 27        assert.deepEqual(processLauncher.subscriptions, {
 28          "test": [
 29            {
 30              "callback": "myCallback",
 31              "value": "value"
 32            }
 33          ]
 34        });
 35      });
 36  
 37      it('should add another value to the key', () => {
 38        processLauncher.on('test', 'value', 'myCallback');
 39        processLauncher.on('test', 'value2', 'myCallback2');
 40        assert.deepEqual(processLauncher.subscriptions, {
 41          "test": [
 42            {
 43              "callback": "myCallback",
 44              "value": "value"
 45            },
 46            {
 47              "callback": "myCallback2",
 48              "value": "value2"
 49            }
 50          ]
 51        });
 52      });
 53    });
 54  
 55    describe('unsubscribeTo', () => {
 56      it('should remove the value for the key', () => {
 57        processLauncher.subscriptions = {
 58          "test": [
 59            {
 60              "callback": "myCallback",
 61              "value": "value"
 62            },
 63            {
 64              "callback": "myCallback2",
 65              "value": "value2"
 66            }
 67          ]
 68        };
 69  
 70        processLauncher.unsubscribeTo('test', 'value2');
 71        assert.deepEqual(processLauncher.subscriptions, {
 72          "test": [
 73            {
 74              "callback": "myCallback",
 75              "value": "value"
 76            }
 77          ]
 78        });
 79      });
 80  
 81      it('should remove the whole key', () => {
 82        processLauncher.subscriptions = {
 83          "test": [
 84            {
 85              "callback": "myCallback",
 86              "value": "value"
 87            }
 88          ]
 89        };
 90  
 91        processLauncher.unsubscribeTo('test');
 92        assert.deepEqual(processLauncher.subscriptions, {test: []});
 93      });
 94    });
 95  
 96    describe('unsubscribeToAll', () => {
 97      it('clears every subscriptions', () => {
 98        processLauncher.subscriptions = {
 99          "test": [
100            {
101              "callback": "myCallback",
102              "value": "value"
103            }
104          ]
105        };
106  
107        processLauncher.unsubscribeToAll();
108        assert.deepEqual(processLauncher.subscriptions, {});
109      });
110    });
111  
112    describe('_checkSubscriptions', function () {
113      it('should not do anything if not in subscription', function () {
114        const callback = sinon.stub();
115        processLauncher.subscriptions = {
116          "test": [
117            {
118              "callback": callback,
119              "value": "value"
120            }
121          ]
122        };
123        processLauncher._checkSubscriptions({does: 'nothing', for: 'real'});
124        assert.strictEqual(callback.callCount, 0);
125      });
126  
127      it('should call the callback', function () {
128        const callback = sinon.stub();
129        processLauncher.subscriptions = {
130          "test": [
131            {
132              "callback": callback,
133              "value": "value"
134            }
135          ]
136        };
137        processLauncher._checkSubscriptions({test: 'value'});
138        assert.strictEqual(callback.callCount, 1);
139      });
140  
141      it('should call the callback and remove the sub', function () {
142        const callback = sinon.stub();
143        processLauncher.subscriptions = {
144          "test": [
145            {
146              "callback": callback,
147              "value": "value",
148              "once": true
149            }
150          ]
151        };
152        processLauncher._checkSubscriptions({test: 'value'});
153        assert.strictEqual(callback.callCount, 1);
154        assert.deepEqual(processLauncher.subscriptions, {test: []});
155      });
156  
157      it('should call the callback twice', function () {
158        const callback = sinon.stub();
159        processLauncher.subscriptions = {
160          "test": [
161            {
162              "callback": callback,
163              "value": "value"
164            },
165            {
166              "callback": callback,
167              "value": "value"
168            }
169          ]
170        };
171        processLauncher._checkSubscriptions({test: 'value'});
172        assert.strictEqual(callback.callCount, 2);
173      });
174    });
175  });