From a828c52ae4d7f212471df3c72cdbb155bb30df2f Mon Sep 17 00:00:00 2001 From: Kevin Qi Date: Sat, 11 May 2019 15:18:00 -0700 Subject: [PATCH] fix bug in pathRatio calculation for negative numbers, add tests --- src/CircularProgressbar.tsx | 13 +++--- test/CircularProgressbar.test.tsx | 68 +++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/src/CircularProgressbar.tsx b/src/CircularProgressbar.tsx index 93fd46d..2adf25f 100644 --- a/src/CircularProgressbar.tsx +++ b/src/CircularProgressbar.tsx @@ -56,23 +56,26 @@ class CircularProgressbar extends React.Component { return VIEWBOX_HEIGHT_HALF - this.props.strokeWidth / 2 - this.getBackgroundPadding(); } + // Ratio of path length to trail length, as a value between 0 and 1 + getPathRatio() { + const { value, minValue, maxValue } = this.props; + const boundedValue = Math.min(Math.max(value, minValue), maxValue); + return (boundedValue - minValue) / (maxValue - minValue); + } + render() { const { circleRatio, className, classes, counterClockwise, - maxValue, - minValue, styles, strokeWidth, text, - value, } = this.props; const pathRadius = this.getPathRadius(); - const boundedValue = Math.min(Math.max(value, minValue), maxValue); - const pathRatio = boundedValue / (maxValue - minValue); + const pathRatio = this.getPathRatio(); return ( ', () => { describe('props.value', () => { test('Renders correct path', () => { const percentage = 30; - const wrapper = mount( - , - ); + const wrapper = mount(); expect( wrapper @@ -67,6 +65,70 @@ describe('', () => { .prop('d'), ).toContain(expectedArcto); }); + test('Value is bounded by minValue', () => { + const percentage = -30; + const wrapper = mount( + , + ); + + expect( + wrapper + .find('.CircularProgressbar-path') + .hostNodes() + .prop('style')!.strokeDashoffset, + ).toEqual(getExpectedStrokeDashoffset({ percentage: 0, strokeWidth: 0 })); + }); + test('Value is bounded by maxValue', () => { + const percentage = 130; + const wrapper = mount( + , + ); + + expect( + wrapper + .find('.CircularProgressbar-path') + .hostNodes() + .prop('style')!.strokeDashoffset, + ).toEqual(getExpectedStrokeDashoffset({ percentage: 100, strokeWidth: 0 })); + }); + }); + describe('props.minValue, props.maxValue', () => { + test('Positive min/max', () => { + const wrapper = mount( + , + ); + + expect( + wrapper + .find('.CircularProgressbar-path') + .hostNodes() + .prop('style')!.strokeDashoffset, + ).toEqual(getExpectedStrokeDashoffset({ percentage: 25, strokeWidth: 0 })); + }); + test('Negative to positive min/max', () => { + const wrapper = mount( + , + ); + + expect( + wrapper + .find('.CircularProgressbar-path') + .hostNodes() + .prop('style')!.strokeDashoffset, + ).toEqual(getExpectedStrokeDashoffset({ percentage: 50, strokeWidth: 0 })); + }); + test('Negative min/max', () => { + const wrapper = mount( + , + ); + + expect( + wrapper + .find('.CircularProgressbar-path') + .hostNodes() + .prop('style')!.strokeDashoffset, + ).toEqual(getExpectedStrokeDashoffset({ percentage: 50, strokeWidth: 0 })); + }); }); describe('props.counterClockwise', () => { test('Reverses dashoffset', () => {