Skip to content

Commit

Permalink
Charting: Adding tests for LineChart (#15700)
Browse files Browse the repository at this point in the history
Cherry-pick of #15507.
  • Loading branch information
khmakoto authored Oct 26, 2020
1 parent 9340abc commit 0b370e4
Show file tree
Hide file tree
Showing 3 changed files with 2,146 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 LineChart.",
"packageName": "@fluentui/react-charting",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-10-26T19:57:10.050Z"
}
145 changes: 145 additions & 0 deletions packages/react-charting/src/components/LineChart/LineChart.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
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 { ILineChartProps, LineChart } from './index';
import { ILineChartState, LineChartBase } from './LineChart.base';

// Wrapper of the LineChart to be tested.
let wrapper: ReactWrapper<ILineChartProps, ILineChartState, LineChartBase> | 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: 'LineChart',
lineChartData: points,
};

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

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

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

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

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

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

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

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

it('Should not mount legend when hideLegend true ', () => {
wrapper = mount(<LineChart 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(<LineChart data={chartPoints} hideLegend={false} />);
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="legendContainer"]');
expect(hideLegendDOM).toBeDefined();
});

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

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

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

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

0 comments on commit 0b370e4

Please sign in to comment.