-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
fix: improve Chart component #2138
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import React from 'react'; | ||
|
||
const ChartContext = React.createContext(); | ||
export default ChartContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import styled from 'styled-components'; | ||
|
||
const StyledDatasetContainer = styled.div` | ||
display: none; | ||
`; | ||
|
||
export default StyledDatasetContainer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import Dataset from '..'; | ||
|
||
const context = { | ||
registerDataset: jest.fn(), | ||
unregisterDataset: jest.fn(), | ||
updateDataset: jest.fn(), | ||
}; | ||
|
||
describe('<Dataset />', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
React.useContext = jest.fn().mockReturnValue(context); | ||
}); | ||
|
||
it('should call registerDataset with dataset props', () => { | ||
mount( | ||
<Dataset | ||
title="Dataset 1" | ||
values={[1, 2, 3]} | ||
backgroundColor="#ccc" | ||
borderColor="#000" | ||
stack="stack-1" | ||
type="line" | ||
/>, | ||
); | ||
expect(context.registerDataset).toHaveBeenCalledWith(expect.any(String), { | ||
label: 'Dataset 1', | ||
data: [1, 2, 3], | ||
backgroundColor: '#ccc', | ||
borderColor: '#000', | ||
stack: 'stack-1', | ||
type: 'line', | ||
fill: false, | ||
}); | ||
}); | ||
|
||
it('should call unregisterDataset when unmounted', () => { | ||
const wrapper = mount( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
<Dataset | ||
title="Dataset 1" | ||
values={[1, 2, 3]} | ||
backgroundColor="#ccc" | ||
borderColor="#000" | ||
stack="stack-1" | ||
type="line" | ||
/>, | ||
); | ||
wrapper.unmount(); | ||
expect(context.unregisterDataset).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call updateDataset when props changes', () => { | ||
const wrapper = mount( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
<Dataset | ||
title="Dataset 1" | ||
values={[1, 2, 3]} | ||
backgroundColor="#ccc" | ||
borderColor="#000" | ||
stack="stack-1" | ||
type="line" | ||
/>, | ||
); | ||
wrapper.setProps({ | ||
values: [4, 5, 6], | ||
}); | ||
expect(context.updateDataset).toHaveBeenCalledWith(expect.any(String), { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
label: 'Dataset 1', | ||
data: [4, 5, 6], | ||
backgroundColor: '#ccc', | ||
borderColor: '#000', | ||
stack: 'stack-1', | ||
type: 'line', | ||
fill: false, | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,44 @@ | ||
/* eslint-disable react/no-unused-prop-types */ | ||
import React from 'react'; | ||
import React, { useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ChartContext from '../Chart/context'; | ||
import { useUniqueIdentifier } from '../../libs/hooks'; | ||
|
||
/** @category DataView */ | ||
export default function Dataset() { | ||
return <div />; | ||
export default function Dataset(props) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
const datasetId = useUniqueIdentifier('dataset'); | ||
const { title: label, values: data, ...rest } = props; | ||
const { registerDataset, unregisterDataset, updateDataset } = React.useContext(ChartContext); | ||
const isRegistered = useRef(); | ||
|
||
useEffect(() => { | ||
if (isRegistered.current) { | ||
updateDataset(datasetId, { | ||
label, | ||
data, | ||
...rest, | ||
}); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
registerDataset(datasetId, { | ||
label, | ||
data, | ||
...rest, | ||
}); | ||
isRegistered.current = true; | ||
|
||
return () => { | ||
if (isRegistered.current) { | ||
unregisterDataset(datasetId); | ||
isRegistered.current = false; | ||
} | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return <></>; | ||
} | ||
|
||
Dataset.propTypes = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.