@@ -456,32 +456,74 @@ describe('react-draggable', function () {
456
456
} ) ;
457
457
458
458
it ( 'should modulate position on scroll' , function ( done ) {
459
- // This test fails in karma under Chrome & Firefox, positioning quirks
460
- // FIXME: Why? Chrome reports 2x scrollTo, Phantom reports 0x, Firefox reports 1x as it should
461
- var is_ff = navigator . userAgent . toLowerCase ( ) . indexOf ( 'Firefox' ) > - 1 ;
462
- if ( ! is_ff ) return done ( ) ;
463
-
464
- var dragCalled = false ;
459
+ let dragCalled = false ;
465
460
466
461
function onDrag ( e , coreEvent ) {
467
462
assert ( coreEvent . deltaY === 500 ) ;
468
463
dragCalled = true ;
469
464
}
470
465
drag = TestUtils . renderIntoDocument ( < Draggable onDrag = { onDrag } > < div /> </ Draggable > ) ;
471
- var node = ReactDOM . findDOMNode ( drag ) ;
466
+ const node = ReactDOM . findDOMNode ( drag ) ;
467
+
468
+ // Create a container we can scroll. I'm doing it this way so we can still access <Draggable>.
469
+ // Enzyme (airbnb project) would make this a lot easier.
470
+ const fragment = fragmentFromString ( `
471
+ <div style="overflow: auto; height: 100px;">
472
+ <div style="height: 10000px; position: relative;">
473
+ </div>
474
+ </div>
475
+ ` ) ;
476
+ transplantNodeInto ( node , fragment , ( f ) => f . children [ 0 ] ) ;
472
477
473
478
TestUtils . Simulate . mouseDown ( node , { clientX : 0 , clientY : 0 } ) ;
474
479
assert ( drag . state . dragging === true ) ;
475
480
476
- document . body . style . height = '10000px' ;
477
- window . scrollTo ( 0 , 500 ) ;
478
- TestUtils . Simulate . mouseUp ( node , { clientX : 0 , clientY : 0 } ) ;
481
+ // Scroll the inner container & trigger a scroll
482
+ fragment . scrollTop = 500 ;
483
+ mouseMove ( 0 , 0 ) ;
484
+ TestUtils . Simulate . mouseUp ( node ) ;
479
485
setTimeout ( function ( ) {
486
+ assert ( drag . state . dragging === false ) ;
480
487
assert ( dragCalled === true ) ;
481
- assert ( drag . state . clientY === 500 ) ;
488
+ assert ( drag . state . y === 500 ) ;
489
+ // Cleanup
490
+ document . body . removeChild ( fragment ) ;
482
491
done ( ) ;
483
492
} , 50 ) ;
493
+
484
494
} ) ;
495
+
496
+ it ( 'should respect offsetParent on nested div scroll' , function ( done ) {
497
+ let dragCalled = false ;
498
+ function onDrag ( e , coreEvent ) {
499
+ dragCalled = true ;
500
+ // Because the offsetParent is the body, we technically haven't moved at all relative to it
501
+ assert ( coreEvent . deltaY === 0 ) ;
502
+ }
503
+ drag = TestUtils . renderIntoDocument ( < Draggable onDrag = { onDrag } offsetParent = { document . body } > < div /> </ Draggable > ) ;
504
+ const node = ReactDOM . findDOMNode ( drag ) ;
505
+
506
+ // Create a container we can scroll. I'm doing it this way so we can still access <Draggable>.
507
+ // Enzyme (airbnb project) would make this a lot easier.
508
+ const fragment = fragmentFromString ( `
509
+ <div style="overflow: auto; height: 100px;">
510
+ <div style="height: 10000px; position: relative;">
511
+ </div>
512
+ </div>
513
+ ` ) ;
514
+ transplantNodeInto ( node , fragment , ( f ) => f . children [ 0 ] ) ;
515
+
516
+ TestUtils . Simulate . mouseDown ( node , { clientX : 0 , clientY : 0 } ) ;
517
+ fragment . scrollTop = 500 ;
518
+
519
+ mouseMove ( 0 , 0 ) ;
520
+ TestUtils . Simulate . mouseUp ( node ) ;
521
+ setTimeout ( function ( ) {
522
+ assert ( dragCalled ) ;
523
+ // Cleanup
524
+ document . body . removeChild ( fragment ) ;
525
+ done ( ) ;
526
+ } , 50 ) ;
485
527
} ) ;
486
528
487
529
describe ( 'draggable callbacks' , function ( ) {
@@ -597,3 +639,15 @@ function simulateMovementFromTo(drag, fromX, fromY, toX, toY) {
597
639
mouseMove ( toX , toY ) ;
598
640
TestUtils . Simulate . mouseUp ( node ) ;
599
641
}
642
+
643
+ function fragmentFromString ( strHTML ) {
644
+ var temp = document . createElement ( 'div' ) ;
645
+ temp . innerHTML = strHTML ;
646
+ return temp . children [ 0 ] ;
647
+ }
648
+
649
+ function transplantNodeInto ( node , into , selector ) {
650
+ node . parentNode . removeChild ( node ) ;
651
+ selector ( into ) . appendChild ( node ) ;
652
+ document . body . appendChild ( into ) ;
653
+ }
0 commit comments