diff --git a/README.md b/README.md index ae75ad89..234a4296 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/js/getConfig.js b/js/getConfig.js index b2d0d2db..3c756a09 100644 --- a/js/getConfig.js +++ b/js/getConfig.js @@ -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 @@ -13,7 +14,7 @@ const { isDevEnv, isMac } = require('./utils.js'); * this file is located relative to the executable - it is placed there by * the installer. this makes the file easily modifable by admin (or person who * installed app). for dev env, the file is read directly from packed asar file. - * + * * @return {Object} configuration parameters (e.g., url) */ function getConfig() { @@ -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) { diff --git a/js/utils.js b/js/utils.js index 2f29bdb9..ef6cdb8a 100644 --- a/js/utils.js +++ b/js/utils.js @@ -8,16 +8,16 @@ const isMac = (process.platform === 'darwin'); /** * Generates a guid, * http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript - * + * * @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 = { diff --git a/package.json b/package.json index 7b96e309..8ec60a75 100644 --- a/package.json +++ b/package.json @@ -13,24 +13,33 @@ "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": { - "target": "dmg", - "category": "public.app-category.business" + "target": "dmg", + "category": "public.app-category.business" }, "dmg": { - "contents": [ - { "x": 140, "y": 184 }, - { "x": 412, "y": 184, "type": "link", "path": "/Applications" } - ] + "contents": [ + { + "x": 140, + "y": 184 + }, + { + "x": 412, + "y": 184, + "type": "link", + "path": "/Applications" + } + ] }, "win": { - "target": "squirrel" + "target": "squirrel" } }, "repository": { @@ -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" diff --git a/tests/__mocks__/electron.js b/tests/__mocks__/electron.js new file mode 100644 index 00000000..30b26b45 --- /dev/null +++ b/tests/__mocks__/electron.js @@ -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() +}; diff --git a/tests/getConfig.test.js b/tests/getConfig.test.js new file mode 100644 index 00000000..aceeb92b --- /dev/null +++ b/tests/getConfig.test.js @@ -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'); + }); +}); diff --git a/tests/utils.test.js b/tests/utils.test.js new file mode 100644 index 00000000..f6a0429e --- /dev/null +++ b/tests/utils.test.js @@ -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); + }); + } + }); +});