mirror of
https://github.com/wassname/slider.git
synced 2026-09-12 12:50:45 +08:00
fix: onChange which fired on every mousmove, added test (#145)
This commit is contained in:
committed by
Benjy Cui
parent
abdb297c16
commit
1568019a59
+1
-1
@@ -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];
|
||||
|
||||
@@ -207,4 +207,29 @@ describe('rc-slider', function test() {
|
||||
const slider = ReactDOM.render(<Slider vertical />, 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(<Slider onChange={handler}/>, 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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user