Skip to content

Commit

Permalink
Fix plotline and plotband update lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
anajavi committed Sep 29, 2019
1 parent f14dd1f commit 3b318de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,23 @@ export default function usePlotBandLine(props, plotType) {
const modifiedProps = useModifiedProps(rest);

useEffect(() => {
if (!axis || !plotbandline) return;
if (modifiedProps !== false) {
axis.removePlotBandOrLine(idRef.current);
if (!axis) return;
if(!plotbandline || modifiedProps !== false) {
if(!plotbandline) {
idRef.current = typeof id === 'function' ? id() : id;
}
const myId = idRef.current;
const opts = {
id: idRef.current,
...modifiedProps
id: myId,
...rest
};
setPlotbandline(axis.addPlotBandOrLine(opts, plotType));
}
});

// create plotline
useEffect(() => {
if (!axis) return;
idRef.current = typeof id === 'function' ? id() : id;
const myId = idRef.current;
const opts = {
id: myId,
...rest
};
setPlotbandline(axis.addPlotBandOrLine(opts, plotType));

return () => {
attempt(axis.removePlotBandOrLine, idRef.current);
};
}, [axis]);
});

return plotbandline;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ describe('<PlotLine />', () => {
);
expect(testContext.axisStubs.addPlotBandOrLine.mock.calls[0][0].id).toMatch(uuidRegex);
});
});

describe('when updated', () => {
it('removes and adds plotline when props change', () => {
const wrapper = mount(
<PlotLine id="myplotline" value={3} />
<PlotLine id="myplotline" value={3} width={10} />
);
testContext.axisStubs.addPlotBandOrLine.mockClear();

wrapper.setProps({ value: 4});
expect(testContext.axisStubs.addPlotBandOrLine).toHaveBeenCalledTimes(1);
expect(testContext.axisStubs.removePlotBandOrLine).toHaveBeenCalledWith('myplotline');
expect(testContext.axisStubs.addPlotBandOrLine).toHaveBeenCalledTimes(1);
expect(testContext.axisStubs.addPlotBandOrLine).toHaveBeenCalledWith({
id: "myplotline",
value: 4,
width:10
}, 'plotLines');
});
});
})

describe('when unmounted', () => {
it('removes the plot line by id (if the parent axis still exists)', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/react-jsx-highcharts/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export const createMockProvidedChart = () => {
export const createMockAxis = ({ ...additional }) => ({
...additional,
remove: jest.fn(),
addPlotBandOrLine: jest.fn().mockImplementation(() => ({})),
addPlotBandOrLine: jest.fn().mockImplementation(() => (
{ options: {},
render: jest.fn()
})
),
removePlotBandOrLine: jest.fn(),
getExtremes: jest.fn(),
setExtremes: jest.fn(),
Expand Down

0 comments on commit 3b318de

Please sign in to comment.