Skip to content

Commit

Permalink
Add more tests (spotify#83)
Browse files Browse the repository at this point in the history
* Add YTicks spec

* XGrid spec

* Add XGrid spec static test

* YGrid spec

* XYPlot spec

* Separate test dom and lint

* Lint browser tests

* AreaChart spec

* LineChart spec
  • Loading branch information
Kris Salvador authored Jun 12, 2018
1 parent 8565c13 commit b5c9a6d
Show file tree
Hide file tree
Showing 15 changed files with 641 additions and 83 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"lint": "eslint ./src",
"lint-fix": "eslint ./src ./tests/jsdom/spec --ext .js --fix",
"lint-fix": "eslint ./src ./tests --ext .js --fix",
"prepublish": "npm run build",
"build": "npm run build-lib && npm run build-css && npm run docs",
"dev": "webpack-dev-server --config webpack.config.base.js",
Expand All @@ -23,13 +23,15 @@
"make-docs": "node scripts/makeDocs.js",
"clean": "node scripts/clean.js",
"serve": "python -m SimpleHTTPServer",
"test": "npm run test-jsdom-and-lint",
"test": "npm run test-jsdom && npm run test-lint",
"test-files":
"env NODE_PATH=$NODE_PATH:$PWD/src BABEL_ENV=production mocha --compilers js:babel-register --require tests/jsdom/setup.js --recursive",
"test-browser":
"webpack-dev-server --config tests/browser/webpack.config.test.js",
"test-jsdom-and-lint":
"env NODE_PATH=$NODE_PATH:$PWD/src BABEL_ENV=production mocha --compilers js:babel-register --require tests/jsdom/setup.js --recursive tests/jsdom/spec tests/eslint.spec.js",
"test-jsdom":
"env NODE_PATH=$NODE_PATH:$PWD/src BABEL_ENV=production mocha --compilers js:babel-register --require tests/jsdom/setup.js --recursive tests/jsdom/spec",
"test-lint":
"env NODE_PATH=$NODE_PATH:$PWD/src BABEL_ENV=production mocha --compilers js:babel-register --require tests/jsdom/setup.js --recursive tests/eslint.spec.js",
"test-watch":
"env NODE_PATH=$NODE_PATH:$PWD/src BABEL_ENV=production mocha --watch --compilers js:babel-register --require tests/jsdom/setup.js --recursive tests/jsdom/spec"
},
Expand Down
8 changes: 4 additions & 4 deletions src/AreaChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,21 @@ export default class AreaChart extends React.Component {
const pathStyleBelow = pathStyleNegative || pathStyle || {};

return (
<g className={`${name} rct-area-chart--difference`}>
<g className="rct-area-chart--difference">
<clipPath id={clipAboveId}>
<path className="rct-area-chart-path" d={clipAbovePathStr} />
</clipPath>
<clipPath id={clipBelowId}>
<path className="rct-area-chart-path" d={clipBelowPathStr} />
</clipPath>
<path
className="rct-area-chart-path"
className={`rct-area-chart-path ${pathClassName}`}
d={areaPathStr}
clipPath={`url(#${clipAboveId})`}
style={pathStyleAbove}
/>
<path
className="rct-area-chart-path"
className={`rct-area-chart-path ${pathClassName}`}
d={areaPathStr}
clipPath={`url(#${clipBelowId})`}
style={pathStyleBelow}
Expand All @@ -195,7 +195,7 @@ export default class AreaChart extends React.Component {
);
} else {
return (
<g className={`${name} rct-area-chart`}>
<g className="rct-area-chart">
<path
className={`rct-area-chart-path ${pathClassName}`}
d={areaPathStr}
Expand Down
2 changes: 1 addition & 1 deletion src/YGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class YGrid extends React.Component {
const className = `rct-chart-grid-line ${lineClassName || ""}`;

return (
<g className="chart-grid-y">
<g className="rct-chart-grid-y">
{ticks.map((tick, i) => {
return (
<YLine
Expand Down
13 changes: 6 additions & 7 deletions tests/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
require('./spec/XAxis.spec');
// require('./spec/examples.spec');
require("./spec/XAxis.spec");

// some tests must be run in a browser environment
// also it can be easier to debug tests in browser thanks to chrome debugger
// place browser tests in tests/browser/spec and run `npm run test-browser`

// run mocha
(function() {
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
})();
44 changes: 24 additions & 20 deletions tests/browser/spec/XAxis.spec.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import React from 'react';
import ReactDOM from 'react-dom';
import d3 from 'd3';
import React from "react";
import ReactDOM from "react-dom";
import d3 from "d3";

import {mount, shallow} from 'enzyme';
import {expect} from 'chai';
import { mount, shallow } from "enzyme";
import { expect } from "chai";

import {XYPlot, LineChart, XAxis} from 'src/index.js';
import { XYPlot, LineChart, XAxis } from "src/index.js";

// XAxis tests must run in browser since XAxis uses measureText

describe('XAxis', () => {
describe("XAxis", () => {
const width = 500;
const height = 300;

it('extends the scale domain if to include custom `ticks` if passed', () => {
it("extends the scale domain if to include custom `ticks` if passed", () => {
const props = {
width, height,
scaleType: {x: 'linear', y: 'linear'},
margin: {top: 11, bottom: 22, left: 33, right: 44}
width,
height,
scaleType: { x: "linear", y: "linear" },
margin: { top: 11, bottom: 22, left: 33, right: 44 }
};

const tree = <XYPlot {...props}>
<LineChart data={[[0, 0], [10, 10]]} getX={0} getY={1} />
<XAxis ticks={[-5, 0, 5]} />
</XYPlot>;
const tree = (
<XYPlot {...props}>
<LineChart data={[[0, 0], [10, 10]]} getX={0} getY={1} />
<XAxis ticks={[-5, 0, 5]} />
</XYPlot>
);
const rendered = mount(tree).find(XAxis);

expect(rendered.props().domain.x).to.deep.equal([-5, 10]);
expect(rendered.props().domain.y).to.deep.equal([0, 10]);
});

it('rounds domain to nice numbers if `nice` option is true', () => {
it("rounds domain to nice numbers if `nice` option is true", () => {
const props = {
width, height,
scaleType: {x: 'linear', y: 'linear'},
margin: {top: 11, bottom: 22, left: 33, right: 44}
width,
height,
scaleType: { x: "linear", y: "linear" },
margin: { top: 11, bottom: 22, left: 33, right: 44 }
};

const niceXChart = mount(
Expand All @@ -46,5 +50,5 @@ describe('XAxis', () => {

expect(niceXChart.props().domain.x).to.deep.equal([0, 10]);
expect(niceXChart.props().domain.y).to.deep.equal([0.8, 9.7]);
})
});
});
52 changes: 24 additions & 28 deletions tests/browser/webpack.config.test.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,57 @@
var path = require('path');
var webpack = require('webpack');
var _ = require('lodash');
var HtmlPlugin = require('html-webpack-plugin');
var CleanPlugin = require('clean-webpack-plugin');
var path = require("path");
var webpack = require("webpack");
var _ = require("lodash");
var HtmlPlugin = require("html-webpack-plugin");
var CleanPlugin = require("clean-webpack-plugin");

console.log('dirname', __dirname);
console.log("dirname", __dirname);
module.exports = {
context: __dirname,
entry: [
path.join(__dirname, 'index.js')
],
entry: [path.join(__dirname, "index.js")],
output: {
path: path.join(__dirname, 'build'),
filename: 'bundle.[hash].js',
path: path.join(__dirname, "build"),
filename: "bundle.[hash].js"
},
devtool: 'source-map',
devtool: "source-map",
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new HtmlPlugin({
title: "Reactochart Tests",
template: path.join(__dirname, 'index_html.ejs'),
template: path.join(__dirname, "index_html.ejs")
}),
new CleanPlugin([path.join(__dirname, 'build')])
new CleanPlugin([path.join(__dirname, "build")])
],
resolve: {
extensions: ['.js', '.jsx'],
extensions: [".js", ".jsx"],
modules: ["node_modules", path.resolve(__dirname, "../..")]
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [{loader: 'babel-loader'}]
use: [{ loader: "babel-loader" }]
},
{
test: /\.less?$/,
use: [
{loader: 'style-loader'},
{loader: 'css-loader'},
{loader: 'less-loader'}
{ loader: "style-loader" },
{ loader: "css-loader" },
{ loader: "less-loader" }
]
},
{
test: /\.json$/,
use: [{loader: 'json-loader'}]
use: [{ loader: "json-loader" }]
}
]
},
// https://github.com/airbnb/enzyme/issues/503
externals: {
'jsdom': 'window',
'cheerio': 'window',
'react/lib/ExecutionEnvironment': true,
'react/addons': true,
'react/lib/ReactContext': 'window'
},
jsdom: "window",
cheerio: "window",
"react/lib/ExecutionEnvironment": true,
"react/addons": true,
"react/lib/ReactContext": "window"
}
};


2 changes: 1 addition & 1 deletion tests/eslint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CLIEngine } from "eslint";
import { expect, assert } from "chai";

const srcPaths = glob.sync("./src/*.js");
const testPaths = glob.sync("./tests/jsdom/spec/*.js");
const testPaths = glob.sync("./tests/*.js");
const engine = new CLIEngine({
envs: ["node", "mocha"],
useEslintrc: true
Expand Down
2 changes: 1 addition & 1 deletion tests/jsdom/spec/AreaBarChart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as d3 from "d3";
import { expect } from "chai";
import { mount } from "enzyme";

import { XYPlot, AreaBarChart, RangeRect } from "../../../src/index.js";
import { AreaBarChart, RangeRect } from "../../../src/index.js";
import { getValue } from "../../../src/utils/Data.js";

describe("AreaBarChart", () => {
Expand Down
131 changes: 131 additions & 0 deletions tests/jsdom/spec/AreaChart.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React from "react";
import * as d3 from "d3";
import { expect } from "chai";
import { mount } from "enzyme";
import _ from "lodash";
import { AreaChart } from "../../../src/index.js";

import { combineDatasets } from "../../../src/utils/Data.js";

function randomWalk(length = 100, start = 0, variance = 10) {
return _.reduce(
_.range(length - 1),
(sequence, i) => {
return sequence.concat(_.last(sequence) + _.random(-variance, variance));
},
[start]
);
}

function randomWalkTimeSeries(
length = 100,
start = 0,
variance = 10,
startDate = new Date(2015, 0, 1)
) {
let date = startDate;
return randomWalk(length, start, variance).map((n, i) => {
date = new Date(date.getTime() + 24 * 60 * 60 * 1000);
return [date, n];
});
}

describe("AreaChart", () => {
const areaChartProps = {
data: _.range(41),
x: d => d,
y: d => Math.sin(d / 10) * 10,
yEnd: d => Math.cos((d + 1) / 10) * 10,
pathClassName: "my-path",
xScale: d3.scaleLinear().domain([0, 41]),
yScale: d3.scaleLinear().domain([0, 10]),
pathStyle: { fill: "red" }
};

const data1 = randomWalkTimeSeries(115).map(([x, y]) => ({ x, y }));
const data2 = randomWalkTimeSeries(115).map(([x, y]) => ({ x, y }));

// we have two datasets, but AreaChart takes one combined dataset
// so combine the two datasets into one using the combineDatasets utility function
// (from 'reactochart/utils/Data')
const combined = combineDatasets(
[
{ data: data1, combineKey: "x", dataKeys: { y: "y0" } },
{ data: data2, combineKey: "x", dataKeys: { y: "y1" } }
],
"x"
);

const differenceAreaChartProps = {
data: combined,
isDifference: true,
pathStylePositive: { fill: "blue" },
pathStyleNegative: { fill: "green" },
x: d => d.x,
y: d => d.y0,
yEnd: d => d.y1
};

it("passes props correctly to path", () => {
let chart = mount(<AreaChart {...areaChartProps} />);
let path = chart.find("path");

expect(path.props().className).to.contain("my-path");
expect(path.props().style).to.equal(areaChartProps.pathStyle);

chart = mount(
<AreaChart {...areaChartProps} {...differenceAreaChartProps} />
);
path = chart.find("path");

path.forEach((p, index) => {
if (index < 2) {
expect(p.props().className).to.not.contain(
areaChartProps.pathClassName
);
} else if (index == 3) {
expect(p.props().style.fill).to.equal(
differenceAreaChartProps.pathStyleNegative.fill
);
expect(p.props().className).to.contain(areaChartProps.pathClassName);
} else {
expect(p.props().style.fill).to.equal(
differenceAreaChartProps.pathStylePositive.fill
);
expect(p.props().className).to.contain(areaChartProps.pathClassName);
}
});
});

it("renders the expected group and the expected number of paths", () => {
let chart = mount(<AreaChart {...areaChartProps} />);
let path = chart.find("path");
let group = chart.find("g");

expect(group.length).to.equal(1);
expect(group.props().className).to.equal("rct-area-chart");
expect(path.length).to.equal(1);

chart = mount(
<AreaChart {...areaChartProps} {...differenceAreaChartProps} />
);
path = chart.find("path");
group = chart.find("g");

expect(group.length).to.equal(1);
expect(group.props().className).to.equal("rct-area-chart--difference");
expect(path.length).to.equal(4);
});

it("getDomain returns correctly", () => {
const domainProps = {
data: _.range(10),
x: () => x,
y: () => 0,
yEnd: d => d + 2
};
const domain = { yDomain: [0, 11] };

expect(AreaChart.getDomain(domainProps)).to.eql(domain);
});
});
Loading

0 comments on commit b5c9a6d

Please sign in to comment.