@@ -220,14 +220,7 @@ describe('react-draggable', function () {
220220 var node = ReactDOM . findDOMNode ( drag ) ;
221221
222222 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 ) ;
231224 TestUtils . Simulate . mouseUp ( node ) ;
232225
233226 var transform = node . getAttribute ( 'transform' ) ;
@@ -353,6 +346,48 @@ describe('react-draggable', function () {
353346 } ) ;
354347 } ) ;
355348
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+
356391 describe ( 'validation' , function ( ) {
357392 it ( 'should result with invariant when there isn\'t a child' , function ( ) {
358393 drag = ( < Draggable /> ) ;
@@ -385,3 +420,15 @@ describe('react-draggable', function () {
385420function renderToHTML ( component ) {
386421 return ReactDOM . findDOMNode ( TestUtils . renderIntoDocument ( component ) ) . outerHTML ;
387422}
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