add unt test framework (#23)

This commit is contained in:
Lynn 2017-02-26 17:58:48 -08:00 committed by GitHub
parent 9554c2d79a
commit d4ef876828
7 changed files with 94 additions and 20 deletions

View File

@ -31,5 +31,11 @@ In order to achieve those goals Symphony is participating and working in close c
- To change the start url (i.e., pod url), edit config/Symphony.config and change 'url' variable. if no protocol provided, then https will be added.
- The installer will include file config/Symphony.config next to executable. Changes in this file will effect all users.
## Tests and Code Coverage
- jest framework is used to run tests: http://facebook.github.io/jest/
- to run tests and get code coverage report: npm test
- code coverage reports are placed in dir: converage
- tests are located in dir: tests
## Misc notes
If desiring to run against server without proper cert use cmd line option: --ignore-certificate-errors

View File

@ -4,7 +4,8 @@ const electron = require('electron');
const app = electron.app;
const path = require('path');
const fs = require('fs');
const { isDevEnv, isMac } = require('./utils.js');
const isDevEnv = require('./utils.js').isDevEnv;
const isMac = require('./utils.js').isMac;
/**
* reads global configuration file: config/Symphony.config. this file is
@ -30,7 +31,7 @@ function getConfig() {
let execPath = path.dirname(app.getPath('exe'));
// for mac exec is stored in subdir, for linux/windows config
// dir is in the same location.
configPath = path.join(execPath, isMac ? '..' : '', configFile)
configPath = path.join(execPath, isMac ? '..' : '', configFile);
}
fs.readFile(configPath, 'utf8', function (err, data) {

View File

@ -12,12 +12,12 @@ const isMac = (process.platform === 'darwin');
* @return {String} guid value in string
*/
function getGuid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
return guid;
}
module.exports = {

View File

@ -13,10 +13,11 @@
"dist-win": "build --win --x64",
"dist-win-x86": "build --win --ia32",
"unpacked-win": "build --win --x64 --dir",
"unpacked-win-x86": "build --win --ia32 --dir"
"unpacked-win-x86": "build --win --ia32 --dir",
"test": "jest --coverage"
},
"build": {
"files" : "!installer/*",
"files": [ "!installer/*", "!tests/*" ],
"extraFiles": "config/Symphony.config",
"appId": "symphony-electron-desktop",
"mac": {
@ -25,8 +26,16 @@
},
"dmg": {
"contents": [
{ "x": 140, "y": 184 },
{ "x": 412, "y": 184, "type": "link", "path": "/Applications" }
{
"x": 140,
"y": 184
},
{
"x": 412,
"y": 184,
"type": "link",
"path": "/Applications"
}
]
},
"win": {
@ -48,8 +57,9 @@
"devDependencies": {
"electron": "1.5.1",
"electron-builder": "^13.9.0",
"electron-builder-squirrel-windows": "^12.3.0",
"electron-packager": "^8.5.2",
"electron-builder-squirrel-windows": "^12.3.0"
"jest": "^19.0.2"
},
"dependencies": {
"electron-squirrel-startup": "^1.0.0"

View File

@ -0,0 +1,17 @@
const path = require('path');
function pathToConfigDir() {
return path.join(__dirname, '..');
}
const app = {
getAppPath: pathToConfigDir,
getPath: pathToConfigDir
}
module.exports = {
require: jest.genMockFunction(),
match: jest.genMockFunction(),
app: app,
remote: jest.genMockFunction(),
dialog: jest.genMockFunction()
};

15
tests/getConfig.test.js Normal file
View File

@ -0,0 +1,15 @@
const getConfig = require('../js/getconfig');
// mock required so getConfig reads config from correct path
jest.mock('../js/utils', function() {
return {
isDevEnv: false,
isMac: false
}
});
test('getConfig should have proper url', function() {
return getConfig(false).then(function(result) {
expect(result.url).toBe('https://foundation-dev.symphony.com');
});
});

25
tests/utils.test.js Normal file
View File

@ -0,0 +1,25 @@
const utils = require('../js/utils');
describe('guid tests', function() {
it('should have valid length', function() {
var guid = utils.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 = utils.getGuid();
var parts = guid.split('-');
parts.forEach(function(part) {
expect(/^([A-Fa-f0-9]{2})+$/.test(part)).toBe(true);
});
}
});
});