Skip to content

Commit

Permalink
[tests] move to jest instead of mocha and update npm scripts. add tes…
Browse files Browse the repository at this point in the history
…ts for xy-chart chartUtils.
  • Loading branch information
williaster committed May 26, 2017
1 parent 7661f1c commit 6078b3b
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
"jsx-a11y",
"import"
],
"rules": {},
"rules": {
"react/jsx-filename-extension": 0,
},
"env": {
"browser": true,
"node": true,
"jest": true,
},
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ _gh-pages
node_modules
*.log
/.eslintcache
coverage
25 changes: 20 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"lint": "eslint --ignore-path=.eslintignore --ext .js,.jsx ./packages/",
"publish": "npm run lint && lerna run test && lerna publish && lerna run gh-pages",
"test": "lerna run test"
"test": "lerna exec npm install && jest"
},
"repository": "https://github.com/williaster/data-ui.git",
"keywords": [
Expand All @@ -21,24 +21,39 @@
"author": "Chris Williams <[email protected]>",
"license": "MIT",
"devDependencies": {
"babel-jest": "^20.0.3",
"babel-polyfill": "^6.23.0",
"babel-register": "^6.24.1",
"chai": "^3.5.0",
"chai-enzyme": "^0.5.2",
"enzyme": "^2.7.0",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^20.0.3",
"lerna": "^2.0.0-rc.2",
"mocha": "^3.2.0",
"react": "~15.4.2",
"react-addons-test-utils": "^15.5.1",
"react-dom": "~15.4.2"
},
"peerDependencies": {
"lerna": "2.0.0-rc.2",
"react": "~15.4.2",
"react-dom": "~15.4.2"
},
"jest": {
"projects": [
"<rootDir>/packages/*"
],
"collectCoverage": true,
"coverageDirectory": "<rootDir>/coverage",
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"coverageReporters": [
"text",
"lcov"
]
}
}


