Skip to content

Commit

Permalink
Charting: Adding tests for AreaChart (#15702)
Browse files Browse the repository at this point in the history
Cherry-pick of #15520.
  • Loading branch information
khmakoto authored Oct 26, 2020
1 parent b2a4c18 commit 0fe2bfa
Show file tree
Hide file tree
Showing 3 changed files with 2,298 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "Charting: Adding tests for AreaChart.",
"packageName": "@fluentui/react-charting",
"email": "humbertomakotomorimoto@gmail.com",
"dependentChangeType": "none",
"date": "2020-10-26T21:48:22.991Z"
}
192 changes: 192 additions & 0 deletions packages/react-charting/src/components/AreaChart/AreaChart.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
jest.mock('react-dom');
import * as React from 'react';
import { resetIds } from '../../Utilities';
import * as renderer from 'react-test-renderer';
import { mount, ReactWrapper } from 'enzyme';
import { IAreaChartProps, AreaChart } from './index';
import { IAreaChartState, AreaChartBase } from './AreaChart.base';
import { ICustomizedCalloutData } from '../../index';

// Wrapper of the AreaChart to be tested.
let wrapper: ReactWrapper<IAreaChartProps, IAreaChartState, AreaChartBase> | undefined;

function sharedBeforeEach() {
resetIds();
}

function sharedAfterEach() {
if (wrapper) {
wrapper.unmount();
wrapper = undefined;
}

// Do this after unmounting the wrapper to make sure if any timers cleaned up on unmount are
// cleaned up in fake timers world
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((global.setTimeout as any).mock) {
jest.useRealTimers();
}
}

const points = [
{
legend: 'metaData1',
data: [
{ x: 20, y: 50 },
{ x: 40, y: 80 },
],
color: 'red',
},
];
const chartPoints = {
chartTitle: 'AreaChart',
lineChartData: points,
};

describe('AreaChart snapShot testing', () => {
it('renders Areachart correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders hideLegend hhh correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} hideLegend={true} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders hideTooltip correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} hideTooltip={true} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders enabledLegendsWrapLines correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} enabledLegendsWrapLines={true} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders showXAxisLablesTooltip correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} showXAxisLablesTooltip={true} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders wrapXAxisLables correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} wrapXAxisLables={true} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders yAxisTickFormat correctly', () => {
const component = renderer.create(<AreaChart data={chartPoints} yAxisTickFormat={'/%d'} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});

describe('AreaChart - basic props', () => {
beforeEach(sharedBeforeEach);
afterEach(sharedAfterEach);

it('Should not mount legend when hideLegend true ', () => {
wrapper = mount(<AreaChart data={chartPoints} hideLegend={true} />);
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="legendContainer"]');
expect(hideLegendDOM!.length).toBe(0);
});

it('Should mount legend when hideLegend false ', () => {
wrapper = mount(<AreaChart data={chartPoints} />);
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="legendContainer"]');
expect(hideLegendDOM).toBeDefined();
});

it('Should mount callout when hideTootip false ', () => {
wrapper = mount(<AreaChart data={chartPoints} />);
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="ms-Layer"]');
expect(hideLegendDOM).toBeDefined();
});

it('Should not mount callout when hideTootip true ', () => {
wrapper = mount(<AreaChart data={chartPoints} hideTooltip={true} />);
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="ms-Layer"]');
expect(hideLegendDOM.length).toBe(0);
});

it('Should render onRenderCalloutPerStack ', () => {
wrapper = mount(
<AreaChart
data={chartPoints}
onRenderCalloutPerStack={(props: ICustomizedCalloutData) =>
props ? (
<div className="onRenderCalloutPerStack">
<p>Custom Callout Content</p>
</div>
) : null
}
/>,
);
const renderedDOM = wrapper.getDOMNode().getElementsByClassName('.onRenderCalloutPerStack');
expect(renderedDOM).toBeDefined();
});

it('Should not render onRenderCalloutPerStack ', () => {
wrapper = mount(<AreaChart data={chartPoints} />);
const renderedDOM = wrapper.getDOMNode().getElementsByClassName('.onRenderCalloutPerStack');
expect(renderedDOM!.length).toBe(0);
});

it('Should render onRenderCalloutPerDataPoint ', () => {
wrapper = mount(
<AreaChart
data={chartPoints}
onRenderCalloutPerDataPoint={(props: ICustomizedCalloutData) =>
props ? (
<div className="onRenderCalloutPerDataPoint">
<p>Custom Callout Content</p>
</div>
) : null
}
/>,
);
const renderedDOM = wrapper.getDOMNode().getElementsByClassName('.onRenderCalloutPerDataPoint');
expect(renderedDOM).toBeDefined();
});

it('Should not render onRenderCalloutPerDataPoint ', () => {
wrapper = mount(<AreaChart data={chartPoints} />);
const renderedDOM = wrapper.getDOMNode().getElementsByClassName('.onRenderCalloutPerDataPoint');
expect(renderedDOM!.length).toBe(0);
});
});

describe('Render calling with respective to props', () => {
it('No prop changes', () => {
const renderMock = jest.spyOn(AreaChartBase.prototype, 'render');
const props = {
data: chartPoints,
height: 300,
width: 600,
};
const component = mount(<AreaChart {...props} />);
component.setProps({ ...props });
expect(renderMock).toHaveBeenCalledTimes(2);
renderMock.mockRestore();
});

it('prop changes', () => {
const renderMock = jest.spyOn(AreaChartBase.prototype, 'render');
const props = {
data: chartPoints,
height: 300,
width: 600,
hideLegend: true,
};
const component = mount(<AreaChart {...props} />);
component.setProps({ ...props, hideTooltip: true });
expect(renderMock).toHaveBeenCalledTimes(2);
renderMock.mockRestore();
});
});
Loading

0 comments on commit 0fe2bfa

Please sign in to comment.