Skip to content

Commit a7a35b2

Browse files
acustiSTRML
authored andcommitted
✅ Add draggable in iframe tests (#182)
I was unable to get these tests working using TestUtils’ renderIntoDocument function (or the various find* helpers) for these tests and had to use actual ReactDOM.render instead along with regular querySelector. Has something to do with the tricky parts of react-frame-component (rendering an iframe with React).
1 parent 53e8230 commit a7a35b2

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"pre-commit": "^1.1.3",
5656
"react": "^15.2.1",
5757
"react-dom": "^15.2.1",
58+
"react-frame-component": "0.6.2",
5859
"semver": "^5.3.0",
5960
"static-server": "^2.0.3",
6061
"uglify-js": "^2.7.0",

specs/draggable.spec.jsx

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import ReactDOM from 'react-dom';
44
import TestUtils from 'react/lib/ReactTestUtils';
55
import Draggable, {DraggableCore} from '../index';
6+
import FrameComponent from 'react-frame-component';
67
import assert from 'power-assert';
78
import _ from 'lodash';
89
import {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

Comments
 (0)