6 changes: 4 additions & 2 deletions packages/data-table/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"build": "webpack -p --env build && npm run copy-styles",
"copy-styles": "cp ./node_modules/react-virtualized/styles.css ./build/styles.css",
"dev": "webpack --progress --colors --watch --env dev",
"mocha": "mocha --opts ./test/mocha.opts",
"test": "npm run mocha ./test"
"test": "jest --colors --verbose --coverage"
},
"repository": "https://github.com/williaster/data-ui",
"keywords": [
Expand All @@ -26,11 +25,14 @@
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-jest": "^20.0.3",
"babel-loader": "^6.4.1",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-preset-airbnb": "^2.2.3",
"css-loader": "^0.28.0",
"jest": "^20.0.3",
"react": "~15.4.2",
"react-addons-test-utils": "^15.5.1",
"react-dom": "~15.4.2",
"webpack": "^2.4.1"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/data-table/test/.eslintrc

This file was deleted.

7 changes: 7 additions & 0 deletions packages/data-table/test/Table.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Table from '../src/components/Table';

describe('<Table />', () => {
test('it should be defined', () => {
expect(Table).toBeDefined();
});
});
10 changes: 0 additions & 10 deletions packages/data-table/test/Table_test.jsx

This file was deleted.

3 changes: 0 additions & 3 deletions packages/data-table/test/mocha.opts

This file was deleted.

6 changes: 5 additions & 1 deletion packages/xy-chart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "webpack -p --env build",
"dev": "webpack --progress --colors --watch --env dev",
"test": "echo \"Error: no test specified\" && exit 0"
"test": "jest --colors --verbose --coverage"
},
"repository": "https://github.com/williaster/data-ui",
"keywords": [
Expand Down Expand Up @@ -34,12 +34,16 @@
"prop-types": "^15.5.10"
},
"devDependencies": {
"babel-jest": "^20.0.3",
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-preset-airbnb": "^2.2.3",
"enzyme": "^2.8.2",
"jest": "^20.0.3",
"react": "~15.4.2",
"react-dom": "~15.4.2",
"react-addons-test-utils": "^15.5.1",
"webpack": "^2.4.1"
},
"peerDependencies": {
Expand Down
16 changes: 9 additions & 7 deletions packages/xy-chart/src/utils/chartUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ export function collectDataFromChildSeries(children) {
const dataByIndex = {};
const dataBySeriesType = {};
Children.forEach(children, (Child, i) => {
const name = componentName(Child);
const { data } = Child.props;
if (data && isSeries(name)) {
dataByIndex[i] = data;
allData = allData.concat(data);
dataBySeriesType[name] = (dataBySeriesType[name] || []).concat(data);
if (Child && Child.props && Child.props.data) {
const name = componentName(Child);
const { data } = Child.props;
if (data && isSeries(name)) {
dataByIndex[i] = data;
allData = allData.concat(data);
dataBySeriesType[name] = (dataBySeriesType[name] || []).concat(data);
}
}
});
return { dataByIndex, allData, dataBySeriesType };
Expand All @@ -73,7 +75,7 @@ export function getScaleForAccessor({
...rest
}) {
let domain;
if (type === 'band') {
if (type === 'band' || type === 'ordinal') {
domain = allData.map(accessor);
}
if (type === 'linear' || type === 'time') {
Expand Down
7 changes: 7 additions & 0 deletions packages/xy-chart/test/XAxis.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { XAxis } from '../src/';

describe('<XAxis />', () => {
test('it should be defined', () => {
expect(XAxis).toBeDefined();
});
});
133 changes: 133 additions & 0 deletions packages/xy-chart/test/chartUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import React from 'react';
import { BarSeries, LineSeries } from '../src';

import {
callOrValue,
getScaleForAccessor,
collectDataFromChildSeries,
} from '../src/utils/chartUtils';

describe('collectDataFromChildSeries', () => {
const dummyProps = { xScale: () => {}, yScale: () => {}, label: 'bogus', barWidth: 0 };
const barData = [{ x: 'bar', y: 123 }];
const lineData = [{ x: 'line', y: 123 }];

const children = [
<div />,
<BarSeries data={barData} {...dummyProps} />,
<LineSeries data={lineData} {...dummyProps} />,
<BarSeries data={barData} {...dummyProps} />,
null,
];

test('should ignore non-series children', () => {
expect(
collectDataFromChildSeries([<span data={[]} />, <div />]).allData,
).toEqual([]);
});

const output = collectDataFromChildSeries(children);

test('should concatenate all data', () => {
expect(output.allData).toEqual([...barData, ...lineData, ...barData]);
});

test('should collect data by Series type', () => {
expect(output.dataBySeriesType).toEqual({
BarSeries: [...barData, ...barData],
LineSeries: [...lineData],
});
});

test('should collect data by child index', () => {
expect(output.dataByIndex).toEqual({
1: barData,
2: lineData,
3: barData,
});
});
});

describe('callOrValue', () => {
test('should return non-functions', () => {
expect(callOrValue(123)).toEqual(123);
expect(callOrValue('123')).toEqual('123');
expect(callOrValue(['hello'])).toEqual(['hello']);
});

test('should call a function', () => {
expect(callOrValue(() => 'abc')).toEqual('abc');
});

test('should pass args to functions', () => {
expect(callOrValue((a, b, c) => `${a}${b}${c}`, 'x', 'y')).toEqual('xyundefined');
});
});

describe('getScaleForAccessor', () => {
const allData = [
{ date: '2016-01-05', dirtyNum: undefined, num: 124, cat: 'a' },
{ date: '2017-01-05', dirtyNum: -15, num: 500, cat: 'b' },
{ date: '2018-01-05', dirtyNum: 7, num: 50, cat: 'c' },
{ date: '2019-01-05', dirtyNum: null, num: 501, cat: 'z' },
];

test('should compute date domains', () => {
expect(getScaleForAccessor({
allData,
accessor: d => new Date(d.date),
type: 'time',
range: [0, 100],
}).domain()).toEqual([
new Date(allData[0].date),
new Date(allData[allData.length - 1].date),
]);
});

test('should compute date strings domains', () => {
expect(getScaleForAccessor({
allData,
accessor: d => d.date,
type: 'band',
range: [0, 100],
}).domain()).toEqual(['2016-01-05', '2017-01-05', '2018-01-05', '2019-01-05']);
});

test('should compute categorical domains', () => {
expect(getScaleForAccessor({
allData,
accessor: d => d.cat,
type: 'band',
range: [0, 100],
}).domain()).toEqual(['a', 'b', 'c', 'z']);
});

test('should compute numeric domains including zero', () => {
expect(getScaleForAccessor({
allData,
accessor: d => d.num,
type: 'linear',
range: [0, 100],
}).domain()).toEqual([0, 501]);
});

test('should compute numeric domains excluding zero', () => {
expect(getScaleForAccessor({
allData,
accessor: d => d.num,
type: 'linear',
range: [0, 100],
includeZero: false,
}).domain()).toEqual([50, 501]);
});

test('should compute numeric domains with missing values', () => {
expect(getScaleForAccessor({
allData,
accessor: d => d.dirtyNum,
type: 'linear',
range: [0, 100],
includeZero: false,
}).domain()).toEqual([-15, 7]);
});
});

0 comments on commit 6078b3b

Please sign in to comment.