-
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 VerticalStackedBarChart (#15709)
Cherry-pick of #15522.
- Loading branch information
Showing
3 changed files
with
2,665 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-17-48-27-verticalStackedBarChartTests.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 VerticalStackedBarChart.", | ||
"packageName": "@fluentui/react-charting", | ||
"email": "humbertomakotomorimoto@gmail.com", | ||
"dependentChangeType": "none", | ||
"date": "2020-10-27T00:48:27.433Z" | ||
} |
229 changes: 229 additions & 0 deletions
229
...es/react-charting/src/components/VerticalStackedBarChart/VerticalStackedBarChart.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,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(); | ||
}); | ||
}); |
Oops, something went wrong.