Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charting: Adding tests for VerticalStackedBarChart #15709

Merged
merged 2 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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