Skip to content

Commit

Permalink
Write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 committed Feb 25, 2017
1 parent 4c4c2d6 commit 187570d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .."
Expand All @@ -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",
Expand All @@ -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",
Expand Down
12 changes: 5 additions & 7 deletions src/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
99 changes: 99 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -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'
}
})
})
})
})

0 comments on commit 187570d

Please # to comment.