diff --git a/package.json b/package.json index f6daacc..065e249 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "main": "main.js", "scripts": { "start": "electron .", + "test": "jest", "build": "webpack", "package": "electron-packager . --platform=darwin --arch=x64 --out=packaged --icon=img/out.icns --overwrite", "zip": "cd packaged/Alchemy-darwin-x64/ ; zip -r ../Alchemy.zip * ; cd .." @@ -13,6 +14,7 @@ "babel-loader": "^6.2.10", "babel-preset-es2015": "^6.18.0", "babel-preset-react": "^6.16.0", + "chai": "^3.5.0", "css-loader": "^0.26.1", "electron-packager": "^8.5.0", "eslint": "^3.13.1", @@ -21,6 +23,7 @@ "eslint-plugin-jsx-a11y": "^3.0.2", "eslint-plugin-react": "^6.9.0", "extract-text-webpack-plugin": "^1.0.1", + "jest": "^19.0.2", "node-sass": "^4.3.0", "sass-loader": "^4.1.1", "style-loader": "^0.13.1", diff --git a/src/helpers/util.js b/src/helpers/util.js index 2c46c25..9d1ad3a 100644 --- a/src/helpers/util.js +++ b/src/helpers/util.js @@ -17,13 +17,11 @@ const uniqueFiles = (files, newArray) => }) }, files) -const removeByKey = (myObj, deleteKey) => - Object.keys(myObj) - .filter(key => key !== deleteKey) - .reduce((result, current) => { - result[current] = myObj[current] - return result - }, {}) +const removeByKey = (myObj, deleteKey) => { + const newObj = Object.assign({}, myObj) + delete newObj[deleteKey] + return newObj +} module.exports = { concatFiles, diff --git a/test/util.test.js b/test/util.test.js new file mode 100644 index 0000000..1eaf52a --- /dev/null +++ b/test/util.test.js @@ -0,0 +1,99 @@ +/* global describe, it */ +/* eslint-disable prefer-arrow-callback */ + +const { expect } = require('chai') +const { concatFiles, uniqueFiles, removeByKey, replaceSpaceCharacters } = require('../src/helpers/util') + +describe('UTIL', function () { + describe('concatFiles()', function () { + it('concats files', () => { + const files = [ + '/User/test/test.png', + '/User/test/test.jpg', + ] + expect(concatFiles(files)).to.equal('test_test') + }) + it('truncates long file names', () => { + const files = [ + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + '/User/test/test.jpg', + ] + expect(concatFiles(files)).to.equal('testaaaaaa_test') + }) + it('truncates final output name', () => { + const files = [ + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + '/User/test/testaaaaaaaaaaaaaaaaaa.png', + ] + expect(concatFiles(files)).to.equal('testaaaaaa_testaaaaaa_testaaaaaa_testaaaaaa_testaa') + }) + }) + + describe('removeByKey()', function () { + it('remove key from an object', () => { + const obj = { + key1: 'here', + key2: 'there' + } + expect(removeByKey(obj, 'key1')).to.deep.equal({ + key2: 'there' + }) + }) + + it('do nothing if key doesnt exist', () => { + const obj = { + key1: 'here', + key2: 'there' + } + expect(removeByKey(obj, 'key3')).to.deep.equal({ + key1: 'here', + key2: 'there' + }) + }) + + + it('do nothing if key doesnt exist', () => { + const value = 8 + const obj = { + key1: value + } + expect(removeByKey(obj, 'key1')).to.deep.equal({}) + expect(value).to.equal(8) + }) + }) + + describe('replaceSpaceCharacters()', function () { + it('replace space characters with escaped space', () => { + const fileName = 'This is a bad file name.png' + expect(replaceSpaceCharacters(fileName)).to.equal('This\\ is\\ a\\ bad\\ file\\ name.png') + }) + }) + + describe('uniqueFiles()', function () { + it('ensures unique files by path', () => { + const files = { + key1: 'here', + key2: 'there' + } + const newFiles = [ + { + path: 'key1' + }, + { + path: 'key3' + } + ] + expect(uniqueFiles(files, newFiles)).to.deep.equal({ + key1: 'here', + key2: 'there', + key3: { + path: 'key3' + } + }) + }) + }) +})