@@ -220,14 +220,7 @@ describe('react-draggable', function () {
220
220
var node = ReactDOM . findDOMNode ( drag ) ;
221
221
222
222
TestUtils . Simulate . mouseDown ( node , { clientX : 0 , clientY : 0 } ) ;
223
- // Simulate a movement; can't use TestUtils here because it uses react's event system only,
224
- // but <DraggableCore> attaches event listeners directly to the document.
225
- // Would love to new MouseEvent() here but it doesn't work with PhantomJS / old browsers.
226
- // var e = new MouseEvent('mousemove', {clientX: 100, clientY: 100});
227
- var evt = document . createEvent ( 'MouseEvents' ) ;
228
- evt . initMouseEvent ( 'mousemove' , true , true , window ,
229
- 0 , 0 , 0 , 100 , 100 , false , false , false , false , 0 , null ) ;
230
- document . dispatchEvent ( evt ) ;
223
+ mouseMove ( node , 100 , 100 ) ;
231
224
TestUtils . Simulate . mouseUp ( node ) ;
232
225
233
226
var transform = node . getAttribute ( 'transform' ) ;
@@ -353,6 +346,48 @@ describe('react-draggable', function () {
353
346
} ) ;
354
347
} ) ;
355
348
349
+ describe ( 'draggable callbacks' , function ( ) {
350
+ it ( 'should call back on drag' , function ( ) {
351
+ function onDrag ( event , data ) {
352
+ expect ( data . position . left ) . toEqual ( 100 ) ;
353
+ expect ( data . position . top ) . toEqual ( 100 ) ;
354
+ expect ( data . deltaX ) . toEqual ( 100 ) ;
355
+ expect ( data . deltaY ) . toEqual ( 100 ) ;
356
+ }
357
+ drag = TestUtils . renderIntoDocument (
358
+ < Draggable onDrag = { onDrag } >
359
+ < div />
360
+ </ Draggable >
361
+ ) ;
362
+
363
+ var node = ReactDOM . findDOMNode ( drag ) ;
364
+
365
+ TestUtils . Simulate . mouseDown ( node , { clientX : 0 , clientY : 0 } ) ;
366
+ var moveEvt = mouseMove ( node , 100 , 100 ) ;
367
+ TestUtils . Simulate . mouseUp ( node ) ;
368
+ } ) ;
369
+
370
+ it ( 'should call back with offset left/top, not client' , function ( ) {
371
+ function onDrag ( event , data ) {
372
+ expect ( data . position . left ) . toEqual ( 100 ) ;
373
+ expect ( data . position . top ) . toEqual ( 100 ) ;
374
+ expect ( data . deltaX ) . toEqual ( 100 ) ;
375
+ expect ( data . deltaY ) . toEqual ( 100 ) ;
376
+ }
377
+ drag = TestUtils . renderIntoDocument (
378
+ < Draggable onDrag = { onDrag } style = { { position : 'relative' , top : '200px' , left : '200px' } } >
379
+ < div />
380
+ </ Draggable >
381
+ ) ;
382
+
383
+ var node = ReactDOM . findDOMNode ( drag ) ;
384
+
385
+ TestUtils . Simulate . mouseDown ( node , { clientX : 200 , clientY : 200 } ) ;
386
+ var moveEvt = mouseMove ( node , 300 , 300 ) ;
387
+ TestUtils . Simulate . mouseUp ( node ) ;
388
+ } ) ;
389
+ } ) ;
390
+
356
391
describe ( 'validation' , function ( ) {
357
392
it ( 'should result with invariant when there isn\'t a child' , function ( ) {
358
393
drag = ( < Draggable /> ) ;
@@ -385,3 +420,15 @@ describe('react-draggable', function () {
385
420
function renderToHTML ( component ) {
386
421
return ReactDOM . findDOMNode ( TestUtils . renderIntoDocument ( component ) ) . outerHTML ;
387
422
}
423
+
424
+ // Simulate a movement; can't use TestUtils here because it uses react's event system only,
425
+ // but <DraggableCore> attaches event listeners directly to the document.
426
+ // Would love to new MouseEvent() here but it doesn't work with PhantomJS / old browsers.
427
+ // var e = new MouseEvent('mousemove', {clientX: 100, clientY: 100});
428
+ function mouseMove ( node , x , y ) {
429
+ var evt = document . createEvent ( 'MouseEvents' ) ;
430
+ evt . initMouseEvent ( 'mousemove' , true , true , window ,
431
+ 0 , 0 , 0 , x , y , false , false , false , false , 0 , null ) ;
432
+ document . dispatchEvent ( evt ) ;
433
+ return evt ;
434
+ }
0 commit comments