Skip to content

Commit

Permalink
Add tests for hover states (#24390)
Browse files Browse the repository at this point in the history
* Add test case for AreaChart hover state

* Add enzyme-to-json package

* Remove _rectRef

* Add test case for DonutChart hover state

* Update callout snapshot

* Add test case for LineChart hover state

* Add test case for GroupedBarChart hover state

* Add test case for HeatMapChart hover state

* Add test case for HorizontalBarChart hover state

* Add test case for StackedBarChart hover state

* Add test case for VerticalBarChart hover state

* Add test case for VertStackedBarChart hover state

* Mock Math.random()

* Remove Legends measurement snapshot

* Add comments

* Add change file

* Add more test cases

* Remove duplicate code

* Update snapshots

* Replace callout text with complete HTML comparison

* Update Math.random() mock

* Update snapshots

* Move dependency to root

* Update @types/enzyme
  • Loading branch information
krkshitij authored Sep 12, 2022
1 parent 866e19a commit 8c22e07
Show file tree
Hide file tree
Showing 26 changed files with 11,727 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Add tests for hover states",
"packageName": "@fluentui/react-charting",
"email": "kumarkshitij@microsoft.com",
"dependentChangeType": "none"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"@types/dedent": "0.7.0",
"@types/doctrine": "0.0.5",
"@types/enhanced-resolve": "3.0.7",
"@types/enzyme": "3.10.3",
"@types/enzyme": "3.10.7",
"@types/eslint": "7.2.13",
"@types/express": "4.17.2",
"@types/fs-extra": "8.0.1",
Expand Down Expand Up @@ -177,6 +177,7 @@
"enhanced-resolve": "5.8.2",
"enquirer": "2.3.6",
"enzyme": "3.10.0",
"enzyme-to-json": "3.6.2",
"esbuild": "0.14.27",
"esbuild-loader": "2.18.0",
"eslint": "7.25.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-charting/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let path = require('path');

const config = createConfig({
setupFiles: [path.resolve(path.join(__dirname, 'config', 'tests.js'))],
snapshotSerializers: [resolveMergeStylesSerializer()],
snapshotSerializers: [resolveMergeStylesSerializer(), 'enzyme-to-json/serializer'],
});

module.exports = config;
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
// determines if the given area chart has multiple stacked bar charts
private _isMultiStackChart: boolean;
private _tooltipId: string;
private _highlightedCircleId: string;

public constructor(props: IAreaChartProps) {
super(props);
Expand Down Expand Up @@ -123,6 +124,16 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
this._tooltipId = getId('AreaChartTooltipID');
}

public componentDidUpdate() {
if (this.state.isShowCalloutPending) {
this.setState({
refSelected: `#${this._highlightedCircleId}`,
isCalloutVisible: true,
isShowCalloutPending: false,
});
}
}

public render(): JSX.Element {
const { lineChartData, chartTitle } = this.props.data;
const { colors, opacity, stackedInfo, calloutPoints } = this._createSet(this.props.data);
Expand Down Expand Up @@ -537,13 +548,7 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
private _updateCircleFillColor = (xDataPoint: number | Date, lineColor: string, circleId: string): string => {
let fillColor = lineColor;
if (this.state.nearestCircleToHighlight === xDataPoint) {
if (this.state.isShowCalloutPending) {
this.setState({
refSelected: `#${circleId}`,
isCalloutVisible: true,
isShowCalloutPending: false,
});
}
this._highlightedCircleId = circleId;
if (!this.state.isCircleClicked) {
fillColor = this.props.theme!.palette.white;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mount, ReactWrapper } from 'enzyme';
import { IAreaChartProps, AreaChart } from './index';
import { IAreaChartState, AreaChartBase } from './AreaChart.base';
import { ICustomizedCalloutData } from '../../index';
import toJson from 'enzyme-to-json';

// Wrapper of the AreaChart to be tested.
let wrapper: ReactWrapper<IAreaChartProps, IAreaChartState, AreaChartBase> | undefined;
Expand Down Expand Up @@ -190,3 +191,80 @@ describe('Render calling with respective to props', () => {
renderMock.mockRestore();
});
});

describe('AreaChart - mouse events', () => {
let root: HTMLDivElement | null;

beforeEach(() => {
sharedBeforeEach();

root = document.createElement('div');
document.body.appendChild(root);
});

afterEach(() => {
sharedAfterEach();

if (root) {
document.body.removeChild(root);
root = null;
}
});

it('Should render callout correctly on mouseover', () => {
// document.getElementbyId() returns null if component is not attached to DOM
wrapper = mount(<AreaChart data={chartPoints} calloutProps={{ doNotLayer: true }} />, { attachTo: root });
wrapper.find('rect').simulate('mouseover');
const tree = toJson(wrapper, { mode: 'deep' });
expect(tree).toMatchSnapshot();
});

it('Should render callout correctly on mousemove', () => {
wrapper = mount(<AreaChart data={chartPoints} calloutProps={{ doNotLayer: true }} />, { attachTo: root });
wrapper.find('rect').simulate('mousemove', { clientX: 40, clientY: 0 });
const html1 = wrapper.html();
wrapper.find('rect').simulate('mousemove', { clientX: -20, clientY: 0 });
const html2 = wrapper.html();
expect(html1).not.toBe(html2);
});

it('Should render customized callout on mouseover', () => {
wrapper = mount(
<AreaChart
data={chartPoints}
calloutProps={{ doNotLayer: true }}
onRenderCalloutPerDataPoint={(props: ICustomizedCalloutData) =>
props ? (
<div>
<pre>{JSON.stringify(props, null, 2)}</pre>
</div>
) : null
}
/>,
{ attachTo: root },
);
wrapper.find('rect').simulate('mouseover');
const tree = toJson(wrapper, { mode: 'deep' });
expect(tree).toMatchSnapshot();
});

it('Should render customized callout per stack on mouseover', () => {
wrapper = mount(
<AreaChart
data={chartPoints}
calloutProps={{ doNotLayer: true }}
onRenderCalloutPerStack={(props: ICustomizedCalloutData) =>
props ? (
<div>
<pre>{JSON.stringify(props, null, 2)}</pre>
</div>
) : null
}
/>,
{ attachTo: root },
);
wrapper.find('rect').simulate('mouseover');
const tree = toJson(wrapper, { mode: 'deep' });
expect(tree).toMatchSnapshot();
});
});
Loading

0 comments on commit 8c22e07

Please sign in to comment.