diff --git a/package.json b/package.json index 8d3185e..4c25766 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "scripts": { "compile": "babel --presets es2015,stage-0 -d lib/ src/", "prepublish": "npm run compile", - "test": "node_modules/.bin/_mocha test", - "test2": "node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha -- test/*.js", + "pretest": "npm run compile", + "test2": "node_modules/.bin/_mocha test", + "test": "node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha -- test/*.js", "coverage": "istanbul cover _mocha -- --ui bdd -R spec -t 5000;open ./coverage/lcov-report/index.html", "cover": "node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha", "coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls" @@ -38,14 +39,18 @@ "devDependencies": { "babel-cli": "^6.18.0", "babel-core": "^6.18.2", + "babel-istanbul": "^0.11.0", "babel-polyfill": "^6.16.0", "babel-preset-es2015": "^6.18.0", "babel-preset-react": "^6.16.0", "babel-preset-stage-0": "^6.16.0", "coveralls": "^2.11.15", "enzyme": "^2.6.0", + "expect": "^1.20.2", + "mocha": "^3.2.0", "mocha-lcov-reporter": "^1.2.0", - "react": "^15.4.1" + "react": "^15.4.1", + "sinon": "^1.17.6" }, "dependencies": { "x-object": "^1.0.10" diff --git a/src/core.js b/src/core.js index 77f6c8e..f880beb 100644 --- a/src/core.js +++ b/src/core.js @@ -13,7 +13,7 @@ export const jsonsHeaders = ((array) => Array.from( export const jsons2arrays = (jsons, headers) => { headers = headers || jsonsHeaders(jsons); - return [headers, ...array.map((object) => + return [headers, ...jsons.map((object) => headers.map((header) => object[header] ? object[header] : ''))] }; diff --git a/test/coreSpec.js b/test/coreSpec.js new file mode 100644 index 0000000..fc04c63 --- /dev/null +++ b/test/coreSpec.js @@ -0,0 +1,181 @@ +import expect from 'expect'; + +import { + isJsons, + isArrays, + jsonsHeaders, + jsons2arrays, + arrays2csv, + jsons2csv, + string2csv, + toCSV, + buildURI +} from '../lib/core'; + +describe(`core::isJsons`, () => { + it(`returns true if all items of array are literal objects`, () => { + const target = [{}, {}, {}, {}]; + expect(isJsons(target)).toBeTruthy(); + }); + it(`returns false if one of array items is not literal object`, () => { + let target = ["", {}, {}, {}]; + expect(isJsons(target)).toBeFalsy(); + target = [{}, + [], {}, {} + ]; + expect(isJsons(target)).toBeFalsy(); + }); + +}); + +describe(`core::isArrays`, () => { + it(`retruns true if all array items are arrays too`, () => { + const target = [ + [], + [], + [], + [] + ]; + expect(isArrays(target)).toBeTruthy(); + }); + it(`retruns false if one of array items is not array`, () => { + let target = [{}, + [], + [], + [] + ]; + expect(isArrays(target)).toBeFalsy(); + target = [ + [], new Set([]), [], + [] + ]; + expect(isArrays(target)).toBeFalsy(); + target = [ + [], '[]', [], + [] + ]; + expect(isArrays(target)).toBeFalsy(); + }); + +}); + + +describe(`core::jsonsHeaders`, () => { + let fixtures; + beforeEach(() => { + fixtures = [{ + maths: 90, + phy: 80 + }, { + maths: 50, + sport: 97, + ch: 66 + }, { + ch: 77, + sport: 99 + }] + }); + + it(`returns union of keys of all array items `, () => { + let actual = jsonsHeaders(fixtures); + expect(actual).toEqual([`maths`, `phy`, 'sport', 'ch']) + }), + + it(`does not duplicate keys on the array`, () => { + let actual = jsonsHeaders(fixtures); + let keysOfAllItems = fixtures.map((object) => Object.keys(object)) + .reduce((a, b) => [...a, ...b], []); + expect(actual.length).toBeLessThanOrEqualTo(keysOfAllItems.length); + expect(actual.length).toEqual(new Set(keysOfAllItems).size); + + }) + +}); +describe(`core::jsons2arrays`, () => { + let fixtures; + beforeEach(() => { + fixtures = [{ + maths: '90' + }, { + sport: '97' + }, { + maths: '77', + sport: '99' + }] + }); + it(`converts an Array of literal objects to Array of arrays`, () => { + const actual = jsons2arrays(fixtures); + const expected = [ + ['maths', 'sport'], + ['90', ''], + ['', '97'], + ['77', '99'] + ]; + expect(actual).toEqual(expected); + }); + + it(`converts to Array of arrays following the order of headers`, () => { + const actual = jsons2arrays(fixtures, ['sport', 'maths']); + const expected = [ + ['maths', 'sport'].reverse(), ['90', ''].reverse(), ['', '97'].reverse(), ['77', '99'].reverse() + ]; + expect(actual).toEqual(expected); + }); + it(`accepts any size of headers list`, () => { + const headers = ['maths', 'sport', 'phy', 'ch']; + const actual = jsons2arrays(fixtures, headers); + const expected = [ + headers, ['90', '', '', ''], + ['', '97', '', ''], + ['77', '99', '', ''] + ]; + expect(actual).toEqual(expected); + }); + +}); + +describe(`core::arrays2csv`, () => { + let fixtures; + beforeEach(() => { + fixtures = [ + [`a`, `b`], + [`c`, `d`] + ]; + + }); + it(`converts Array of arrays to string in CSV format`, () => { + const actual = arrays2csv(fixtures); + expect(actual).toBeA('string'); + expect(actual.split(`\n`).join(`|`)).toEqual(`a,b|c,d`); + }); + + it(`renders CSV headers whenever it was given `, () => { + const headers = [`X`, `Y`]; + const firstLineOfCSV = arrays2csv(fixtures, headers).split(`\n`)[0]; + expect(firstLineOfCSV).toEqual(headers.join(`,`)); + }); +}); + + +describe(`core::jsons2csv`, () => { + let fixtures; + beforeEach(() => { + fixtures = [{ + X: '88', + Y: '97' + }, { + X: '77', + Y: '99' + }] + }); + + it(`converts Array of literal objects to string in CSV format including headers`, () => { + + const actual = jsons2csv(fixtures); + const expectedHeaders = ['X', 'Y']; + const expected =`${expectedHeaders.join(`,`)}|88,97|77,99`; + expect(actual).toBeA('string'); + expect(actual.split(`\n`).join(`|`)).toEqual(expected); + }); + +});