22 * draggable_core.js
33 */
44
5- var el , offsetBefore , offsetAfter , dragged ;
5+ TestHelpers . draggable = { } ;
66
7- function drag ( handle , dx , dy ) {
8- offsetBefore = el . offset ( ) ;
7+ TestHelpers . draggable . drag = function ( handle , dx , dy ) {
98 $ ( handle ) . simulate ( "drag" , {
109 dx : dx || 0 ,
1110 dy : dy || 0
1211 } ) ;
13- dragged = { dx : dx , dy : dy } ;
14- offsetAfter = el . offset ( ) ;
15- }
12+ return el . offset ( ) ;
13+ } ;
14+
15+ TestHelpers . draggable . testDrag = function ( el , handle , dx , dy , expectedDX , expectedDY , msg ) {
16+
17+ var offsetBefore = el . offset ( ) ,
18+ offsetAfter = TestHelpers . draggable . drag ( handle , dx , dy ) ,
19+ actual = { left : offsetAfter . left , top : offsetAfter . top } ,
20+ expected = { left : offsetBefore . left + expectedDX , top : offsetBefore . top + expectedDY } ;
1621
17- function moved ( dx , dy , msg ) {
1822 msg = msg ? msg + "." : "" ;
19- var actual = { left : offsetAfter . left , top : offsetAfter . top } ,
20- expected = { left : offsetBefore . left + dx , top : offsetBefore . top + dy } ;
21- deepEqual ( actual , expected , 'dragged[' + dragged . dx + ', ' + dragged . dy + '] ' + msg ) ;
22- }
23+ deepEqual ( actual , expected , 'dragged[' + dx + ', ' + dy + '] ' + msg ) ;
24+ } ;
25+
26+ TestHelpers . draggable . shouldMove = function ( el , why ) {
27+ TestHelpers . draggable . testDrag ( el , el , 50 , 50 , 50 , 50 , why ) ;
28+ } ;
29+
30+ TestHelpers . draggable . shouldNotMove = function ( el , why ) {
31+ TestHelpers . draggable . testDrag ( el , el , 50 , 50 , 0 , 0 , why ) ;
32+ } ;
33+
34+ TestHelpers . draggable . testScroll = function ( el , position ) {
35+ var oldPosition = $ ( "#main" ) . css ( 'position' ) ;
36+ $ ( "#main" ) . css ( 'position' , position ) ;
37+ TestHelpers . draggable . shouldMove ( el , position + ' parent' ) ;
38+ $ ( "#main" ) . css ( 'position' , oldPosition ) ;
39+ } ;
2340
24- TestHelpers . restoreScroll = function ( what ) {
41+ TestHelpers . draggable . restoreScroll = function ( what ) {
2542 if ( what ) {
2643 $ ( document ) . scrollTop ( 0 ) ; $ ( document ) . scrollLeft ( 0 ) ;
2744 } else {
28- $ ( "#main" ) [ 0 ] . scrollTop = 0 ; $ ( "#main" ) [ 0 ] . scrollLeft = 0 ;
45+ $ ( "#main" ) . scrollTop ( 0 ) ; $ ( "#main" ) . scrollLeft ( 0 ) ;
2946 }
30- }
47+ } ;
48+
49+ TestHelpers . draggable . setScroll = function ( what ) {
50+ if ( what ) {
51+ // todo: currently, the draggable interaction doesn't properly account for scrolled pages,
52+ // uncomment the line below to make the tests fail that should when the page is scrolled
53+ // $(document).scrollTop(100); $(document).scrollLeft(100);
54+ } else {
55+ $ ( "#main" ) . scrollTop ( 100 ) ; $ ( "#main" ) . scrollLeft ( 100 ) ;
56+ }
57+ } ;
58+
59+ TestHelpers . draggable . border = function ( el , side ) {
60+ return parseInt ( el . css ( 'border-' + side + '-width' ) , 10 ) ;
61+ } ;
62+ TestHelpers . draggable . margin = function ( el , side ) {
63+ return parseInt ( el . css ( 'margin-' + side ) , 10 ) ;
64+ } ;
3165
3266( function ( $ ) {
3367
@@ -36,31 +70,35 @@ module("draggable");
3670test ( "element types" , function ( ) {
3771 var typeNames = ( 'p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form' +
3872 ',table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr' +
39- ',acronym,code,samp,kbd,var,img,object, hr' +
73+ ',acronym,code,samp,kbd,var,img,hr' +
4074 ',input,button,label,select,iframe' ) . split ( ',' ) ;
4175
76+ expect ( typeNames . length ) ;
77+
4278 $ . each ( typeNames , function ( i ) {
43- var typeName = typeNames [ i ] ;
44- el = $ ( document . createElement ( typeName ) ) . appendTo ( 'body ' ) ;
79+ var offsetBefore , offsetAfter , typeName = typeNames [ i ] ;
80+ el = $ ( document . createElement ( typeName ) ) . appendTo ( '#main ' ) ;
4581 ( typeName === 'table' && el . append ( "<tr><td>content</td></tr>" ) ) ;
4682 el . draggable ( { cancel : '' } ) ;
47- drag ( el , 50 , 50 ) ;
48- moved ( 50 , 50 , "<" + typeName + ">" ) ;
83+ offsetBefore = el . offset ( ) ;
84+ offsetAfter = TestHelpers . draggable . drag ( el , 50 , 50 ) ;
85+ //there are some rounding errors in FF and Chrome, so we can't say equal, we have to settle for close enough
86+ ok ( offsetAfter . left - offsetBefore . left - 50 < 1 && offsetAfter . top - offsetBefore . top - 50 < 1 , 'dragged[50, 50] ' + "<" + typeName + ">" ) ;
4987 el . draggable ( "destroy" ) ;
5088 el . remove ( ) ;
5189 } ) ;
5290} ) ;
5391
5492test ( "No options, relative" , function ( ) {
93+ expect ( 1 ) ;
5594 el = $ ( "#draggable1" ) . draggable ( ) ;
56- drag ( el , 50 , 50 ) ;
57- moved ( 50 , 50 ) ;
95+ TestHelpers . draggable . shouldMove ( el ) ;
5896} ) ;
5997
6098test ( "No options, absolute" , function ( ) {
99+ expect ( 1 ) ;
61100 el = $ ( "#draggable2" ) . draggable ( ) ;
62- drag ( el , 50 , 50 ) ;
63- moved ( 50 , 50 ) ;
101+ TestHelpers . draggable . shouldMove ( el ) ;
64102} ) ;
65103
66104} ) ( jQuery ) ;
0 commit comments