From 1568019a59d19d27df17152600dd5043843ac0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Mon, 12 Sep 2016 04:54:16 +0200 Subject: [PATCH] fix: `onChange` which fired on every mousmove, added test (#145) --- src/Slider.jsx | 2 +- tests/index.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Slider.jsx b/src/Slider.jsx index 5f08f73..332cdd1 100644 --- a/src/Slider.jsx +++ b/src/Slider.jsx @@ -125,7 +125,7 @@ class Slider extends React.Component { const diffValue = diffPosition / this.getSliderLength() * (props.max - props.min); const value = this.trimAlignValue(this.startValue + diffValue); - const oldValue = state[state.handle]; + const oldValue = state.bounds[state.handle]; if (value === oldValue) return; const nextBounds = [...state.bounds]; diff --git a/tests/index.js b/tests/index.js index 325139c..60c054f 100644 --- a/tests/index.js +++ b/tests/index.js @@ -207,4 +207,29 @@ describe('rc-slider', function test() { const slider = ReactDOM.render(, div); expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-vertical').length).to.be(1); }); + + it('should not call onChange when value is the same', () => { + const values = []; + const handler = (e) => { + values.push(e); + }; + + ReactDOM.render(, div); + const handle = div.querySelector('.rc-slider-handle'); + + const down = document.createEvent('MouseEvent'); + down.initEvent('mousedown', true, true); + + const move = document.createEvent('MouseEvent'); + move.initEvent('mousemove', true, true); + + const up = document.createEvent('MouseEvent'); + up.initEvent('mouseup', true, true); + + handle.dispatchEvent(down); + handle.dispatchEvent(move); + handle.dispatchEvent(up); + + expect(values.length).to.be(0); + }); });