From a839c86d83c866233f9a0098bbd92e2e0c55ee53 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Fri, 2 Jun 2017 21:59:31 +0530 Subject: [PATCH] Electron-66 (Unit tests) (#120) * Electron-66 - Added more unit tests for config, getRegistry & throttle * Electron-66 - Added some more unit tests and enabled activity detection unit test --- js/activityDetection/activityDetection.js | 1 + js/config.js | 7 +- tests/activityDetection.test.js | 51 ++++++++- tests/config.test.js | 122 +++++++++++++++++++++- tests/log.test.js | 16 ++- tests/utils/getRegistry.test.js | 70 +++++++++++++ tests/utils/throttle.test.js | 20 ++++ 7 files changed, 277 insertions(+), 10 deletions(-) create mode 100644 tests/utils/getRegistry.test.js diff --git a/js/activityDetection/activityDetection.js b/js/activityDetection/activityDetection.js index fc61534d..7b9b8283 100644 --- a/js/activityDetection/activityDetection.js +++ b/js/activityDetection/activityDetection.js @@ -95,5 +95,6 @@ module.exports = { send: send, setActivityWindow: setActivityWindow, activityDetection: activityDetection, + monitorUserActivity: monitorUserActivity, // Exporting this for unit test initiateActivityDetection: initiateActivityDetection }; diff --git a/js/config.js b/js/config.js index 89554d52..7c92c87d 100644 --- a/js/config.js +++ b/js/config.js @@ -164,4 +164,9 @@ function saveUserConfig(fieldName, newValue, oldConfig) { }); } -module.exports = { getConfigField, updateConfigField, configFileName }; +module.exports = { + getConfigField, + updateConfigField, + configFileName, + saveUserConfig // Exporting this for unit tests +}; diff --git a/tests/activityDetection.test.js b/tests/activityDetection.test.js index cc8fe1f4..1225d3b1 100644 --- a/tests/activityDetection.test.js +++ b/tests/activityDetection.test.js @@ -1,10 +1,30 @@ -// const activityDetection = require('../js/activityDetection/activityDetection.js'); -// const electron = require('./__mocks__/electron'); +const electron = require('./__mocks__/electron'); +const childProcess = require('child_process'); -xdescribe('Tests for Activity Detection', function() { +let activityDetection; - beforeAll(function () { - activityDetection.setActivityWindow(120000, electron.ipcRenderer); +describe('Tests for Activity Detection', function() { + + var originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; + jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000; + + beforeAll(function (done) { + childProcess.exec('npm rebuild --runtime=electron --target=1.2.2 --disturl=https://atom.io/download/atom-shell --build-from-source', function (err) { + activityDetection = require('../js/activityDetection/activityDetection.js'); + activityDetection.setActivityWindow(120000, electron.ipcRenderer); + done(); + }); + }); + + beforeEach(function () { + jest.clearAllMocks() + }); + + afterAll(function (done) { + childProcess.exec('npm run rebuild', function (err, stdout) { + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; + done(); + }); }); it('should get user activity where user is not idle', function() { @@ -37,4 +57,25 @@ xdescribe('Tests for Activity Detection', function() { }); + it('should monitor user activity', function () { + activityDetection.setActivityWindow(500000, electron.ipcRenderer); + const spy = jest.spyOn(activityDetection, 'monitorUserActivity'); + + expect(spy).not.toBeCalled(); + + activityDetection.monitorUserActivity(); + expect(spy).toHaveBeenCalled(); + + }); + + it('should not send activity event as data is undefined', function () { + const spy = jest.spyOn(activityDetection, 'send'); + + expect(spy).not.toBeCalled(); + + activityDetection.send(undefined); + expect(spy).toHaveBeenCalledWith(undefined); + + }); + }); diff --git a/tests/config.test.js b/tests/config.test.js index 17caa01c..e4a7026f 100644 --- a/tests/config.test.js +++ b/tests/config.test.js @@ -1,4 +1,4 @@ -const { getConfigField, updateConfigField, configFileName } = require('../js/config'); +const { getConfigField, updateConfigField, configFileName, saveUserConfig } = require('../js/config'); const fs = require('fs'); const path = require('path'); const os = require('os'); @@ -135,6 +135,40 @@ describe('getConfigField tests', function() { expect(url).toBe('something'); }); }); + + it('should fail when global config path is invalid', function() { + + var globalConfig = { + url: 'something-else' + }; + createTempGlobalConfig(globalConfig); + + let correctConfigDir = globalConfigDir; + globalConfigDir = '//'; + + return getConfigField('url').catch(function(err) { + globalConfigDir = correctConfigDir; + expect(err).toBeTruthy(); + }); + + }); + + it('should fail when user config path is invalid', function() { + + var userConfig = { + url: 'something' + }; + createTempUserConfig(userConfig); + + let correctConfigDir = userConfigDir; + userConfigDir = '//'; + + return getConfigField('url').catch(function(err) { + userConfigDir = correctConfigDir; + expect(err).toBeTruthy(); + }); + + }); }); describe('updateConfigField tests', function() { @@ -142,7 +176,7 @@ describe('getConfigField tests', function() { it('should succeed and overwrite existing field', function() { var userConfig = { url: 'something' - } + }; createTempUserConfig(userConfig); @@ -157,7 +191,7 @@ describe('getConfigField tests', function() { it('should succeed and add new field', function() { var userConfig = { url: 'something' - } + }; createTempUserConfig(userConfig); @@ -169,5 +203,87 @@ describe('getConfigField tests', function() { }); }); }); + + it('should fail to update if invalid field name', function() { + + var userConfig = { + url: 'something' + }; + + createTempUserConfig(userConfig); + + return updateConfigField('', 'hello word').catch(function (err) { + expect(err).toBe('can not save config, invalid input'); + }); + + }); + + it('should throw error if path is not defined', function() { + + userConfigDir = null; + + return updateConfigField('url2', 'hello world') + .catch(function (err) { + expect(err).toThrow(err); + }); + + }); + + it('should throw error if fieldName is not defined', function() { + + var userConfig = { + url: 'something' + }; + + createTempUserConfig(userConfig); + + return saveUserConfig(undefined, 'something', 'oldConfig') + .catch(function (reject) { + expect(reject).toBe('can not save config, invalid input'); + }); + }); + + it('should throw error if config is not defined', function() { + + var userConfig = { + url: 'something' + }; + + createTempUserConfig(userConfig); + + return saveUserConfig('url2', 'something', undefined) + .catch(function (reject) { + expect(reject).toBe('can not save config, invalid input'); + }); + }); + + it('should throw error if config file is not correct', function() { + + var userConfig = { + url: 'something' + }; + createTempUserConfig(userConfig); + + let correctConfigDir = userConfigDir; + userConfigDir = '//'; + + return saveUserConfig('url2', 'hello world', 'url') + .catch(function(err) { + userConfigDir = correctConfigDir; + expect(err).toBeTruthy(); + }); + + }); + + it('should throw error if path is not defined for saveUserConfig()', function() { + + userConfigDir = null; + + return saveUserConfig('url2', 'hello world') + .catch(function (err) { + expect(err).toThrow(err); + }); + }); + }); }); diff --git a/tests/log.test.js b/tests/log.test.js index e6dea72e..62875ae2 100644 --- a/tests/log.test.js +++ b/tests/log.test.js @@ -52,5 +52,19 @@ describe('logger tests', function() { let queue = log.logQueue; expect(queue.length).toBe(100); - }) + }); + + it('should not send the logs', function() { + let mockWin = { + send: jest.fn() + }; + + log.setLogWindow(mockWin); + log.send(); + + let queue = log.logQueue; + + expect(mockWin.send).toHaveBeenCalledWith("log", {"msgs": []}); + expect(queue.length).toBe(0); + }); }); diff --git a/tests/utils/getRegistry.test.js b/tests/utils/getRegistry.test.js new file mode 100644 index 00000000..97e382d5 --- /dev/null +++ b/tests/utils/getRegistry.test.js @@ -0,0 +1,70 @@ +const getRegistry = require('../../js/utils/getRegistry.js'); + +const { isMac } = require('../../js/utils/misc.js'); + +describe('Tests for getRegistry', function() { + + describe('Should not get registry for mac', function() { + + if (isMac){ + + it('should fail to get path for mac', function(done) { + + getRegistry('PodUrl').then(resolve).catch(reject); + + function resolve() { + // shouldn't get here + expect(true).toBe(false); + } + + function reject(err) { + expect(err).toBeTruthy(); + done(); + } + }); + + } + + }); + + describe('Should get registry for windows', function() { + + if (!isMac){ + + it('should get registry path', function(done) { + + getRegistry('PodUrl').then(resolve).catch(reject); + + function resolve(url) { + expect(url).toBe('string'); + done(); + } + + function reject(err) { + expect(err).toBeTruthy(); + done(); + } + + }); + + it('should not get the registry path', function(done) { + + getRegistry('wrongUrl').then(resolve).catch(reject); + + function resolve() { + expect(true).toBe(false) + } + + function reject(err) { + expect(err).toBeTruthy(); + expect(err).toBe('Cannot find PodUrl Registry. Using default url.'); + done(); + } + + }); + + } + + }); + +}); \ No newline at end of file diff --git a/tests/utils/throttle.test.js b/tests/utils/throttle.test.js index 719725e3..24e2a06b 100644 --- a/tests/utils/throttle.test.js +++ b/tests/utils/throttle.test.js @@ -55,6 +55,26 @@ describe('throttle tests', function() { expect(callback.mock.calls.length).toBe(2); }); + it('expect clearTimeout to be invoked', function() { + const callback = jest.fn(); + const throttledCB = throttle(1000, callback); + + expect(callback).not.toBeCalled(); + + throttledCB(); + expect(callback.mock.calls.length).toBe(1); + expect(clearTimeout.mock.calls.length).toBe(0); + + now -= 1000; + throttledCB(); + expect(callback.mock.calls.length).toBe(1); + + now += 1000; + throttledCB(); + expect(callback.mock.calls.length).toBe(1); + expect(clearTimeout.mock.calls.length).toBe(1); + }); + describe('expect to throw exception', function() { it('when calling throttle with time equal to zero', function(done) { try {