-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathdraggable.spec.jsx
1044 lines (888 loc) · 35.7 KB
/
draggable.spec.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*eslint no-unused-vars:0, no-console:0*/
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';
import ShallowRenderer from 'react-test-renderer/shallow';
import Draggable, {DraggableCore} from '../lib/Draggable';
import FrameComponent from 'react-frame-component';
import assert from 'assert';
import _ from 'lodash';
import {getPrefix, browserPrefixToKey, browserPrefixToStyle} from '../lib/utils/getPrefix';
const transformStyle = browserPrefixToStyle('transform', getPrefix('transform'));
const transformKey = browserPrefixToKey('transform', getPrefix('transform'));
const userSelectStyle = browserPrefixToStyle('user-select', getPrefix('user-select'));
describe('react-draggable', function () {
var drag;
// Remove body margin so offsetParent calculations work properly
beforeAll(function() {
const styleNode = document.createElement('style');
// browser detection (based on prototype.js)
const styleText = document.createTextNode('body {margin: 0;}');
styleNode.appendChild(styleText);
document.getElementsByTagName('head')[0].appendChild(styleNode);
});
beforeEach(function() {
spyOn(console, 'error');
});
afterEach(function() {
try {
TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag)); // reset user-select
ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(drag).parentNode);
} catch(e) { return; }
});
describe('props', function () {
it('should have default properties', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
assert.equal(drag.props.axis, 'both');
assert(drag.props.bounds == false);
assert.equal(typeof drag.props.onStart, 'function');
assert.equal(typeof drag.props.onDrag, 'function');
assert.equal(typeof drag.props.onStop, 'function');
});
it('should pass style and className properly from child', function () {
drag = (<Draggable><div className="foo" style={{color: 'black'}}/></Draggable>);
const node = renderToNode(drag);
// Touch-action hack has been removed
if ('touchAction' in document.body.style) {
assert.equal(node.getAttribute('style').indexOf('touch-action: none'), -1);
}
assert(node.getAttribute('style').indexOf('color: black') >= 0);
assert(new RegExp(transformStyle + ': translate\\(0px(?:, 0px)?\\)').test(node.getAttribute('style')));
assert.equal(node.getAttribute('class'), 'foo react-draggable');
});
it('should set the appropriate custom className when dragging or dragged', function () {
drag = TestUtils.renderIntoDocument(
<Draggable
defaultClassName='foo'
defaultClassNameDragging='bar'
defaultClassNameDragged='baz'
>
<div/>
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
assert(node.getAttribute('class').indexOf('foo') >= 0);
TestUtils.Simulate.mouseDown(node);
assert(node.getAttribute('class').indexOf('bar') >= 0);
TestUtils.Simulate.mouseUp(node);
assert(node.getAttribute('class').indexOf('baz') >= 0);
});
// NOTE: this runs a shallow renderer, which DOES NOT actually render <DraggableCore>
it('should pass handle on to <DraggableCore>', function () {
drag = <Draggable handle=".foo"><div /></Draggable>;
const renderer = new ShallowRenderer();
renderer.render(drag);
const output = renderer.getRenderOutput();
const expected = (
<DraggableCore handle=".foo">
<div
className="react-draggable"
style={{
[transformKey]: 'translate(0px, 0px)'
}}
transform={null} />
</DraggableCore>
);
// Not easy to actually test equality here. The functions are bound as static props so we can't test those easily.
const toOmit = ['onStart', 'onStop', 'onDrag', 'onMouseDown', 'children'];
assert.deepEqual(
_.omit(output.props, toOmit),
_.omit(expected.props, toOmit)
);
});
it('should honor props', function () {
function handleStart() {}
function handleDrag() {}
function handleStop() {}
drag = TestUtils.renderIntoDocument(
<Draggable
axis="y"
handle=".handle"
cancel=".cancel"
grid={[10, 10]}
onStart={handleStart}
onDrag={handleDrag}
onStop={handleStop}>
<div className="foo bar">
<div className="handle"/>
<div className="cancel"/>
</div>
</Draggable>
);
assert.equal(drag.props.axis, 'y');
assert.equal(drag.props.handle, '.handle');
assert.equal(drag.props.cancel, '.cancel');
assert(_.isEqual(drag.props.grid, [10, 10]));
assert.equal(drag.props.onStart, handleStart);
assert.equal(drag.props.onDrag, handleDrag);
assert.equal(drag.props.onStop, handleStop);
});
it('should adjust draggable data output when `scale` prop supplied', function () {
function onDrag(event, data) {
assert.equal(data.x, 200);
assert.equal(data.y, 200);
assert.equal(data.deltaX, 200);
assert.equal(data.deltaY, 200);
}
drag = TestUtils.renderIntoDocument(
<Draggable
scale={0.5}
onDrag={onDrag}>
<div />
</Draggable>
);
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should throw when setting className', function () {
drag = (<Draggable className="foo"><span /></Draggable>);
TestUtils.renderIntoDocument(drag);
expect(
console.error.calls.argsFor(0)[0].replace('propType:', 'prop type:').split('\n')[0]
).toBe(
'Warning: Failed prop type: Invalid prop className passed to Draggable - do not set this, set it on the child.'
);
});
it('should throw when setting style', function () {
drag = (<Draggable style={{color: 'red'}}><span /></Draggable>);
TestUtils.renderIntoDocument(drag);
expect(
console.error.calls.argsFor(0)[0].replace('propType:', 'prop type:').split('\n')[0]
).toBe(
'Warning: Failed prop type: Invalid prop style passed to Draggable - do not set this, set it on the child.'
);
});
it('should throw when setting transform', function () {
drag = (<Draggable transform="translate(100, 100)"><span /></Draggable>);
TestUtils.renderIntoDocument(drag);
expect(
console.error.calls.argsFor(0)[0].replace('propType:', 'prop type:').split('\n')[0]
).toBe(
'Warning: Failed prop type: Invalid prop transform passed to Draggable - do not set this, set it on the child.'
);
});
it('should call onStart when dragging begins', function () {
let called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onStart={function () { called = true; }}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
assert.equal(called, true);
});
it('should call onStop when dragging ends', function () {
let called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onStop={function () { called = true; }}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag));
assert.equal(called, true);
});
it('should not call onStart when dragging begins and disabled', function () {
let called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onStart={function () { called = true; }} disabled={true}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
assert.equal(called, false);
});
it('should immediately call onStop if onDrag returns false', function () {
let called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function () { return false; }} onStop={function () { called = true; }}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
assert.equal(called, false);
mouseMove(10, 10);
assert.equal(called, true);
assert.equal(drag.state.x, 0);
assert.equal(drag.state.y, 0);
});
it('should render with style translate() for DOM nodes', function () {
let dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }}>
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
const style = node.getAttribute('style');
assert.equal(dragged, true);
assert(style.indexOf('transform: translate(100px, 100px);') >= 0);
});
it('should render with positionOffset set as string transform and handle subsequent translate() for DOM nodes', function () {
let dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable positionOffset={{x: '10%', y: '10%'}} onDrag={function() { dragged = true; }}>
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
const style = node.getAttribute('style');
assert.equal(dragged, true);
assert(style.indexOf('translate(10%, 10%) translate(100px, 100px);') >= 0);
});
it('should honor "x" axis', function () {
let dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }} axis="x">
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
const style = node.getAttribute('style');
assert.equal(dragged, true);
assert(/transform: translate\(100px(?:, 0px)?\);/.test(style));
});
it('should honor "y" axis', function () {
let dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }} axis="y">
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
const style = node.getAttribute('style');
assert.equal(dragged, true);
assert(style.indexOf('transform: translate(0px, 100px);') >= 0);
});
it('should honor "none" axis', function () {
let dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }} axis="none">
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
const style = node.getAttribute('style');
assert.equal(dragged, true);
assert(/transform: translate\(0px(?:, 0px)?\);/.test(style));
});
it('should detect if an element is instanceof SVGElement and set state.isElementSVG to true', function() {
drag = TestUtils.renderIntoDocument(
<Draggable>
<svg />
</Draggable>
);
assert.equal(drag.state.isElementSVG, true);
});
it('should detect if an element is NOT an instanceof SVGElement and set state.isElementSVG to false', function() {
drag = TestUtils.renderIntoDocument(
<Draggable>
<div />
</Draggable>
);
assert.equal(drag.state.isElementSVG, false);
});
it('should render with transform translate() for SVG nodes', function () {
drag = TestUtils.renderIntoDocument(
<Draggable>
<svg />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
const transform = node.getAttribute('transform');
assert(transform.indexOf('translate(100,100)') >= 0);
});
it('should add and remove transparent selection class', function () {
drag = TestUtils.renderIntoDocument(
<Draggable>
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
});
it('should not add and remove transparent selection class when disabled', function () {
drag = TestUtils.renderIntoDocument(
<Draggable enableUserSelectHack={false}>
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
});
it('should not add and remove transparent selection class when onStart returns false', function () {
function onStart() { return false; }
drag = TestUtils.renderIntoDocument(
<Draggable onStart={onStart}>
<div />
</Draggable>
);
const node = ReactDOM.findDOMNode(drag);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
});
it('should not defocus inputs when unmounting', function () {
// Have only successfully gotten this to run on Chrome unfortunately, otherwise the initial
// select does not work.
// As of April 2020 we have verified this works in other browsers manually
if (!/Chrome/.test(window.navigator.userAgent)) {
return pending();
}
class TestCase extends React.Component {
constructor() {
super();
this.state = {text: false};
}
render() {
return (
<div>
<input type="text" onChange={(e) => this.setState({text: e.target.value})} size={5} />
{!this.state.text && (
<Draggable>
<div />
</Draggable>
)}
</div>
);
}
}
drag = TestUtils.renderIntoDocument(<TestCase/>);
const dragEl = ReactDOM.findDOMNode(drag);
// Need to append to a real document to test focus/selection, can't just be a fragment
document.body.appendChild(dragEl);
const input = dragEl.querySelector('input');
input.focus();
assert.equal(window.getSelection().type, 'Caret', 'Element should be focused before draggable unmounts');
TestUtils.Simulate.keyDown(input, {key: 'a', keyCode: 65, which: 65});
assert.equal(window.getSelection().type, 'Caret', 'Element should be focused after draggable unmounts');
document.body.removeChild(dragEl);
});
it('should be draggable when in an iframe', function (done) {
let dragged = false;
const dragElement = (
<Draggable onDrag={function() { dragged = true; }}>
<div />
</Draggable>
);
const renderRoot = document.body.appendChild(document.createElement('div'));
const ref = React.createRef();
const frame = ReactDOM.render(<FrameComponent ref={ref}>{ dragElement }</FrameComponent>, renderRoot);
setTimeout(function checkIframe() {
const iframeDoc = ref.current?.contentDocument;
if (!iframeDoc) return setTimeout(checkIframe, 50);
const node = iframeDoc.querySelector('.react-draggable');
if (!node) return setTimeout(checkIframe, 50);
simulateMovementFromTo(node, 0, 0, 100, 100);
const style = node.getAttribute('style');
assert.equal(dragged, true);
assert(style.indexOf('transform: translate(100px, 100px);') >= 0);
renderRoot.parentNode.removeChild(renderRoot);
done();
}, 0);
});
it('should add and remove transparent selection class to iframe\'s body when in an iframe', function (done) {
const dragElement = (
<Draggable>
<div />
</Draggable>
);
const renderRoot = document.body.appendChild(document.createElement('div'));
const ref = React.createRef();
ReactDOM.render(<FrameComponent ref={ref}>{ dragElement }</FrameComponent>, renderRoot);
setTimeout(function checkIframe() {
const iframeDoc = ref.current?.contentDocument;
if (!iframeDoc) return setTimeout(checkIframe, 50);
const node = iframeDoc.querySelector('.react-draggable');
if (!node) return setTimeout(checkIframe, 50);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
assert(!iframeDoc.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
assert(iframeDoc.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
assert(!iframeDoc.body.classList.contains('react-draggable-transparent-selection'));
renderRoot.parentNode.removeChild(renderRoot);
done();
}, 0);
});
});
describe('interaction', function () {
function mouseDownOn(drag, selector, shouldDrag) {
resetDragging(drag);
const node = ReactDOM.findDOMNode(drag).querySelector(selector);
if (!node) throw new Error(`Selector not found: ${selector}`);
TestUtils.Simulate.mouseDown(node);
assert.equal(drag.state.dragging, shouldDrag);
}
function resetDragging(drag) {
TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag));
assert.equal(drag.state.dragging, false);
}
it('should initialize dragging onmousedown', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
assert.equal(drag.state.dragging, true);
});
it('should only initialize dragging onmousedown of handle', function () {
drag = TestUtils.renderIntoDocument(
<Draggable handle=".handle">
<div>
<div className="handle">Handle</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
mouseDownOn(drag, '.content', false);
mouseDownOn(drag, '.handle', true);
});
it('should only initialize dragging onmousedown of handle, even if children fire event', function () {
drag = TestUtils.renderIntoDocument(
<Draggable handle=".handle">
<div>
<div className="handle">
<div><span><div className="deep">Handle</div></span></div>
</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
mouseDownOn(drag, '.content', false);
mouseDownOn(drag, '.deep', true);
mouseDownOn(drag, '.handle > div', true);
mouseDownOn(drag, '.handle span', true);
mouseDownOn(drag, '.handle', true);
});
it('should not initialize dragging onmousedown of cancel', function () {
drag = TestUtils.renderIntoDocument(
<Draggable cancel=".cancel">
<div>
<div className="cancel">Cancel</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
mouseDownOn(drag, '.cancel', false);
mouseDownOn(drag, '.content', true);
});
it('should not initialize dragging onmousedown of handle, even if children fire event', function () {
drag = TestUtils.renderIntoDocument(
<Draggable cancel=".cancel">
<div>
<div className="cancel">
<div><span><div className="deep">Cancel</div></span></div>
</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
mouseDownOn(drag, '.content', true);
mouseDownOn(drag, '.deep', false);
mouseDownOn(drag, '.cancel > div', false);
mouseDownOn(drag, '.cancel span', false);
mouseDownOn(drag, '.cancel', false);
});
it('should discontinue dragging onmouseup', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
assert.equal(drag.state.dragging, true);
resetDragging(drag);
});
it('should initialize dragging ontouchstart', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
// Need to dispatch this ourselves as there is no onTouchStart handler (due to passive)
// so TestUtils.Simulate will not work
const e = new Event('touchstart');
ReactDOM.findDOMNode(drag).dispatchEvent(e);
assert.equal(drag.state.dragging, true);
});
it('should call preventDefault on touchStart event', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
const e = new Event('touchstart');
// Oddly `e.defaultPrevented` is not changing here. Maybe because we're not mounted to a real doc?
let pdCalled = false;
e.preventDefault = function() { pdCalled = true; };
ReactDOM.findDOMNode(drag).dispatchEvent(e);
assert(pdCalled);
assert.equal(drag.state.dragging, true);
});
it('should *not* call preventDefault on touchStart event if "allowMobileScroll"', function () {
drag = TestUtils.renderIntoDocument(<Draggable allowMobileScroll={true}><div/></Draggable>);
const e = new Event('touchstart');
// Oddly `e.defaultPrevented` is not changing here. Maybe because we're not mounted to a real doc?
let pdCalled = false;
e.preventDefault = function() { pdCalled = true; };
ReactDOM.findDOMNode(drag).dispatchEvent(e);
assert(!pdCalled);
assert.equal(drag.state.dragging, true);
});
it('should not call preventDefault on touchStart event if not on handle', function () {
drag = TestUtils.renderIntoDocument(
<Draggable handle=".handle">
<div>
<div className="handle">
<div><span><div className="deep">Handle</div></span></div>
</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
const e = new Event('touchstart');
let pdCalled = false;
e.preventDefault = function() { pdCalled = true; };
ReactDOM.findDOMNode(drag).querySelector('.content').dispatchEvent(e);
assert(!pdCalled);
assert(drag.state.dragging !== true);
});
it('should modulate position on scroll', function (done) {
let dragCalled = false;
function onDrag(e, coreEvent) {
assert.equal(Math.round(coreEvent.deltaY), 500);
dragCalled = true;
}
drag = TestUtils.renderIntoDocument(<Draggable onDrag={onDrag}><div/></Draggable>);
const node = ReactDOM.findDOMNode(drag);
// Create a container we can scroll. I'm doing it this way so we can still access <Draggable>.
// Enzyme (airbnb project) would make this a lot easier.
const fragment = fragmentFromString(`
<div style="overflow: auto; height: 100px;">
<div style="height: 10000px; position: relative;">
</div>
</div>
`);
transplantNodeInto(node, fragment, (f) => f.children[0]);
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert.equal(drag.state.dragging, true);
// Scroll the inner container & trigger a scroll
fragment.scrollTop = 500;
mouseMove(0, 0);
TestUtils.Simulate.mouseUp(node);
setTimeout(function() {
assert.equal(drag.state.dragging, false);
assert.equal(dragCalled, true);
assert.equal(drag.state.y, 500);
// Cleanup
document.body.removeChild(fragment);
done();
}, 50);
});
it('should respect offsetParent on nested div scroll', function (done) {
let dragCalled = false;
function onDrag(e, coreEvent) {
dragCalled = true;
// Because the offsetParent is the body, we technically haven't moved at all relative to it
assert.equal(coreEvent.deltaY, 0);
}
drag = TestUtils.renderIntoDocument(<Draggable onDrag={onDrag} offsetParent={document.body}><div/></Draggable>);
const node = ReactDOM.findDOMNode(drag);
// Create a container we can scroll. I'm doing it this way so we can still access <Draggable>.
// Enzyme (airbnb project) would make this a lot easier.
const fragment = fragmentFromString(`
<div style="overflow: auto; height: 100px;">
<div style="height: 10000px; position: relative;">
</div>
</div>
`);
transplantNodeInto(node, fragment, (f) => f.children[0]);
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
fragment.scrollTop = 500;
mouseMove(0, 0);
TestUtils.Simulate.mouseUp(node);
setTimeout(function() {
assert(dragCalled);
// Cleanup
document.body.removeChild(fragment);
done();
}, 50);
});
});
describe('draggable callbacks', function () {
it('should call back on drag', function (done) {
function onDrag(event, data) {
assert.equal(data.x, 100);
assert.equal(data.y, 100);
assert.equal(data.deltaX, 100);
assert.equal(data.deltaY, 100);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag}>
<div />
</Draggable>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with correct dom node with nodeRef', function (done) {
function onDrag(event, data) {
// Being tricky here and installing the ref on the inner child, to ensure it's working
// and not just falling back on ReactDOM.findDOMNode()
assert.equal(data.node, ReactDOM.findDOMNode(drag).firstChild);
done();
}
const nodeRef = React.createRef();
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag} nodeRef={nodeRef}>
<span>
<div ref={nodeRef} />
</span>
</Draggable>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with correct dom node with nodeRef (forwardRef)', function (done) {
const Component1 = React.forwardRef(function (props, ref) {
return <div {...props} ref={ref}>Nested component</div>;
});
function onDrag(event, data) {
assert.equal(data.node, ReactDOM.findDOMNode(drag));
assert.equal(data.node.innerText, 'Nested component');
done();
}
const nodeRef = React.createRef();
drag = TestUtils.renderIntoDocument(
<DraggableCore onDrag={onDrag} nodeRef={nodeRef}>
<Component1 ref={nodeRef} />
</DraggableCore>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back on drag, with values within the defined bounds', function(done){
function onDrag(event, data) {
assert.equal(data.x, 90);
assert.equal(data.y, 90);
assert.equal(data.deltaX, 90);
assert.equal(data.deltaY, 90);
done();
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag} bounds={{left: 0, right: 90, top: 0, bottom: 90}}>
<div />
</Draggable>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with offset left/top, not client', function(done) {
function onDrag(event, data) {
assert.equal(data.x, 100);
assert.equal(data.y, 100);
assert.equal(data.deltaX, 100);
assert.equal(data.deltaY, 100);
done();
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag} >
<div style={{position: 'relative', top: '200px', left: '200px'}} />
</Draggable>
);
simulateMovementFromTo(drag, 200, 200, 300, 300);
});
it('should call back with correct position when parent element is 2x scaled', function(done) {
function onDrag(event, data) {
// visually it will look like 100, because parent is 2x scaled
assert.equal(data.x, 50);
assert.equal(data.y, 50);
assert.equal(data.deltaX, 50);
assert.equal(data.deltaY, 50);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag} scale={2}>
<div />
</Draggable>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with correct position when parent element is 0.5x scaled', function(done) {
function onDrag(event, data) {
// visually it will look like 100, because parent is 0.5x scaled
assert.equal(data.x, 200);
assert.equal(data.y, 200);
assert.equal(data.deltaX, 200);
assert.equal(data.deltaY, 200);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag} scale={0.5}>
<div />
</Draggable>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should not throw an error if unmounted during a callback', function () {
function App(props) {
const [firstVisible, setFirstVisible] = React.useState(true);
// Callback with ref to draggable
const dragRef = React.useRef(null);
props.draggableRefCb(dragRef);
return (
<div className="App">
<button onClick={() => setFirstVisible(true)}>Show draggables</button>
{firstVisible && (
<Draggable onStop={() => setFirstVisible(false)} ref={dragRef}>
<h2>1. Drag me!</h2>
</Draggable>
)}
</div>
);
}
let dragRef;
const appContainer = TestUtils.renderIntoDocument(
<App draggableRefCb={(_ref) => {dragRef = _ref;}}/>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(dragRef.current, 0, 0, 100, 100);
// ok, was a setstate warning thrown?
// Assert unmounted
assert.equal(dragRef.current, null);
});
});
describe('DraggableCore callbacks', function () {
it('should call back with node on drag', function(done) {
function onDrag(event, data) {
assert.equal(data.x, 100);
assert.equal(data.y, 100);
assert.equal(data.deltaX, 100);
assert.equal(data.deltaY, 100);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<DraggableCore onDrag={onDrag}>
<div />
</DraggableCore>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with correct position when parent element is 2x scaled', function(done) {
function onDrag(event, data) {
// visually it will look like 100, because parent is 2x scaled
assert.equal(data.x, 50);
assert.equal(data.y, 50);
assert.equal(data.deltaX, 50);
assert.equal(data.deltaY, 50);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<DraggableCore onDrag={onDrag} scale={2}>
<div />
</DraggableCore>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with correct position when parent element is 0.5x scaled', function(done) {
function onDrag(event, data) {
// visually it will look like 100, because parent is 0.5x scaled
assert.equal(data.x, 200);
assert.equal(data.y, 200);
assert.equal(data.deltaX, 200);
assert.equal(data.deltaY, 200);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<DraggableCore onDrag={onDrag} scale={0.5}>
<div />
</DraggableCore>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with snapped data output when grid prop is provided', function(done) {
function onDrag(event, data) {
assert.equal(data.x, 99);
assert.equal(data.y, 96);
assert.equal(data.deltaX, 99);
assert.equal(data.deltaY, 96);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
}
function onStop(event, data) {
assert.equal(data.x, 99);
assert.equal(data.y, 96);
// Single drag-and-stop so stop {x, y} is same as drag {x, y}.
assert.equal(data.deltaX, 0);
assert.equal(data.deltaY, 0);
assert.equal(data.node, ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<DraggableCore onDrag={onDrag} onStop={onStop} grid={[9, 16]}>
<div />
</DraggableCore>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
});
describe('validation', function () {
it('should result with invariant when there isn\'t a child', function () {
const renderer = new ShallowRenderer();
assert.throws(() => renderer.render(<Draggable />));
});
it('should result with invariant if there\'s more than a single child', function () {
const renderer = new ShallowRenderer();
assert.throws(() => renderer.render(<Draggable><div/><div/></Draggable>));
});