mirror of
https://github.com/finos/SymphonyElectron.git
synced 2026-07-29 15:55:55 -05:00
add unit test for isInDisplayBounds and config.js (#66)
* add unit test for util: isInDisplayBounds * add tests for config.js
This commit is contained in:
@@ -8,20 +8,6 @@ function pathToConfigDir() {
|
||||
return path.join(__dirname, '/../fixtures');
|
||||
}
|
||||
|
||||
// electron app mock...
|
||||
const app = {
|
||||
getAppPath: pathToConfigDir,
|
||||
getPath: function(type) {
|
||||
if (type === 'exe') {
|
||||
return path.join(pathToConfigDir(), '/Symphony.exe');
|
||||
}
|
||||
return pathToConfigDir();
|
||||
},
|
||||
on: function() {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
|
||||
// simple ipc mocks for render and main process ipc using
|
||||
// nodes' EventEmitter
|
||||
const ipcMain = {
|
||||
@@ -62,7 +48,7 @@ const ipcRenderer = {
|
||||
module.exports = {
|
||||
require: jest.genMockFunction(),
|
||||
match: jest.genMockFunction(),
|
||||
app: app,
|
||||
app: jest.genMockFunction(),
|
||||
ipcMain: ipcMain,
|
||||
ipcRenderer: ipcRenderer,
|
||||
remote: jest.genMockFunction(),
|
||||
|
||||
+159
-4
@@ -1,4 +1,6 @@
|
||||
const { getConfigField } = require('../js/config');
|
||||
const { getConfigField, updateConfigField, configFileName } = require('../js/config');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// mock required so getConfig reads config from correct path
|
||||
jest.mock('../js/utils/misc.js', function() {
|
||||
@@ -8,8 +10,161 @@ jest.mock('../js/utils/misc.js', function() {
|
||||
}
|
||||
});
|
||||
|
||||
test('getConfig should have proper url', function() {
|
||||
return getConfigField('url').then(function(url) {
|
||||
expect(url).toBe('https://my.symphony.com');
|
||||
let globalConfigDir;
|
||||
let userConfigDir;
|
||||
|
||||
jest.mock('electron', function() {
|
||||
return {
|
||||
app: {
|
||||
getPath: mockedGetPath
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function mockedGetPath(type) {
|
||||
if (type === 'exe') {
|
||||
return globalConfigDir;
|
||||
}
|
||||
|
||||
if (type === 'userData') {
|
||||
return userConfigDir
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
describe('getConfigField tests', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
/// reset module vars between running tests.
|
||||
globalConfigDir = null;
|
||||
userConfigDir = null;
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
// clean up temp files creating during tests
|
||||
if (globalConfigDir) {
|
||||
fs.unlinkSync(path.join(globalConfigDir, configFileName));
|
||||
fs.rmdirSync(globalConfigDir);
|
||||
fs.rmdirSync(path.join(globalConfigDir, '..'));
|
||||
}
|
||||
if (userConfigDir) {
|
||||
fs.unlinkSync(path.join(userConfigDir, configFileName));
|
||||
fs.rmdirSync(userConfigDir);
|
||||
}
|
||||
});
|
||||
|
||||
function createTempConfigFile(filePath, config) {
|
||||
fs.writeFileSync(filePath, JSON.stringify(config));
|
||||
}
|
||||
|
||||
function createTempUserConfig(config) {
|
||||
userConfigDir = fs.mkdtempSync('/tmp/config-');
|
||||
return createTempConfigFile(path.join(userConfigDir, configFileName), config);
|
||||
}
|
||||
|
||||
function createTempGlobalConfig(config) {
|
||||
globalConfigDir = path.join(fs.mkdtempSync('/tmp/config-'), 'config');
|
||||
fs.mkdirSync(globalConfigDir);
|
||||
return createTempConfigFile(path.join(globalConfigDir, configFileName), config);
|
||||
}
|
||||
|
||||
function removeTempConfigFile(filePath) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
|
||||
describe('getConfigField tests', function() {
|
||||
it('should fail when field not present in either user or global config', function() {
|
||||
var userConfig = {
|
||||
url: 'something'
|
||||
}
|
||||
|
||||
createTempUserConfig(userConfig);
|
||||
|
||||
var globalConfig = {
|
||||
url: 'something-else'
|
||||
}
|
||||
|
||||
createTempGlobalConfig(globalConfig);
|
||||
|
||||
return getConfigField('noturl').catch(function(err) {
|
||||
expect(err).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
it('should succeed when field only present in user config', function() {
|
||||
var userConfig = {
|
||||
url: 'something'
|
||||
}
|
||||
|
||||
createTempUserConfig(userConfig);
|
||||
|
||||
return getConfigField('url').then(function(url) {
|
||||
expect(url).toBe('something');
|
||||
});
|
||||
});
|
||||
|
||||
it('should succeed when field only present in global config', function() {
|
||||
var globalConfig = {
|
||||
url: 'something-else'
|
||||
}
|
||||
|
||||
createTempGlobalConfig(globalConfig);
|
||||
|
||||
return getConfigField('url').then(function(url) {
|
||||
expect(url).toBe('something-else');
|
||||
});
|
||||
});
|
||||
|
||||
it('should succeed and return user config field when value is in both', function() {
|
||||
var userConfig = {
|
||||
url: 'something'
|
||||
}
|
||||
|
||||
createTempUserConfig(userConfig);
|
||||
|
||||
var globalConfig = {
|
||||
url: 'something-else'
|
||||
}
|
||||
|
||||
createTempGlobalConfig(globalConfig);
|
||||
|
||||
return getConfigField('url').then(function(url) {
|
||||
expect(url).toBe('something');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateConfigField tests', function() {
|
||||
|
||||
it('should succeed and overwrite existing field', function() {
|
||||
var userConfig = {
|
||||
url: 'something'
|
||||
}
|
||||
|
||||
createTempUserConfig(userConfig);
|
||||
|
||||
return updateConfigField('url', 'hello world')
|
||||
.then(function(newConfig) {
|
||||
expect(newConfig).toEqual({
|
||||
url: 'hello world'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should succeed and add new field', function() {
|
||||
var userConfig = {
|
||||
url: 'something'
|
||||
}
|
||||
|
||||
createTempUserConfig(userConfig);
|
||||
|
||||
return updateConfigField('url2', 'hello world')
|
||||
.then(function(newConfig) {
|
||||
expect(newConfig).toEqual({
|
||||
url: 'something',
|
||||
url2: 'hello world'
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
-3
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"url": "https://my.symphony.com"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
const getGuid = require('../../js/utils/getGuid.js');
|
||||
|
||||
describe('guid tests', function() {
|
||||
it('should have valid length', function() {
|
||||
var guid = getGuid();
|
||||
expect(guid.length).toBe(36);
|
||||
var parts = guid.split('-');
|
||||
expect(parts.length).toBe(5);
|
||||
expect(parts[0].length).toBe(8);
|
||||
expect(parts[1].length).toBe(4);
|
||||
expect(parts[2].length).toBe(4);
|
||||
expect(parts[3].length).toBe(4);
|
||||
expect(parts[4].length).toBe(12);
|
||||
});
|
||||
|
||||
it('should only contains hex chars', function() {
|
||||
for(var i = 0; i < 100; i++) {
|
||||
var guid = getGuid();
|
||||
var parts = guid.split('-');
|
||||
parts.forEach(function(part) {
|
||||
expect(/^([A-Fa-f0-9]{2})+$/.test(part)).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
const isInDisplayBounds = require('../../js/utils/isInDisplayBounds.js');
|
||||
|
||||
let displays;
|
||||
|
||||
jest.mock('electron', function() {
|
||||
return {
|
||||
screen: {
|
||||
getAllDisplays: mockedGetAllDisplays
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function mockedGetAllDisplays() {
|
||||
return displays;
|
||||
}
|
||||
|
||||
describe('isInDisplayBounds should', function() {
|
||||
it('return false when given no rect', function() {
|
||||
expect(isInDisplayBounds(null)).toBe(false);
|
||||
});
|
||||
|
||||
function createMockDisplay(newDisplays) {
|
||||
displays = newDisplays;
|
||||
}
|
||||
|
||||
it('return true when rect in display bounds', function() {
|
||||
createMockDisplay([{
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
}]);
|
||||
|
||||
var rect = {
|
||||
x: 1,
|
||||
y: 1,
|
||||
width: 90,
|
||||
height: 90
|
||||
}
|
||||
|
||||
expect(isInDisplayBounds(rect)).toBe(true);
|
||||
});
|
||||
|
||||
it('return true when rect match exactly display', function() {
|
||||
createMockDisplay([{
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
}]);
|
||||
|
||||
var rect = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 100,
|
||||
height: 100
|
||||
}
|
||||
|
||||
expect(isInDisplayBounds(rect)).toBe(true);
|
||||
});
|
||||
|
||||
it('return true when rect match exactly display', function() {
|
||||
createMockDisplay([{
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
}]);
|
||||
|
||||
var rect = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 100,
|
||||
height: 100
|
||||
}
|
||||
|
||||
expect(isInDisplayBounds(rect)).toBe(true);
|
||||
});
|
||||
|
||||
it('return true when rect contained in at least one display', function() {
|
||||
|
||||
let display1 = {
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
};
|
||||
let display2 = {
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 100,
|
||||
y: 0
|
||||
}
|
||||
}
|
||||
createMockDisplay([ display1, display2 ]);
|
||||
|
||||
var rect = {
|
||||
x: 110,
|
||||
y: 0,
|
||||
width: 50,
|
||||
height: 50
|
||||
}
|
||||
|
||||
expect(isInDisplayBounds(rect)).toBe(true);
|
||||
});
|
||||
|
||||
it('return false when rect is not in display bounds', function() {
|
||||
createMockDisplay([{
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
}]);
|
||||
|
||||
var rect = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 100,
|
||||
height: 101
|
||||
}
|
||||
|
||||
expect(isInDisplayBounds(rect)).toBe(false);
|
||||
});
|
||||
|
||||
it('return false when rect spans two displays', function() {
|
||||
let display1 = {
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
};
|
||||
let display2 = {
|
||||
workArea: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
x: 100,
|
||||
y: 0
|
||||
}
|
||||
}
|
||||
createMockDisplay([ display1, display2 ]);
|
||||
|
||||
var rect = {
|
||||
x: 50,
|
||||
y: 50,
|
||||
width: 75,
|
||||
height: 25
|
||||
}
|
||||
|
||||
expect(isInDisplayBounds(rect)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,29 +1,4 @@
|
||||
const getGuid = require('../js/utils/getGuid.js');
|
||||
const throttle = require('../js/utils/throttle.js');
|
||||
|
||||
describe('guid tests', function() {
|
||||
it('should have valid length', function() {
|
||||
var guid = getGuid();
|
||||
expect(guid.length).toBe(36);
|
||||
var parts = guid.split('-');
|
||||
expect(parts.length).toBe(5);
|
||||
expect(parts[0].length).toBe(8);
|
||||
expect(parts[1].length).toBe(4);
|
||||
expect(parts[2].length).toBe(4);
|
||||
expect(parts[3].length).toBe(4);
|
||||
expect(parts[4].length).toBe(12);
|
||||
});
|
||||
|
||||
it('should only contains hex chars', function() {
|
||||
for(var i = 0; i < 100; i++) {
|
||||
var guid = getGuid();
|
||||
var parts = guid.split('-');
|
||||
parts.forEach(function(part) {
|
||||
expect(/^([A-Fa-f0-9]{2})+$/.test(part)).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
const throttle = require('../../js/utils/throttle.js');
|
||||
|
||||
describe('throttle tests', function() {
|
||||
var now, origNow;
|
||||
Reference in New Issue
Block a user