Skip to content

Commit

Permalink
Charting: Adding tests for VerticalStackedBarChart (microsoft#15709)
Browse files Browse the repository at this point in the history
Cherry-pick of microsoft#15522.
  • Loading branch information
khmakoto authored and Seth Donohue committed Nov 2, 2020
1 parent 89fcf8d commit 48cd06f
Show file tree
Hide file tree
Showing 3 changed files with 2,665 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 VerticalStackedBarChart.",
"packageName": "@fluentui/react-charting",
"email": "humbertomakotomorimoto@gmail.com",
"dependentChangeType": "none",
"date": "2020-10-27T00:48:27.433Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
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 { DefaultPalette } from '@fluentui/react/lib/Styling';
import {
IVSChartDataPoint,
IVerticalStackedBarChartProps,
VerticalStackedBarChart,
IVerticalStackedChartProps,
} from '../../index';
import { IVerticalStackedBarChartState, VerticalStackedBarChartBase } from './VerticalStackedBarChart.base';

// Wrapper of the VerticalStackedBarChart to be tested.
let wrapper:
| ReactWrapper<IVerticalStackedBarChartProps, IVerticalStackedBarChartState, VerticalStackedBarChartBase>
| 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 firstChartPoints: IVSChartDataPoint[] = [
{
legend: 'Metadata1',
data: 40,
color: DefaultPalette.accent,
xAxisCalloutData: '2020/04/30',
yAxisCalloutData: '40%',
},
{
legend: 'Metadata2',
data: 5,
color: DefaultPalette.blueMid,
xAxisCalloutData: '2020/04/30',
yAxisCalloutData: '5%',
},
];

const secondChartPoints: IVSChartDataPoint[] = [
{
legend: 'Metadata1',
data: 30,
color: DefaultPalette.accent,
xAxisCalloutData: '2020/04/30',
yAxisCalloutData: '30%',
},
{
legend: 'Metadata2',
data: 20,
color: DefaultPalette.blueMid,
xAxisCalloutData: '2020/04/30',
yAxisCalloutData: '20%',
},
];

const chartPoints: IVerticalStackedChartProps[] = [
{ chartData: firstChartPoints, xAxisPoint: 0 },
{ chartData: secondChartPoints, xAxisPoint: 20 },
];

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

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

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

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

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

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

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

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

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

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

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

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

it('Should render onRenderCalloutPerStack ', () => {
wrapper = mount(
<VerticalStackedBarChart
data={chartPoints}
onRenderCalloutPerStack={(props: IVerticalStackedChartProps) =>
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(<VerticalStackedBarChart data={chartPoints} />);
const renderedDOM = wrapper.getDOMNode().getElementsByClassName('.onRenderCalloutPerStack');
expect(renderedDOM!.length).toBe(0);
});

it('Should render onRenderCalloutPerDataPoint ', () => {
wrapper = mount(
<VerticalStackedBarChart
data={chartPoints}
onRenderCalloutPerDataPoint={(props: IVSChartDataPoint) =>
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(<VerticalStackedBarChart 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(VerticalStackedBarChartBase.prototype, 'render');
const props = {
data: chartPoints,
height: 300,
width: 600,
};
const component = mount(<VerticalStackedBarChart {...props} />);
component.setProps({ ...props });
expect(renderMock).toHaveBeenCalledTimes(2);
renderMock.mockRestore();
});

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

0 comments on commit 48cd06f

Please sign in to comment.