test: extract mock plugins into separate file (#172)

* test: extract mock plugins into separate file

* fix: use constants

* fix: test coverage
This commit is contained in:
Krist Wongsuphasawat 2019-06-12 16:42:45 -07:00 committed by Yongjie Zhao
parent bbf13fd0d0
commit 4f23311813
2 changed files with 110 additions and 68 deletions

View File

@ -0,0 +1,69 @@
import React from 'react';
import { ChartMetadata, ChartPlugin, ChartFormData } from '../../src';
export const TestComponent = ({
message,
width,
height,
}: {
message: string;
width: number;
height: number;
}) => (
<div className="test-component">
<span className="message">{message || 'test-message'}</span>
<span className="dimension">{[width, height].join('x')}</span>
</div>
);
export const ChartKeys = {
DILIGENT: 'diligent-chart',
LAZY: 'lazy-chart',
SLOW: 'slow-chart',
};
export class DiligentChartPlugin extends ChartPlugin<ChartFormData> {
constructor() {
super({
metadata: new ChartMetadata({
name: ChartKeys.DILIGENT,
thumbnail: '',
}),
Chart: TestComponent,
transformProps: x => x,
});
}
}
export class LazyChartPlugin extends ChartPlugin<ChartFormData> {
constructor() {
super({
metadata: new ChartMetadata({
name: ChartKeys.LAZY,
thumbnail: '',
}),
// this mirrors `() => import(module)` syntax
loadChart: () => Promise.resolve({ default: TestComponent }),
// promise without .default
loadTransformProps: () => Promise.resolve((x: any) => x),
});
}
}
export class SlowChartPlugin extends ChartPlugin<ChartFormData> {
constructor() {
super({
metadata: new ChartMetadata({
name: ChartKeys.SLOW,
thumbnail: '',
}),
loadChart: () =>
new Promise(resolve => {
setTimeout(() => {
resolve(TestComponent);
}, 1000);
}),
transformProps: x => x,
});
}
}

View File

@ -1,73 +1,33 @@
import React from 'react'; import React from 'react';
import { mount, shallow } from 'enzyme'; import { mount, shallow } from 'enzyme';
import { ChartProps, ChartMetadata, ChartPlugin, ChartFormData, SuperChart } from '../../src'; import { ChartProps } from '../../src';
import {
ChartKeys,
DiligentChartPlugin,
LazyChartPlugin,
SlowChartPlugin,
} from './MockChartPlugins';
import SuperChart from '../../src/components/SuperChart';
describe('SuperChart', () => { describe('SuperChart', () => {
const TestComponent = (props: any) => (
<div className="test-component">{props.character || 'test-component'}</div>
);
const chartProps = new ChartProps(); const chartProps = new ChartProps();
class MyChartPlugin extends ChartPlugin<ChartFormData> { new DiligentChartPlugin().configure({ key: ChartKeys.DILIGENT }).register();
constructor() { new LazyChartPlugin().configure({ key: ChartKeys.LAZY }).register();
super({ new SlowChartPlugin().configure({ key: ChartKeys.SLOW }).register();
metadata: new ChartMetadata({
name: 'my-chart',
thumbnail: '',
}),
Chart: TestComponent,
transformProps: x => x,
});
}
}
class SecondChartPlugin extends ChartPlugin<ChartFormData> {
constructor() {
super({
metadata: new ChartMetadata({
name: 'second-chart',
thumbnail: '',
}),
// this mirrors `() => import(module)` syntax
loadChart: () => Promise.resolve({ default: TestComponent }),
// promise without .default
loadTransformProps: () => Promise.resolve((x: any) => x),
});
}
}
class SlowChartPlugin extends ChartPlugin<ChartFormData> {
constructor() {
super({
metadata: new ChartMetadata({
name: 'slow-chart',
thumbnail: '',
}),
loadChart: () =>
new Promise(resolve => {
setTimeout(() => {
resolve(TestComponent);
}, 1000);
}),
transformProps: x => x,
});
}
}
new MyChartPlugin().configure({ key: 'my-chart' }).register();
new SecondChartPlugin().configure({ key: 'second-chart' }).register();
new SlowChartPlugin().configure({ key: 'slow-chart' }).register();
describe('registered charts', () => { describe('registered charts', () => {
it('renders registered chart', done => { it('renders registered chart', done => {
const wrapper = shallow(<SuperChart chartType="my-chart" chartProps={chartProps} />); const wrapper = shallow(
<SuperChart chartType={ChartKeys.DILIGENT} chartProps={chartProps} />,
);
setTimeout(() => { setTimeout(() => {
expect(wrapper.render().find('div.test-component')).toHaveLength(1); expect(wrapper.render().find('div.test-component')).toHaveLength(1);
done(); done();
}, 0); }, 0);
}); });
it('renders registered chart with default export', done => { it('renders registered chart with default export', done => {
const wrapper = shallow(<SuperChart chartType="second-chart" />); const wrapper = shallow(<SuperChart chartType={ChartKeys.LAZY} />);
setTimeout(() => { setTimeout(() => {
expect(wrapper.render().find('div.test-component')).toHaveLength(1); expect(wrapper.render().find('div.test-component')).toHaveLength(1);
done(); done();
@ -82,14 +42,14 @@ describe('SuperChart', () => {
}, 5); }, 5);
}); });
it('adds id to container if specified', done => { it('adds id to container if specified', done => {
const wrapper = shallow(<SuperChart chartType="my-chart" id="the-chart" />); const wrapper = shallow(<SuperChart chartType={ChartKeys.DILIGENT} id="the-chart" />);
setTimeout(() => { setTimeout(() => {
expect(wrapper.render().attr('id')).toEqual('the-chart'); expect(wrapper.render().attr('id')).toEqual('the-chart');
done(); done();
}, 0); }, 0);
}); });
it('adds class to container if specified', done => { it('adds class to container if specified', done => {
const wrapper = shallow(<SuperChart chartType="my-chart" className="the-chart" />); const wrapper = shallow(<SuperChart chartType={ChartKeys.DILIGENT} className="the-chart" />);
setTimeout(() => { setTimeout(() => {
expect(wrapper.hasClass('the-chart')).toBeTruthy(); expect(wrapper.hasClass('the-chart')).toBeTruthy();
done(); done();
@ -97,13 +57,16 @@ describe('SuperChart', () => {
}); });
it('uses overrideTransformProps when specified', done => { it('uses overrideTransformProps when specified', done => {
const wrapper = shallow( const wrapper = shallow(
<SuperChart chartType="my-chart" overrideTransformProps={() => ({ character: 'hulk' })} />, <SuperChart
chartType={ChartKeys.DILIGENT}
overrideTransformProps={() => ({ message: 'hulk' })}
/>,
); );
setTimeout(() => { setTimeout(() => {
expect( expect(
wrapper wrapper
.render() .render()
.find('div.test-component') .find('span.message')
.text(), .text(),
).toEqual('hulk'); ).toEqual('hulk');
done(); done();
@ -111,11 +74,11 @@ describe('SuperChart', () => {
}); });
it('uses preTransformProps when specified', done => { it('uses preTransformProps when specified', done => {
const chartPropsWithPayload = new ChartProps({ const chartPropsWithPayload = new ChartProps({
payload: { character: 'hulk' }, payload: { message: 'hulk' },
}); });
const wrapper = shallow( const wrapper = shallow(
<SuperChart <SuperChart
chartType="my-chart" chartType={ChartKeys.DILIGENT}
preTransformProps={() => chartPropsWithPayload} preTransformProps={() => chartPropsWithPayload}
overrideTransformProps={props => props.payload} overrideTransformProps={props => props.payload}
/>, />,
@ -124,7 +87,7 @@ describe('SuperChart', () => {
expect( expect(
wrapper wrapper
.render() .render()
.find('div.test-component') .find('span.message')
.text(), .text(),
).toEqual('hulk'); ).toEqual('hulk');
done(); done();
@ -132,34 +95,44 @@ describe('SuperChart', () => {
}); });
it('uses postTransformProps when specified', done => { it('uses postTransformProps when specified', done => {
const wrapper = shallow( const wrapper = shallow(
<SuperChart chartType="my-chart" postTransformProps={() => ({ character: 'hulk' })} />, <SuperChart
chartType={ChartKeys.DILIGENT}
postTransformProps={() => ({ message: 'hulk' })}
/>,
); );
setTimeout(() => { setTimeout(() => {
expect( expect(
wrapper wrapper
.render() .render()
.find('div.test-component') .find('span.message')
.text(), .text(),
).toEqual('hulk'); ).toEqual('hulk');
done(); done();
}, 0); }, 0);
}); });
it('renders if chartProps is not specified', done => { it('renders if chartProps is not specified', done => {
const wrapper = shallow(<SuperChart chartType="my-chart" />); const wrapper = shallow(<SuperChart chartType={ChartKeys.DILIGENT} />);
setTimeout(() => { setTimeout(() => {
expect(wrapper.render().find('div.test-component')).toHaveLength(1); expect(wrapper.render().find('div.test-component')).toHaveLength(1);
done(); done();
}, 0); }, 0);
}); });
it('does not render anything while waiting for Chart code to load', done => { it('does not render anything while waiting for Chart code to load', done => {
const wrapper = shallow(<SuperChart chartType="slow-chart" />); const wrapper = shallow(<SuperChart chartType={ChartKeys.SLOW} />);
setTimeout(() => { setTimeout(() => {
expect(wrapper.render().children()).toHaveLength(0); expect(wrapper.render().children()).toHaveLength(0);
done(); done();
}, 0); }, 0);
}); });
it('eventually renders after Chart is loaded', done => {
const wrapper = shallow(<SuperChart chartType={ChartKeys.SLOW} />);
setTimeout(() => {
expect(wrapper.render().find('div.test-component')).toHaveLength(1);
done();
}, 1500);
});
it('does not render if chartProps is null', done => { it('does not render if chartProps is null', done => {
const wrapper = shallow(<SuperChart chartType="my-chart" chartProps={null} />); const wrapper = shallow(<SuperChart chartType={ChartKeys.DILIGENT} chartProps={null} />);
setTimeout(() => { setTimeout(() => {
expect(wrapper.render().find('div.test-component')).toHaveLength(0); expect(wrapper.render().find('div.test-component')).toHaveLength(0);
done(); done();
@ -180,7 +153,7 @@ describe('SuperChart', () => {
describe('.processChartProps()', () => { describe('.processChartProps()', () => {
it('use identity functions for unspecified transforms', () => { it('use identity functions for unspecified transforms', () => {
const chart = new SuperChart({ const chart = new SuperChart({
chartType: 'my-chart', chartType: ChartKeys.DILIGENT,
}); });
const chartProps2 = new ChartProps(); const chartProps2 = new ChartProps();
expect(chart.processChartProps({ chartProps: chartProps2 })).toBe(chartProps2); expect(chart.processChartProps({ chartProps: chartProps2 })).toBe(chartProps2);