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
This commit is contained in:
Kiran Niranjan 2017-06-02 21:59:31 +05:30 committed by Lynn
parent 4a02beeb86
commit a839c86d83
7 changed files with 277 additions and 10 deletions

View File

@ -95,5 +95,6 @@ module.exports = {
send: send,
setActivityWindow: setActivityWindow,
activityDetection: activityDetection,
monitorUserActivity: monitorUserActivity, // Exporting this for unit test
initiateActivityDetection: initiateActivityDetection
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 {