-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 LineChart (#15700)
Cherry-pick of #15507.
- Loading branch information
Showing
3 changed files
with
2,146 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-12-57-10-lineChartTests.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 LineChart.", | ||
"packageName": "@fluentui/react-charting", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch", | ||
"date": "2020-10-26T19:57:10.050Z" | ||
} |
145 changes: 145 additions & 0 deletions
145
packages/react-charting/src/components/LineChart/LineChart.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,145 @@ | ||
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 { ILineChartProps, LineChart } from './index'; | ||
import { ILineChartState, LineChartBase } from './LineChart.base'; | ||
|
||
// Wrapper of the LineChart to be tested. | ||
let wrapper: ReactWrapper<ILineChartProps, ILineChartState, LineChartBase> | 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: 'LineChart', | ||
lineChartData: points, | ||
}; | ||
|
||
describe('LineChart snapShot testing', () => { | ||
it('renders LineChart correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders hideLegend correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} hideLegend={true} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders hideTooltip correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} hideTooltip={true} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders enabledLegendsWrapLines correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} enabledLegendsWrapLines={true} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders showXAxisLablesTooltip correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} showXAxisLablesTooltip={true} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders wrapXAxisLables correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} wrapXAxisLables={true} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders yAxisTickFormat correctly', () => { | ||
const component = renderer.create(<LineChart data={chartPoints} yAxisTickFormat={'/%d'} />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('LineChart - basic props', () => { | ||
beforeEach(sharedBeforeEach); | ||
afterEach(sharedAfterEach); | ||
|
||
it('Should not mount legend when hideLegend true ', () => { | ||
wrapper = mount(<LineChart 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(<LineChart data={chartPoints} hideLegend={false} />); | ||
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="legendContainer"]'); | ||
expect(hideLegendDOM).toBeDefined(); | ||
}); | ||
|
||
it('Should mount callout when hideTootip false ', () => { | ||
wrapper = mount(<LineChart data={chartPoints} />); | ||
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="ms-Layer"]'); | ||
expect(hideLegendDOM).toBeDefined(); | ||
}); | ||
|
||
it('Should not mount callout when hideTootip true ', () => { | ||
wrapper = mount(<LineChart data={chartPoints} hideTooltip={true} />); | ||
const hideLegendDOM = wrapper.getDOMNode().querySelectorAll('[class^="ms-Layer"]'); | ||
expect(hideLegendDOM.length).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('Render calling with respective to props', () => { | ||
it('No prop changes', () => { | ||
const renderMock = jest.spyOn(LineChartBase.prototype, 'render'); | ||
const props = { | ||
data: chartPoints, | ||
height: 300, | ||
width: 600, | ||
}; | ||
const component = mount(<LineChart {...props} />); | ||
component.setProps({ ...props }); | ||
expect(renderMock).toHaveBeenCalledTimes(2); | ||
renderMock.mockRestore(); | ||
}); | ||
|
||
it('prop changes', () => { | ||
const renderMock = jest.spyOn(LineChartBase.prototype, 'render'); | ||
const props = { | ||
data: chartPoints, | ||
height: 300, | ||
width: 600, | ||
hideLegend: true, | ||
}; | ||
const component = mount(<LineChart {...props} />); | ||
component.setProps({ ...props, hideTooltip: true }); | ||
expect(renderMock).toHaveBeenCalledTimes(2); | ||
renderMock.mockRestore(); | ||
}); | ||
}); |
Oops, something went wrong.