Skip to content

Commit

Permalink
Clean up scroll events on unmount and test scroll modulation.
Browse files Browse the repository at this point in the history
This test is a little brittle, I can only get it to work in Chrome.
  • Loading branch information
STRML committed Nov 14, 2015
1 parent ebeae88 commit 5aaca75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/DraggableCore.es6
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export default class DraggableCore extends React.Component {
removeEvent(document, eventsFor.touch.move, this.handleDrag);
removeEvent(document, eventsFor.mouse.stop, this.handleDragStop);
removeEvent(document, eventsFor.touch.stop, this.handleDragStop);
removeEvent(document, 'scroll', this.handleScroll);
if (this.props.enableUserSelectHack) removeUserSelectStyles();
}

Expand Down Expand Up @@ -370,12 +371,12 @@ export default class DraggableCore extends React.Component {

// Create the usual event, but make the scroll offset our deltas.
let coreEvent = createCoreEvent(this);
coreEvent.deltaX = x - s.scrollX;
coreEvent.deltaY = y - s.scrollY;
coreEvent.position.deltaX = x - s.scrollX;
coreEvent.position.deltaY = y - s.scrollY;

this.setState({
lastX: s.lastX + coreEvent.deltaX,
lastY: s.lastY + coreEvent.deltaY
lastX: s.lastX + coreEvent.position.deltaX,
lastY: s.lastY + coreEvent.position.deltaY
});

this.props.onDrag(e, coreEvent);
Expand Down
30 changes: 28 additions & 2 deletions specs/draggable.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('react-draggable', function () {

afterEach(function() {
try {
ReactDOM.findDOMNode(drag);
drag.componentWillUnmount();
React.unmountComponentAtNode(React.findDOMNode(drag).parentNode);
// TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag));
} catch(e) { return; }
});

Expand Down Expand Up @@ -325,6 +325,32 @@ describe('react-draggable', function () {
TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag));
expect(drag.state.dragging).toEqual(false);
});

it('should modulate position on scroll', function (done) {
// This test fails in karma under PhantomJS & Firefox, scroll event quirks
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (!is_chrome) return done();

var dragCalled = false;

function onDrag(e, coreEvent) {
expect(coreEvent.deltaY).toEqual(500);
dragCalled = true;
}
drag = TestUtils.renderIntoDocument(<Draggable onDrag={onDrag}><div/></Draggable>);
var node = ReactDOM.findDOMNode(drag);

TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag)); // start drag so window listener is up
expect(drag.state.dragging).toEqual(true);

document.body.style.height = '10000px';
window.scrollTo(0, 500);
setTimeout(function() {
expect(dragCalled).toEqual(true);
expect(drag.state.clientY).toEqual(500);
done();
}, 0);
});
});

describe('validation', function () {
Expand Down

0 comments on commit 5aaca75

Please sign in to comment.