Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ grunt.initConfig({
},
jscs: {
// datepicker, sortable, resizable and draggable are getting rewritten, ignore until that's done
ui: [ "ui/*.js", "!ui/datepicker.js", "!ui/sortable.js", "!ui/resizable.js", "!ui/draggable.js" ],
ui: [ "ui/*.js", "!ui/datepicker.js", "!ui/sortable.js", "!ui/resizable.js" ],
// TODO enable this once we have a tool that can help with fixing formatting of existing files
// tests: "tests/unit/**/*.js",
grunt: "Gruntfile.js"
Expand Down
57 changes: 32 additions & 25 deletions tests/unit/draggable/draggable_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test( "{ appendTo: Selector }", function() {
test( "appendTo, default, switching after initialization", function() {
expect( 2 );

var element = $( "#draggable1" ).draggable({ helper : "clone" });
var element = $( "#draggable1" ).draggable({ helper: "clone" });

TestHelpers.draggable.trackAppendedParent( element );

Expand Down Expand Up @@ -140,7 +140,7 @@ test( "{ axis: ? }, unexpected", function() {
test( "axis, default, switching after initialization", function() {
expect( 6 );

var element = $( "#draggable1" ).draggable({ axis : false });
var element = $( "#draggable1" ).draggable({ axis: false });

// Any Direction
TestHelpers.draggable.shouldMove( element, "axis: default" );
Expand Down Expand Up @@ -504,23 +504,23 @@ test( "cursorAt", function() {
var deltaX = -3,
deltaY = -3,
tests = {
"false": { cursorAt : false },
"{ left: -5, top: -5 }": { x: -5, y: -5, cursorAt : { left: -5, top: -5 } },
"[ 10, 20 ]": { x: 10, y: 20, cursorAt : [ 10, 20 ] },
"'10 20'": { x: 10, y: 20, cursorAt : "10 20" },
"{ left: 20, top: 40 }": { x: 20, y: 40, cursorAt : { left: 20, top: 40 } },
"{ right: 10, bottom: 20 }": { x: 10, y: 20, cursorAt : { right: 10, bottom: 20 } }
"false": { cursorAt: false },
"{ left: -5, top: -5 }": { x: -5, y: -5, cursorAt: { left: -5, top: -5 } },
"[ 10, 20 ]": { x: 10, y: 20, cursorAt: [ 10, 20 ] },
"'10 20'": { x: 10, y: 20, cursorAt: "10 20" },
"{ left: 20, top: 40 }": { x: 20, y: 40, cursorAt: { left: 20, top: 40 } },
"{ right: 10, bottom: 20 }": { x: 10, y: 20, cursorAt: { right: 10, bottom: 20 } }
};

$.each( tests, function( testName, testData ) {
$.each( [ "relative", "absolute" ], function( i, position ) {
var element = $( "#draggable" + ( i + 1 ) ).draggable({
cursorAt: testData.cursorAt,
drag: function( event, ui ) {
if( !testData.cursorAt ) {
if ( !testData.cursorAt ) {
equal( ui.position.left - ui.originalPosition.left, deltaX, testName + " " + position + " left" );
equal( ui.position.top - ui.originalPosition.top, deltaY, testName + " " + position + " top" );
} else if( testData.cursorAt.right ) {
} else if ( testData.cursorAt.right ) {
equal( ui.helper.width() - ( event.clientX - ui.offset.left ), testData.x - TestHelpers.draggable.unreliableOffset, testName + " " + position + " left" );
equal( ui.helper.height() - ( event.clientY - ui.offset.top ), testData.y - TestHelpers.draggable.unreliableOffset, testName + " " +position + " top" );
} else {
Expand All @@ -545,12 +545,12 @@ test( "cursorAt, switching after initialization", function() {
var deltaX = -3,
deltaY = -3,
tests = {
"false": { cursorAt : false },
"{ left: -5, top: -5 }": { x: -5, y: -5, cursorAt : { left: -5, top: -5 } },
"[ 10, 20 ]": { x: 10, y: 20, cursorAt : [ 10, 20 ] },
"'10 20'": { x: 10, y: 20, cursorAt : "10 20" },
"{ left: 20, top: 40 }": { x: 20, y: 40, cursorAt : { left: 20, top: 40 } },
"{ right: 10, bottom: 20 }": { x: 10, y: 20, cursorAt : { right: 10, bottom: 20 } }
"false": { cursorAt: false },
"{ left: -5, top: -5 }": { x: -5, y: -5, cursorAt: { left: -5, top: -5 } },
"[ 10, 20 ]": { x: 10, y: 20, cursorAt: [ 10, 20 ] },
"'10 20'": { x: 10, y: 20, cursorAt: "10 20" },
"{ left: 20, top: 40 }": { x: 20, y: 40, cursorAt: { left: 20, top: 40 } },
"{ right: 10, bottom: 20 }": { x: 10, y: 20, cursorAt: { right: 10, bottom: 20 } }
};

$.each( tests, function( testName, testData ) {
Expand All @@ -559,10 +559,10 @@ test( "cursorAt, switching after initialization", function() {

element.draggable({
drag: function( event, ui ) {
if( !testData.cursorAt ) {
if ( !testData.cursorAt ) {
equal( ui.position.left - ui.originalPosition.left, deltaX, testName + " " + position + " left" );
equal( ui.position.top - ui.originalPosition.top, deltaY, testName + " " + position + " top" );
} else if( testData.cursorAt.right ) {
} else if ( testData.cursorAt.right ) {
equal( ui.helper.width() - ( event.clientX - ui.offset.left ), testData.x - TestHelpers.draggable.unreliableOffset, testName + " " + position + " left" );
equal( ui.helper.height() - ( event.clientY - ui.offset.top ), testData.y - TestHelpers.draggable.unreliableOffset, testName + " " +position + " top" );
} else {
Expand Down Expand Up @@ -672,7 +672,7 @@ test( "helper, default, switching after initialization", function() {
});

/* jshint loopfunc: true */
(function(){
(function() {
var k, l, m,
scrollElements = {
"no elements": [],
Expand All @@ -689,7 +689,7 @@ test( "helper, default, switching after initialization", function() {
for ( m = 0 ; m < helpers.length; m++ ) {
for ( l = 0; l < positions.length; l++ ) {
for ( k in scrollElements ) {
(function( position, helper, scrollElements, scrollElementsTitle ){
(function( position, helper, scrollElements, scrollElementsTitle ) {
test( "{ helper: '" + helper + "' }, " + position + ", with scroll offset on " + scrollElementsTitle, function() {
expect( scrollPositions.length * 2 );

Expand Down Expand Up @@ -835,7 +835,8 @@ test( "scroll, scrollSensitivity, and scrollSpeed", function() {

TestHelpers.draggable.setScrollable( "#main", false );

var viewportHeight = $( window ).height(),
var currentScrollTop,
viewportHeight = $( window ).height(),
element = $( "#draggable1" ).draggable({ scroll: true }).appendTo( "#qunit-fixture" ),
scrollSensitivity = element.draggable( "option", "scrollSensitivity" ),
scrollSpeed = element.draggable( "option", "scrollSpeed" );
Expand All @@ -845,29 +846,35 @@ test( "scroll, scrollSensitivity, and scrollSpeed", function() {
left: 1
});

$( element ).one( "drag", function() {
equal( $( window ).scrollTop(), 0, "scroll: true doesn't scroll when the element is dragged outside of scrollSensitivity" );
});

element.simulate( "drag", {
dx: 1,
y: viewportHeight - scrollSensitivity - 1,
moves: 1
});

ok( $( window ).scrollTop() === 0, "scroll: true doesn't scroll when the element is dragged outside of scrollSensitivity" );

element.draggable( "option", "scrollSensitivity", scrollSensitivity + 10 );

element.offset({
top: viewportHeight - scrollSensitivity - 1,
left: 1
});

currentScrollTop = $( window ).scrollTop();

$( element ).one( "drag", function() {
ok( $( window ).scrollTop() - currentScrollTop, scrollSpeed, "scroll: true scrolls when the element is dragged within scrollSensitivity" );
});

element.simulate( "drag", {
dx: 1,
y: viewportHeight - scrollSensitivity - 1,
moves: 1
});

ok( $( window ).scrollTop() === scrollSpeed, "scroll: true scrolls when the element is dragged within scrollSensitivity" );

TestHelpers.draggable.restoreScroll( document );
});

Expand Down
28 changes: 19 additions & 9 deletions tests/unit/draggable/draggable_test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ TestHelpers.draggable = {
// TODO: remove the unreliable offset hacks
unreliableOffset: $.ui.ie && ( !document.documentMode || document.documentMode < 8 ) ? 2 : 0,
// Support: Opera 12.10, Safari 5.1, jQuery <1.8
unreliableContains: (function(){
unreliableContains: (function() {
var element = $( "<div>" );
return $.contains( element[ 0 ].ownerDocument, element[ 0 ] );
})(),
Expand All @@ -12,17 +12,27 @@ TestHelpers.draggable = {
$( el ).one( "dragstop", function( event, ui ) {
var positionExpected = { left: ui.originalPosition.left + expectedDX, top: ui.originalPosition.top + expectedDY };
deepEqual( ui.position, positionExpected, "position dragged[" + dx + ", " + dy + "] " + msg );
} );
});
},
testDragOffset: function( el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : "";

var offsetBefore = el.offset(),
offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };

$( el ).one( "dragstop", function( event, ui ) {
deepEqual( ui.offset, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
});
},
testDragHelperOffset: function( el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : "";

var offsetBefore = el.offset(),
offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };

$( el ).one( "dragstop", function( event, ui ) {
deepEqual( ui.helper.offset(), offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
} );
});
},
testDrag: function( el, handle, dx, dy, expectedDX, expectedDY, msg ) {
TestHelpers.draggable.testDragPosition( el, dx, dy, expectedDX, expectedDY, msg );
Expand All @@ -36,7 +46,7 @@ TestHelpers.draggable = {
shouldMovePositionButNotOffset: function( el, msg, handle ) {
handle = handle || el;
TestHelpers.draggable.testDragPosition( el, 100, 100, 100, 100, msg );
TestHelpers.draggable.testDragOffset( el, 100, 100, 0, 0, msg );
TestHelpers.draggable.testDragHelperOffset( el, 100, 100, 0, 0, msg );

$( handle ).simulate( "drag", {
dx: 100,
Expand All @@ -55,14 +65,14 @@ TestHelpers.draggable = {
handle = handle || el;
$( el ).bind( "dragstop", function() {
ok( false, "should not drag " + msg );
} );
});
$( handle ).simulate( "drag", {
dx: 100,
dy: 100
});
$( el ).unbind( "dragstop" );
},
setScrollable: function ( what, isScrollable ) {
setScrollable: function( what, isScrollable ) {
var overflow = isScrollable ? "scroll" : "hidden";
$( what ).css({ overflow: overflow, overflowX: overflow, overflowY: overflow });
},
Expand Down Expand Up @@ -90,12 +100,12 @@ TestHelpers.draggable = {
dy: y
});
},
trackMouseCss : function( el ) {
trackMouseCss: function( el ) {
el.bind( "drag", function() {
el.data( "last_dragged_cursor", $( "body" ).css( "cursor" ) );
});
},
trackAppendedParent : function( el ) {
trackAppendedParent: function( el ) {
// TODO: appendTo is currently ignored if helper is original (see #7044)
el.draggable( "option", "helper", "clone" );

Expand All @@ -104,4 +114,4 @@ TestHelpers.draggable = {
el.data( "last_dragged_parent", ui.helper.parent()[ 0 ] );
});
}
};
};
Loading