-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Charting: Adding tests for AreaChart (#15702)
Cherry-pick of #15520.
- Loading branch information
Showing
3 changed files
with
2,298 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
change/@fluentui-react-charting-2020-10-26-14-48-23-areaChartTests.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
192
packages/react-charting/src/components/AreaChart/AreaChart.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.