@@ -3,6 +3,7 @@ import React from 'react';
3
3
import ReactDOM from 'react-dom' ;
4
4
import TestUtils from 'react/lib/ReactTestUtils' ;
5
5
import Draggable , { DraggableCore } from '../index' ;
6
+ import FrameComponent from 'react-frame-component' ;
6
7
import assert from 'power-assert' ;
7
8
import _ from 'lodash' ;
8
9
import { getPrefix , browserPrefixToKey , browserPrefixToStyle } from '../lib/utils/getPrefix' ;
@@ -289,7 +290,7 @@ describe('react-draggable', function () {
289
290
} ) ;
290
291
291
292
it ( 'should detect if an element is instanceof SVGElement and set state.isElementSVG to true' , function ( ) {
292
- drag = TestUtils . renderIntoDocument (
293
+ drag = TestUtils . renderIntoDocument (
293
294
< Draggable >
294
295
< svg />
295
296
</ Draggable >
@@ -299,7 +300,7 @@ describe('react-draggable', function () {
299
300
} ) ;
300
301
301
302
it ( 'should detect if an element is NOT an instanceof SVGElement and set state.isElementSVG to false' , function ( ) {
302
- drag = TestUtils . renderIntoDocument (
303
+ drag = TestUtils . renderIntoDocument (
303
304
< Draggable >
304
305
< div />
305
306
</ Draggable >
@@ -374,6 +375,60 @@ describe('react-draggable', function () {
374
375
TestUtils . Simulate . mouseUp ( node ) ;
375
376
assert ( document . body . getAttribute ( 'style' ) === '' ) ;
376
377
} ) ;
378
+
379
+ it ( 'should be draggable when in an iframe' , function ( done ) {
380
+ let dragged = false ;
381
+ const dragElement = (
382
+ < Draggable onDrag = { function ( ) { dragged = true ; } } >
383
+ < div />
384
+ </ Draggable >
385
+ ) ;
386
+ const renderRoot = document . body . appendChild ( document . createElement ( 'div' ) ) ;
387
+ const frame = ReactDOM . render ( < FrameComponent > { dragElement } </ FrameComponent > , renderRoot ) ;
388
+
389
+ setTimeout ( ( ) => {
390
+ const body = ReactDOM . findDOMNode ( frame ) . contentDocument . body ;
391
+ const node = body . querySelector ( '.react-draggable' ) ;
392
+ simulateMovementFromTo ( node , 0 , 0 , 100 , 100 ) ;
393
+
394
+ const style = node . getAttribute ( 'style' ) ;
395
+ assert ( dragged === true ) ;
396
+ assert ( style . indexOf ( 'transform: translate(100px, 100px);' ) >= 0 ) ;
397
+
398
+ renderRoot . parentNode . removeChild ( renderRoot ) ;
399
+ done ( ) ;
400
+ } , 50 ) ;
401
+ } ) ;
402
+
403
+ it ( 'should add and remove user-select styles to iframe’s body when in an iframe' , function ( done ) {
404
+ const userSelectStyleStr = `;${ userSelectStyle } : none;` ;
405
+
406
+ const dragElement = (
407
+ < Draggable onDrag = { function ( ) { dragged = true ; } } >
408
+ < div />
409
+ </ Draggable >
410
+ ) ;
411
+ const renderRoot = document . body . appendChild ( document . createElement ( 'div' ) ) ;
412
+ const frame = ReactDOM . render ( < FrameComponent > { dragElement } </ FrameComponent > , renderRoot ) ;
413
+
414
+ setTimeout ( ( ) => {
415
+ const iframeDoc = ReactDOM . findDOMNode ( frame ) . contentDocument ;
416
+ const node = iframeDoc . querySelector ( '.react-draggable' ) ;
417
+ iframeDoc . body . setAttribute ( 'style' , '' ) ;
418
+
419
+ assert ( iframeDoc . body . getAttribute ( 'style' ) === '' ) ;
420
+ assert ( document . body . getAttribute ( 'style' ) === '' ) ;
421
+ TestUtils . Simulate . mouseDown ( node , { clientX : 0 , clientY : 0 } ) ;
422
+ assert ( iframeDoc . body . getAttribute ( 'style' ) === userSelectStyleStr ) ;
423
+ assert ( document . body . getAttribute ( 'style' ) === '' ) ;
424
+ TestUtils . Simulate . mouseUp ( node ) ;
425
+ assert ( iframeDoc . body . getAttribute ( 'style' ) === '' ) ;
426
+ assert ( document . body . getAttribute ( 'style' ) === '' ) ;
427
+
428
+ renderRoot . parentNode . removeChild ( renderRoot ) ;
429
+ done ( ) ;
430
+ } , 50 ) ;
431
+ } ) ;
377
432
} ) ;
378
433
379
434
describe ( 'interaction' , function ( ) {
@@ -477,7 +532,7 @@ describe('react-draggable', function () {
477
532
let dragCalled = false ;
478
533
479
534
function onDrag ( e , coreEvent ) {
480
- assert ( coreEvent . deltaY === 500 ) ;
535
+ assert ( Math . round ( coreEvent . deltaY ) === 500 ) ;
481
536
dragCalled = true ;
482
537
}
483
538
drag = TestUtils . renderIntoDocument ( < Draggable onDrag = { onDrag } > < div /> </ Draggable > ) ;
@@ -642,11 +697,12 @@ function renderToNode(component) {
642
697
// but <DraggableCore> attaches event listeners directly to the document.
643
698
// Would love to new MouseEvent() here but it doesn't work with PhantomJS / old browsers.
644
699
// var e = new MouseEvent('mousemove', {clientX: 100, clientY: 100});
645
- function mouseMove ( x , y ) {
646
- const evt = document . createEvent ( 'MouseEvents' ) ;
700
+ function mouseMove ( x , y , node ) {
701
+ const doc = node ? node . ownerDocument : document ;
702
+ const evt = doc . createEvent ( 'MouseEvents' ) ;
647
703
evt . initMouseEvent ( 'mousemove' , true , true , window ,
648
704
0 , 0 , 0 , x , y , false , false , false , false , 0 , null ) ;
649
- document . dispatchEvent ( evt ) ;
705
+ doc . dispatchEvent ( evt ) ;
650
706
return evt ;
651
707
}
652
708
@@ -655,7 +711,7 @@ function simulateMovementFromTo(drag, fromX, fromY, toX, toY) {
655
711
const node = ReactDOM . findDOMNode ( drag ) ;
656
712
657
713
TestUtils . Simulate . mouseDown ( node , { clientX : fromX , clientY : fromX } ) ;
658
- mouseMove ( toX , toY ) ;
714
+ mouseMove ( toX , toY , node ) ;
659
715
TestUtils . Simulate . mouseUp ( node ) ;
660
716
}
661
717
0 commit comments