@@ -3,6 +3,7 @@ import React from 'react';
33import ReactDOM from 'react-dom' ;
44import TestUtils from 'react/lib/ReactTestUtils' ;
55import Draggable , { DraggableCore } from '../index' ;
6+ import FrameComponent from 'react-frame-component' ;
67import assert from 'power-assert' ;
78import _ from 'lodash' ;
89import { getPrefix , browserPrefixToKey , browserPrefixToStyle } from '../lib/utils/getPrefix' ;
@@ -289,7 +290,7 @@ describe('react-draggable', function () {
289290 } ) ;
290291
291292 it ( 'should detect if an element is instanceof SVGElement and set state.isElementSVG to true' , function ( ) {
292- drag = TestUtils . renderIntoDocument (
293+ drag = TestUtils . renderIntoDocument (
293294 < Draggable >
294295 < svg />
295296 </ Draggable >
@@ -299,7 +300,7 @@ describe('react-draggable', function () {
299300 } ) ;
300301
301302 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 (
303304 < Draggable >
304305 < div />
305306 </ Draggable >
@@ -374,6 +375,60 @@ describe('react-draggable', function () {
374375 TestUtils . Simulate . mouseUp ( node ) ;
375376 assert ( document . body . getAttribute ( 'style' ) === '' ) ;
376377 } ) ;
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+ } ) ;
377432 } ) ;
378433
379434 describe ( 'interaction' , function ( ) {
@@ -477,7 +532,7 @@ describe('react-draggable', function () {
477532 let dragCalled = false ;
478533
479534 function onDrag ( e , coreEvent ) {
480- assert ( coreEvent . deltaY === 500 ) ;
535+ assert ( Math . round ( coreEvent . deltaY ) === 500 ) ;
481536 dragCalled = true ;
482537 }
483538 drag = TestUtils . renderIntoDocument ( < Draggable onDrag = { onDrag } > < div /> </ Draggable > ) ;
@@ -642,11 +697,12 @@ function renderToNode(component) {
642697// but <DraggableCore> attaches event listeners directly to the document.
643698// Would love to new MouseEvent() here but it doesn't work with PhantomJS / old browsers.
644699// 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' ) ;
647703 evt . initMouseEvent ( 'mousemove' , true , true , window ,
648704 0 , 0 , 0 , x , y , false , false , false , false , 0 , null ) ;
649- document . dispatchEvent ( evt ) ;
705+ doc . dispatchEvent ( evt ) ;
650706 return evt ;
651707}
652708
@@ -655,7 +711,7 @@ function simulateMovementFromTo(drag, fromX, fromY, toX, toY) {
655711 const node = ReactDOM . findDOMNode ( drag ) ;
656712
657713 TestUtils . Simulate . mouseDown ( node , { clientX : fromX , clientY : fromX } ) ;
658- mouseMove ( toX , toY ) ;
714+ mouseMove ( toX , toY , node ) ;
659715 TestUtils . Simulate . mouseUp ( node ) ;
660716}
661717
0 commit comments