Draggable Tests: Add test coverage for supported options

This commit is contained in:
Mike Sherov 2013-01-12 00:33:45 -05:00
parent 6d3a1e1fe8
commit c278a4461b
10 changed files with 903 additions and 258 deletions

View File

@ -23,6 +23,7 @@
"Globalize", "Globalize",
"module", "module",
"notEqual", "notEqual",
"notDeepEqual",
"notStrictEqual", "notStrictEqual",
"ok", "ok",
"QUnit", "QUnit",

View File

@ -277,18 +277,30 @@ function findCenter( elem ) {
}; };
} }
function findCorner( elem ) {
var offset,
document = $( elem.ownerDocument );
elem = $( elem );
offset = elem.offset();
return {
x: offset.left - document.scrollLeft(),
y: offset.top - document.scrollTop()
};
}
$.extend( $.simulate.prototype, { $.extend( $.simulate.prototype, {
simulateDrag: function() { simulateDrag: function() {
var i = 0, var i = 0,
target = this.target, target = this.target,
options = this.options, options = this.options,
center = findCenter( target ), center = options.handle === "corner" ? findCorner( target ) : findCenter( target ),
x = Math.floor( center.x ), x = Math.floor( center.x ),
y = Math.floor( center.y ), y = Math.floor( center.y ),
dx = options.dx || 0, coord = { clientX: x, clientY: y },
dy = options.dy || 0, dx = options.dx || ( options.x !== undefined ? options.x - x : 0 ),
moves = options.moves || 3, dy = options.dy || ( options.y !== undefined ? options.y - y : 0 ),
coord = { clientX: x, clientY: y }; moves = options.moves || 3;
this.simulateEvent( target, "mousedown", coord ); this.simulateEvent( target, "mousedown", coord );

View File

@ -23,7 +23,8 @@
"ui/jquery.ui.widget.js", "ui/jquery.ui.widget.js",
"ui/jquery.ui.mouse.js", "ui/jquery.ui.mouse.js",
"ui/jquery.ui.resizable.js", "ui/jquery.ui.resizable.js",
"ui/jquery.ui.draggable.js" "ui/jquery.ui.draggable.js",
"ui/jquery.ui.droppable.js"
] ]
}); });
</script> </script>
@ -46,14 +47,11 @@
<ol id="qunit-tests"></ol> <ol id="qunit-tests"></ol>
<div id="qunit-fixture"> <div id="qunit-fixture">
<div id="main"></div> <div id="main"></div>
<div id="draggable1" style="background: green; width: 200px; height: 100px;">Relative</div> <div id="draggable1" style="background: green; width: 200px; height: 100px;">Relative</div>
<div id="draggable2" style="background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;"><span>Absolute</span></div> <div id="draggable2" style="background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;"><span>Absolute</span></div>
<div style='width: 1px; height: 1000px;'></div> <div id="droppable" style="background: green; width: 200px; height: 100px; position: absolute; top: 110px; left: 110px;"><span>Absolute</span></div>
<div style="width: 1px; height: 1000px;"></div>
<div style="position: absolute; width: 1px; height: 2000px;"></div> <div style="position: absolute; width: 1px; height: 2000px;"></div>
<div id="draggable3"></div>
<div id="draggable4"></div>
</div> </div>
</body> </body>

View File

@ -1,6 +1,5 @@
TestHelpers.commonWidgetTests( "draggable", { TestHelpers.commonWidgetTests( "draggable", {
defaults: { defaults: {
addClasses: true,
appendTo: "parent", appendTo: "parent",
axis: false, axis: false,
cancel: "input,textarea,button,select,option", cancel: "input,textarea,button,select,option",
@ -8,13 +7,10 @@ TestHelpers.commonWidgetTests( "draggable", {
containment: false, containment: false,
cursor: "auto", cursor: "auto",
cursorAt: false, cursorAt: false,
delay: 0,
disabled: false, disabled: false,
distance: 1,
grid: false, grid: false,
handle: false, handle: false,
helper: "original", helper: "original",
iframeFix: false,
opacity: false, opacity: false,
refreshPositions: false, refreshPositions: false,
revert: false, revert: false,
@ -29,6 +25,12 @@ TestHelpers.commonWidgetTests( "draggable", {
stack: false, stack: false,
zIndex: false, zIndex: false,
//todo: remove the following option checks when interactions are rewritten:
addClasses: true,
delay: 0,
distance: 1,
iframeFix: false,
// callbacks // callbacks
create: null, create: null,
drag: null, drag: null,

View File

@ -4,13 +4,26 @@
(function( $ ) { (function( $ ) {
module("draggable"); var relativeElement, absoluteElement;
module( "draggable: core", {
setup: function() {
relativeElement = $("<div style='width: 200px; height: 100px;'>Relative</div>").appendTo("#qunit-fixture");
absoluteElement = $("<div style='background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;'><span>Absolute</span></div>").appendTo("#qunit-fixture");
},
teardown: function() {
relativeElement.remove();
absoluteElement.remove();
}
});
test( "element types", function() { test( "element types", function() {
var typeNames = ("p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" + var typeNames = (
"p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" +
",table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr" + ",table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr" +
",acronym,code,samp,kbd,var,img,hr" + ",acronym,code,samp,kbd,var,img,hr" +
",input,button,label,select,iframe").split(","); ",input,button,label,select,iframe"
).split(",");
expect( typeNames.length * 2 ); expect( typeNames.length * 2 );
@ -19,7 +32,10 @@ test("element types", function() {
typeName = typeNames[ i ], typeName = typeNames[ i ],
el = $( document.createElement( typeName ) ).appendTo("#qunit-fixture"); el = $( document.createElement( typeName ) ).appendTo("#qunit-fixture");
(typeName === "table" && el.append("<tr><td>content</td></tr>")); if ( typeName === "table" ) {
el.append("<tr><td>content</td></tr>");
}
el.draggable({ cancel: "" }); el.draggable({ cancel: "" });
offsetBefore = el.offset(); offsetBefore = el.offset();
el.simulate( "drag", { el.simulate( "drag", {
@ -27,7 +43,9 @@ test("element types", function() {
dy: 50 dy: 50
}); });
offsetAfter = el.offset(); offsetAfter = el.offset();
// there are some rounding errors in FF, Chrome, and IE9, so we can't say equal, we have to settle for close enough
// Support: FF, Chrome, and IE9,
// there are some rounding errors in so we can't say equal, we have to settle for close enough
closeEnough( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" ); closeEnough( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
closeEnough( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" ); closeEnough( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
el.draggable("destroy"); el.draggable("destroy");
@ -37,29 +55,26 @@ test("element types", function() {
test( "No options, relative", function() { test( "No options, relative", function() {
expect( 1 ); expect( 1 );
var el = $("#draggable1").draggable(); TestHelpers.draggable.shouldMove( relativeElement.draggable() );
TestHelpers.draggable.shouldMove(el);
}); });
test( "No options, absolute", function() { test( "No options, absolute", function() {
expect( 1 ); expect( 1 );
var el = $("#draggable2").draggable(); TestHelpers.draggable.shouldMove( absoluteElement.draggable() );
TestHelpers.draggable.shouldMove(el);
}); });
test( "resizable handle with complex markup (#8756 / #8757)", function() { test( "resizable handle with complex markup (#8756 / #8757)", function() {
expect( 2 ); expect( 2 );
$("#draggable1") relativeElement
.append( .append(
$("<div>") $("<div>")
.addClass("ui-resizable-handle") .addClass("ui-resizable-handle ui-resizable-w")
.addClass("ui-resizable-w")
.append( $("<div>") ) .append( $("<div>") )
); );
var handle = $(".ui-resizable-w div"), var handle = $(".ui-resizable-w div"),
target = $("#draggable1").draggable().resizable({ handles: "all" }); target = relativeElement.draggable().resizable({ handles: "all" });
// todo: fix resizable so it doesn't require a mouseover // todo: fix resizable so it doesn't require a mouseover
handle.simulate("mouseover").simulate( "drag", { dx: -50 } ); handle.simulate("mouseover").simulate( "drag", { dx: -50 } );

View File

@ -3,22 +3,37 @@
*/ */
(function( $ ) { (function( $ ) {
module("draggable: events"); var element;
module( "draggable: events", {
setup: function() {
element = $("<div>").appendTo("#qunit-fixture");
},
teardown: function() {
element.draggable("destroy");
}
});
test( "callbacks occurrence count", function() { test( "callbacks occurrence count", function() {
expect( 3 ); expect( 3 );
var start = 0, var start = 0,
stop = 0, stop = 0,
dragc = 0, dragc = 0;
el = $("#draggable2").draggable({
start: function() { start++; }, element.draggable({
drag: function() { dragc++; }, start: function() {
stop: function() { stop++; } start++;
},
drag: function() {
dragc++;
},
stop: function() {
stop++;
}
}); });
el.simulate( "drag", { element.simulate( "drag", {
dx: 10, dx: 10,
dy: 10 dy: 10
}); });
@ -26,23 +41,29 @@ test("callbacks occurrence count", function() {
equal( start, 1, "start callback should happen exactly once" ); equal( start, 1, "start callback should happen exactly once" );
equal( dragc, 3, "drag callback should happen exactly once per mousemove" ); equal( dragc, 3, "drag callback should happen exactly once per mousemove" );
equal( stop, 1, "stop callback should happen exactly once" ); equal( stop, 1, "stop callback should happen exactly once" );
}); });
test( "stopping the start callback", function() { test( "stopping the start callback", function() {
expect( 3 ); expect( 3 );
var start = 0, var start = 0,
stop = 0, stop = 0,
dragc = 0, dragc = 0;
el = $("#draggable2").draggable({
start: function() { start++; return false; }, element.draggable({
drag: function() { dragc++; }, start: function() {
stop: function() { stop++; } start++;
return false;
},
drag: function() {
dragc++;
},
stop: function() {
stop++;
}
}); });
el.simulate( "drag", { element.simulate( "drag", {
dx: 10, dx: 10,
dy: 10 dy: 10
}); });
@ -50,48 +71,53 @@ test("stopping the start callback", function() {
equal( start, 1, "start callback should happen exactly once" ); equal( start, 1, "start callback should happen exactly once" );
equal( dragc, 0, "drag callback should not happen at all" ); equal( dragc, 0, "drag callback should not happen at all" );
equal( stop, 0, "stop callback should not happen if there wasnt even a start" ); equal( stop, 0, "stop callback should not happen if there wasnt even a start" );
}); });
test( "stopping the drag callback", function() { test( "stopping the drag callback", function() {
expect( 2 );
expect(3);
var start = 0, var start = 0,
stop = 0, stop = 0,
dragc = 0, dragc = 0;
el = $("#draggable2").draggable({
start: function() { start++;}, element.draggable({
drag: function() { dragc++; return false; }, start: function() {
stop: function() { stop++; } start++;
},
drag: function() {
dragc++;
return false;
},
stop: function() {
stop++;
}
}); });
el.simulate( "drag", { element.simulate( "drag", {
dx: 10, dx: 10,
dy: 10 dy: 10
}); });
equal( start, 1, "start callback should happen exactly once" ); equal( start, 1, "start callback should happen exactly once" );
equal(dragc, 1, "drag callback should happen exactly once");
equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" ); equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" );
}); });
test( "stopping the stop callback", function() { test( "stopping the stop callback", function() {
expect( 1 ); expect( 1 );
var el = $("#draggable2").draggable({ element.draggable({
helper: "clone", helper: "clone",
stop: function() { return false; } stop: function() {
return false;
}
}); });
el.simulate( "drag", { element.simulate( "drag", {
dx: 10, dx: 10,
dy: 10 dy: 10
}); });
ok($("#draggable2").data("ui-draggable").helper, "the clone should not be deleted if the stop callback is stopped"); ok( element.data("ui-draggable").helper, "the clone should not be deleted if the stop callback is stopped" );
}); });

View File

@ -3,12 +3,21 @@
*/ */
(function( $ ) { (function( $ ) {
module("draggable: methods"); var element;
module( "draggable: methods", {
setup: function() {
element = $("<div style='background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;'><span>Absolute</span></div>").appendTo("#qunit-fixture");
},
teardown: function() {
element.remove();
}
});
test( "init", function() { test( "init", function() {
expect( 5 ); expect( 5 );
$("<div></div>").appendTo("body").draggable().remove(); element.draggable();
ok( true, ".draggable() called on element" ); ok( true, ".draggable() called on element" );
$([]).draggable(); $([]).draggable();
@ -17,25 +26,26 @@ test("init", function() {
$("<div></div>").draggable(); $("<div></div>").draggable();
ok( true, ".draggable() called on disconnected DOMElement" ); ok( true, ".draggable() called on disconnected DOMElement" );
$("<div></div>").draggable().draggable("option", "foo"); element.draggable( "option", "foo" );
ok( true, "arbitrary option getter after init" ); ok( true, "arbitrary option getter after init" );
$("<div></div>").draggable().draggable("option", "foo", "bar"); element.draggable( "option", "foo", "bar" );
ok( true, "arbitrary option setter after init" ); ok( true, "arbitrary option setter after init" );
}); });
test( "destroy", function() { test( "destroy", function() {
expect( 4 ); expect( 4 );
$("<div></div>").appendTo("body").draggable().draggable("destroy").remove();
element.draggable().draggable("destroy");
ok( true, ".draggable('destroy') called on element" ); ok( true, ".draggable('destroy') called on element" );
$([]).draggable().draggable("destroy"); $([]).draggable().draggable("destroy");
ok( true, ".draggable('destroy') called on empty collection" ); ok( true, ".draggable('destroy') called on empty collection" );
$("<div></div>").draggable().draggable("destroy"); element.draggable().draggable("destroy");
ok( true, ".draggable('destroy') called on disconnected DOMElement" ); ok( true, ".draggable('destroy') called on disconnected DOMElement" );
var expected = $("<div></div>").draggable(), var expected = element.draggable(),
actual = expected.draggable("destroy"); actual = expected.draggable("destroy");
equal( actual, expected, "destroy is chainable" ); equal( actual, expected, "destroy is chainable" );
}); });
@ -43,24 +53,22 @@ test("destroy", function() {
test( "enable", function() { test( "enable", function() {
expect( 7 ); expect( 7 );
var expected, actual, el; element.draggable({ disabled: true });
TestHelpers.draggable.shouldNotMove( element, ".draggable({ disabled: true })" );
el = $("#draggable2").draggable({ disabled: true }); element.draggable("enable");
TestHelpers.draggable.shouldNotMove(el, ".draggable({ disabled: true })"); TestHelpers.draggable.shouldMove( element, ".draggable('enable')" );
equal( element.draggable( "option", "disabled" ), false, "disabled option getter" );
el.draggable("enable"); element.draggable("destroy");
TestHelpers.draggable.shouldMove(el, ".draggable('enable')"); element.draggable({ disabled: true });
equal(el.draggable("option", "disabled"), false, "disabled option getter"); TestHelpers.draggable.shouldNotMove( element, ".draggable({ disabled: true })" );
el.draggable("destroy"); element.draggable( "option", "disabled", false );
el.draggable({ disabled: true }); equal(element.draggable( "option", "disabled" ), false, "disabled option setter" );
TestHelpers.draggable.shouldNotMove(el, ".draggable({ disabled: true })"); TestHelpers.draggable.shouldMove( element, ".draggable('option', 'disabled', false)" );
el.draggable("option", "disabled", false); var expected = element.draggable(),
equal(el.draggable("option", "disabled"), false, "disabled option setter");
TestHelpers.draggable.shouldMove(el, ".draggable('option', 'disabled', false)");
expected = $("<div></div>").draggable(),
actual = expected.draggable("enable"); actual = expected.draggable("enable");
equal( actual, expected, "enable is chainable" ); equal( actual, expected, "enable is chainable" );
}); });
@ -68,25 +76,22 @@ test("enable", function() {
test( "disable", function() { test( "disable", function() {
expect( 7 ); expect( 7 );
var expected, actual, el; element = $("#draggable2").draggable({ disabled: false });
TestHelpers.draggable.shouldMove( element, ".draggable({ disabled: false })" );
el = $("#draggable2").draggable({ disabled: false }); element.draggable("disable");
TestHelpers.draggable.shouldMove(el, ".draggable({ disabled: false })"); TestHelpers.draggable.shouldNotMove( element, ".draggable('disable')" );
equal( element.draggable( "option", "disabled" ), true, "disabled option getter" );
el.draggable("disable"); element.draggable("destroy");
TestHelpers.draggable.shouldNotMove(el, ".draggable('disable')"); element.draggable({ disabled: false });
equal(el.draggable("option", "disabled"), true, "disabled option getter"); TestHelpers.draggable.shouldMove( element, ".draggable({ disabled: false })" );
el.draggable("destroy"); element.draggable( "option", "disabled", true );
equal( element.draggable( "option", "disabled" ), true, "disabled option setter" );
TestHelpers.draggable.shouldNotMove( element, ".draggable('option', 'disabled', true)" );
el.draggable({ disabled: false }); var expected = element.draggable(),
TestHelpers.draggable.shouldMove(el, ".draggable({ disabled: false })");
el.draggable("option", "disabled", true);
equal(el.draggable("option", "disabled"), true, "disabled option setter");
TestHelpers.draggable.shouldNotMove(el, ".draggable('option', 'disabled', true)");
expected = $("<div></div>").draggable(),
actual = expected.draggable("disable"); actual = expected.draggable("disable");
equal( actual, expected, "disable is chainable" ); equal( actual, expected, "disable is chainable" );
}); });

View File

@ -5,23 +5,8 @@
module("draggable: options"); module("draggable: options");
test("{ addClasses: true }, default", function() { // TODO: This doesn't actually test whether append happened, possibly remove
expect( 1 ); test("{ appendTo: 'parent' }, default, no clone", function() {
var el = $("<div></div>").draggable({ addClasses: true });
ok(el.is(".ui-draggable"), "'ui-draggable' class added");
el.draggable("destroy");
});
test("{ addClasses: false }", function() {
expect( 1 );
var el = $("<div></div>").draggable({ addClasses: false });
ok(!el.is(".ui-draggable"), "'ui-draggable' class not added");
el.draggable("destroy");
});
test("{ appendTo: 'parent' }, default", function() {
expect( 2 ); expect( 2 );
var el = $("#draggable2").draggable({ appendTo: "parent" }); var el = $("#draggable2").draggable({ appendTo: "parent" });
TestHelpers.draggable.shouldMove(el); TestHelpers.draggable.shouldMove(el);
@ -31,16 +16,19 @@ test("{ appendTo: 'parent' }, default", function() {
}); });
test("{ appendTo: Element }", function() { // TODO: This doesn't actually test whether append happened, possibly remove
test("{ appendTo: Element }, no clone", function() {
expect( 2 ); expect( 2 );
var el = $("#draggable2").draggable({appendTo: $("#draggable2").parent()[0] }); var el = $("#draggable2").draggable({appendTo: $("#draggable2").parent()[0] });
TestHelpers.draggable.shouldMove(el); TestHelpers.draggable.shouldMove(el);
el = $("#draggable1").draggable({ appendTo: $("#draggable2").parent()[0] }); el = $("#draggable1").draggable({ appendTo: $("#draggable2").parent()[0] });
TestHelpers.draggable.shouldMove(el); TestHelpers.draggable.shouldMove(el);
}); });
test("{ appendTo: Selector }", function() { // TODO: This doesn't actually test whether append happened, possibly remove
test("{ appendTo: Selector }, no clone", function() {
expect( 2 ); expect( 2 );
var el = $("#draggable2").draggable({ appendTo: "#main" }); var el = $("#draggable2").draggable({ appendTo: "#main" });
TestHelpers.draggable.shouldMove(el); TestHelpers.draggable.shouldMove(el);
@ -49,6 +37,83 @@ test("{ appendTo: Selector }", function() {
TestHelpers.draggable.shouldMove(el); TestHelpers.draggable.shouldMove(el);
}); });
test("{ appendTo: 'parent' }, default", function() {
expect(2);
var el = $("#draggable1").draggable();
TestHelpers.draggable.trackAppendedParent(el);
equal( el.draggable( "option", "appendTo" ), "parent" );
TestHelpers.draggable.move(el, 1, 1);
equal( el.data("last_dragged_parent"), $("#qunit-fixture")[0] );
});
test("{ appendTo: Element }", function() {
expect(1);
var appendTo = $("#draggable2").parent()[0],
el = $("#draggable1").draggable({ appendTo: appendTo });
TestHelpers.draggable.trackAppendedParent(el);
TestHelpers.draggable.move(el, 1, 1);
equal( el.data("last_dragged_parent"), appendTo );
});
test("{ appendTo: jQuery }", function() {
expect(1);
var appendTo = $("#draggable2").parent(),
el = $("#draggable1").draggable({ appendTo: appendTo });
TestHelpers.draggable.trackAppendedParent(el);
TestHelpers.draggable.move(el, 1, 1);
equal( el.data("last_dragged_parent"), appendTo[0] );
});
test("{ appendTo: Selector }", function() {
expect(1);
var appendTo = "#main",
el = $("#draggable1").draggable({ appendTo: appendTo });
TestHelpers.draggable.trackAppendedParent(el);
TestHelpers.draggable.move(el, 1, 1);
equal( el.data("last_dragged_parent"), $(appendTo)[0] );
});
test("appendTo, default, switching after initialization", function() {
expect(2);
var el = $("#draggable1").draggable({ helper : "clone" });
TestHelpers.draggable.trackAppendedParent(el);
// Move and make sure el was appended to fixture
TestHelpers.draggable.move(el, 1, 1);
equal( el.data("last_dragged_parent"), $("#qunit-fixture")[0] );
// Move and make sure el was appended to main
el.draggable( "option", "appendTo", $("#main") );
TestHelpers.draggable.move(el, 2, 2);
equal( el.data("last_dragged_parent"), $("#main")[0] );
});
test("{ axis: false }, default", function() { test("{ axis: false }, default", function() {
expect( 1 ); expect( 1 );
var el = $("#draggable2").draggable({ axis: false }); var el = $("#draggable2").draggable({ axis: false });
@ -87,6 +152,26 @@ test("{ axis: ? }, unexpected", function() {
}); });
}); });
test("axis, default, switching after initialization", function() {
expect(3);
var el;
// Any direction
el = $("#draggable1").draggable({ axis : false });
TestHelpers.draggable.testDrag(el, el, 50, 50, 50, 50);
// Only horizontal
el.draggable("option", "axis", "x");
TestHelpers.draggable.testDrag(el, el, 50, 50, 50, 0);
// Vertical only
el.draggable("option", "axis", "y");
TestHelpers.draggable.testDrag(el, el, 50, 50, 0, 50);
});
test("{ cancel: 'input,textarea,button,select,option' }, default", function() { test("{ cancel: 'input,textarea,button,select,option' }, default", function() {
expect( 2 ); expect( 2 );
@ -115,6 +200,8 @@ test("{ cancel: 'span' }", function() {
}); });
test( "{ cancel: ? }, unexpected", function() { test( "{ cancel: ? }, unexpected", function() {
expect( 6 );
var el, var el,
unexpected = { unexpected = {
"true": true, "true": true,
@ -122,14 +209,9 @@ test("{ cancel: ? }, unexpected", function() {
"{}": {}, "{}": {},
"[]": [], "[]": [],
"null": null, "null": null,
"undefined": undefined, "undefined": undefined
"function() {return '';}": function() {return "";},
"function() {return true;}": function() {return true;},
"function() {return false;}": function() {return false;}
}; };
expect( 9 );
$.each( unexpected, function( key, val ) { $.each( unexpected, function( key, val ) {
el = $("#draggable2").draggable({ cancel: val }); el = $("#draggable2").draggable({ cancel: val });
TestHelpers.draggable.shouldMove( el, "cancel: " + key ); TestHelpers.draggable.shouldMove( el, "cancel: " + key );
@ -137,20 +219,111 @@ test("{ cancel: ? }, unexpected", function() {
}); });
}); });
/* /**
test("{ containment: false }, default", function() { test("{ cancel: Selectors }, matching parent selector", function() {
expect( 1 );
expect( 5 );
var el = $("#draggable2").draggable({ cancel: "span a" });
$("#qunit-fixture").append( "<span id='wrapping'><a></a></span>" );
el.find( "span" ).append( "<a>" );
$("#wrapping a").append( el );
TestHelpers.draggable.testDrag(el, "#draggable2 span", 50, 50, 50, 50, "drag span child");
TestHelpers.draggable.shouldNotMove( $("#draggable2 span a") );
TestHelpers.draggable.shouldNotMove( $("#wrapping a") );
$("#draggable2").draggable( "option", "cancel", "span > a" );
$("#draggable2").find( "a" ).append( "<a>" );
TestHelpers.draggable.testDrag(el, $("#draggable2 span a").last(), 50, 50, 50, 50, "drag span child");
TestHelpers.draggable.shouldNotMove( $("#draggable2 span a").first() );
});
*/
test("cancel, default, switching after initialization", function() {
expect( 3 );
$("<div id='draggable-option-cancel-default'><input type='text'></div>").appendTo("#main");
var input = $("#draggable-option-cancel-default input"),
el = $("#draggable-option-cancel-default").draggable();
TestHelpers.draggable.testDrag(el, input, 50, 50, 0, 0);
el.draggable("option", "cancel", "textarea" );
TestHelpers.draggable.testDrag(el, input, 50, 50, 50, 50);
el.draggable("option", "cancel", "input" );
TestHelpers.draggable.testDrag(el, input, 50, 50, 0, 0);
ok(false, "missing test - untested code is broken code");
}); });
test("{ containment: Element }", function() { /*
test("{ connectToSortable: selector }, default", function() {
expect( 1 ); expect( 1 );
ok(false, "missing test - untested code is broken code"); ok(false, "missing test - untested code is broken code");
}); });
*/ */
test( "{ containment: Element }", function() {
expect( 1 );
var offsetAfter,
el = $( "#draggable1" ).draggable({ containment: $( "#draggable1" ).parent()[ 0 ] }),
p = el.parent(),
po = p.offset(),
expected = {
left: po.left + TestHelpers.draggable.border(p, "left") + TestHelpers.draggable.margin(el, "left"),
top: po.top + TestHelpers.draggable.border(p, "top") + TestHelpers.draggable.margin(el, "top")
};
el.simulate( "drag", {
dx: -100,
dy: -100
});
offsetAfter = el.offset();
deepEqual(offsetAfter, expected, "compare offset to parent");
});
test( "{ containment: Selector }", function() {
expect( 1 );
var offsetAfter,
el = $( "#draggable1" ).draggable({ containment: $( "#qunit-fixture" ) }),
p = el.parent(),
po = p.offset(),
expected = {
left: po.left + TestHelpers.draggable.border(p, "left") + TestHelpers.draggable.margin(el, "left"),
top: po.top + TestHelpers.draggable.border(p, "top") + TestHelpers.draggable.margin(el, "top")
};
el.simulate( "drag", {
dx: -100,
dy: -100
});
offsetAfter = el.offset();
deepEqual(offsetAfter, expected, "compare offset to parent");
});
test( "{ containment: [x1, y1, x2, y2] }", function() {
expect( 1 );
var el = $( "#draggable1" ).draggable(),
eo = el.offset();
el.draggable( "option", "containment", [ eo.left, eo.top, eo.left + el.width() + 5, eo.left + el.width() + 5 ] );
TestHelpers.draggable.testDrag( el, el, -100, -100, 0, 0 );
});
test("{ containment: 'parent' }, relative", function() { test("{ containment: 'parent' }, relative", function() {
expect( 1 ); expect( 1 );
@ -191,32 +364,25 @@ test("{ containment: 'parent' }, absolute", function() {
deepEqual(offsetAfter, expected, "compare offset to parent"); deepEqual(offsetAfter, expected, "compare offset to parent");
}); });
/* test("containment, default, switching after initialization", function() {
test("{ containment: 'document' }", function() { expect( 2 );
expect( 1 );
var el = $("#draggable1").draggable({ containment: false });
TestHelpers.draggable.testDrag(el, el, -100, -100, -100, -100);
el.draggable( "option", "containment", "parent" )
.css({top:0,left:0})
.appendTo( $("#main") );
TestHelpers.draggable.testDrag(el, el, -100, -100, 0, 0);
// TODO: Switching back to false does not update to false
// el.draggable( "option", "containment", false );
// TestHelpers.draggable.testDrag(el, el, -100, -100, -100, -100);
ok(false, "missing test - untested code is broken code");
}); });
test("{ containment: 'window' }", function() {
expect( 1 );
ok(false, "missing test - untested code is broken code");
});
test("{ containment: Selector }", function() {
expect( 1 );
ok(false, "missing test - untested code is broken code");
});
test("{ containment: [x1, y1, x2, y2] }", function() {
expect( 1 );
ok(false, "missing test - untested code is broken code");
});
*/
test("{ cursor: 'auto' }, default", function() { test("{ cursor: 'auto' }, default", function() {
function getCursor() { return $("#draggable2").css("cursor"); } function getCursor() { return $("#draggable2").css("cursor"); }
@ -270,6 +436,27 @@ test("{ cursor: 'move' }", function() {
}); });
test("cursor, default, switching after initialization", function() {
expect(3);
var el = $("#draggable1").draggable();
TestHelpers.draggable.trackMouseCss( el );
TestHelpers.draggable.move( el, 1, 1 );
equal( el.data("last_dragged_cursor"), "auto" );
el.draggable( "option", "cursor", "move" );
TestHelpers.draggable.move( el, 1, 1 );
equal( el.data("last_dragged_cursor"), "move" );
el.draggable( "option", "cursor", "ns-resize" );
TestHelpers.draggable.move( el, 1, 1 );
equal( el.data("last_dragged_cursor"), "ns-resize" );
});
test( "cursorAt", function() { test( "cursorAt", function() {
expect( 24 ); expect( 24 );
@ -311,15 +498,67 @@ test( "cursorAt", function() {
}); });
}); });
test("{ distance: 10 }", function() { test( "cursorAt, switching after initialization", function() {
expect( 24 );
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 } }
};
$.each( tests, function( testName, testData ) {
$.each( [ "relative", "absolute" ], function( i, position ) {
var el = $( "#draggable" + ( i + 1 ) );
el.draggable({
drag: function( event, ui ) {
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 ) {
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 {
equal( event.clientX - ui.offset.left, testData.x + TestHelpers.draggable.unreliableOffset, testName + " " + position + " left" );
equal( event.clientY - ui.offset.top, testData.y + TestHelpers.draggable.unreliableOffset, testName + " " + position + " top" );
}
}
});
el.draggable( "option", "cursorAt", false );
el.draggable( "option", "cursorAt", testData.cursorAt );
el.simulate( "drag", {
moves: 1,
dx: deltaX,
dy: deltaY
});
});
});
});
test( "disabled", function() {
expect( 3 ); expect( 3 );
var el = $("#draggable2").draggable({ distance: 10 }); var el = $("#draggable1").draggable();
TestHelpers.draggable.testDrag(el, el, -9, -9, 0, 0, "distance not met");
TestHelpers.draggable.testDrag(el, el, -10, -10, -10, -10, "distance met"); TestHelpers.draggable.shouldMove(el);
TestHelpers.draggable.testDrag(el, el, 9, 9, 0, 0, "distance not met"); el.draggable( "option", "disabled", true );
TestHelpers.draggable.shouldNotMove(el);
el.draggable( "option", "disabled", false );
TestHelpers.draggable.shouldMove(el);
}); });
@ -339,6 +578,22 @@ test("{ grid: [50, 50] }, absolute", function() {
TestHelpers.draggable.testDrag(el, el, 26, 25, 50, 50); TestHelpers.draggable.testDrag(el, el, 26, 25, 50, 50);
}); });
test("grid, switching after initialization", function() {
expect( 4 );
var el = $("#draggable1").draggable();
// Forward
TestHelpers.draggable.testDrag(el, el, 24, 24, 24, 24);
TestHelpers.draggable.testDrag(el, el, 0, 0, 0, 0);
el.draggable( "option", "grid", [50,50] );
TestHelpers.draggable.testDrag(el, el, 24, 24, 0, 0);
TestHelpers.draggable.testDrag(el, el, 26, 25, 50, 50);
});
test("{ handle: 'span' }", function() { test("{ handle: 'span' }", function() {
expect( 2 ); expect( 2 );
@ -348,6 +603,65 @@ test("{ handle: 'span' }", function() {
TestHelpers.draggable.shouldNotMove(el, "drag element"); TestHelpers.draggable.shouldNotMove(el, "drag element");
}); });
/*
test("{ handle: Selectors }, matching parent selector", function() {
expect( 4 );
var el = $("#draggable2").draggable({ handle: "span a" });
$("#qunit-fixture").append( "<span id='wrapping'><a></a></span>" );
el.find( "span" ).append( "<a>" );
$("#wrapping a").append( el );
TestHelpers.draggable.testDrag(el, "#draggable2 span a", 50, 50, 50, 50, "drag span child");
TestHelpers.draggable.shouldNotMove( $("#wrapping a") );
$("#draggable2").draggable( "option", "handle", "span > a" );
$("#draggable2").find( "a" ).append( "<a>" );
TestHelpers.draggable.testDrag(el, $("#draggable2 span a").first(), 50, 50, 50, 50, "drag span child");
TestHelpers.draggable.shouldNotMove( $("#draggable2 span a").last() );
});
*/
test("handle, default, switching after initialization", function() {
expect( 6 );
var el = $("#draggable2").draggable();
TestHelpers.draggable.testDrag(el, el, 50, 50, 50, 50);
TestHelpers.draggable.testDrag(el, "#draggable2 span", 100, 100, 100, 100);
// Switch
el.draggable( "option", "handle", "span" );
TestHelpers.draggable.testDrag(el, el, 50, 50, 0, 0);
TestHelpers.draggable.testDrag(el, "#draggable2 span", 100, 100, 100, 100);
// And back
el.draggable( "option", "handle", false );
TestHelpers.draggable.testDrag(el, el, 50, 50, 50, 50);
TestHelpers.draggable.testDrag(el, "#draggable2 span", 100, 100, 100, 100);
});
test("helper, default, switching after initialization", function() {
expect( 3 );
var el = $("#draggable1").draggable();
TestHelpers.draggable.shouldMove(el);
el.draggable( "option", "helper", "clone" );
TestHelpers.draggable.shouldNotMove(el);
el.draggable( "option", "helper", "original" );
TestHelpers.draggable.shouldMove(el);
});
test("{ helper: 'clone' }, relative", function() { test("{ helper: 'clone' }, relative", function() {
expect( 1 ); expect( 1 );
@ -701,8 +1015,267 @@ test("{ opacity: 0.5 }", function() {
}); });
test("{ zIndex: 10 }", function() { test("opacity, default, switching after initialization", function() {
expect(3);
var opacity = null,
el = $("#draggable2").draggable({
start: function() {
opacity = $(this).css("opacity");
}
});
TestHelpers.draggable.move( el, 1, 1 );
equal( opacity, 1 );
el.draggable( "option", "opacity", 0.5 );
TestHelpers.draggable.move( el, 2, 1 );
equal( opacity, 0.5 );
el.draggable( "option", "opacity", false );
TestHelpers.draggable.move( el, 3, 1 );
equal( opacity, 1 );
});
asyncTest( "revert and revertDuration", function() {
expect( 4 );
var el = $( "#draggable2" ).draggable({
revert: true,
revertDuration: 0
});
TestHelpers.draggable.shouldNotMove( el, "revert: true, revertDuration: 0 should revert immediately" );
$( "#draggable2" ).draggable( "option", "revert", "invalid" );
TestHelpers.draggable.shouldNotMove( el, "revert: invalid, revertDuration: 0 should revert immediately" );
$( "#draggable2" ).draggable( "option", "revert", false );
TestHelpers.draggable.shouldMove( el, "revert: false should allow movement" );
$( "#draggable2" ).draggable( "option", {
revert: true,
revertDuration: 200,
stop: function() {
start();
}
});
// animation are async, so test for it asynchronously
TestHelpers.draggable.move( el, 50, 50 );
setTimeout( function() {
ok( $( "#draggable2" ).is( ":animated" ), "revert: true with revertDuration should animate" );
});
});
test( "revert: valid", function() {
expect( 1 );
var el = $( "#draggable2" ).draggable({
revert: "valid",
revertDuration: 0
});
$( "#droppable" ).droppable();
TestHelpers.draggable.testDrag( el, el, 100, 100, 0, 0, "revert: valid reverts when dropped on a droppable" );
});
test( "scope", function() {
expect( 2 );
var el = $( "#draggable2" ).draggable({
scope: "tasks",
revert: "valid",
revertDuration: 0
});
$( "#droppable" ).droppable({ scope: "tasks" });
TestHelpers.draggable.testDrag( el, el, 100, 100, 0, 0, "revert: valid reverts when dropped on a droppable" );
$( "#droppable" ).droppable("destroy").droppable({ scope: "nottasks" });
TestHelpers.draggable.testDrag( el, el, 100, 100, 100, 100, "revert: valid reverts when dropped on a droppable" );
});
test( "scroll, scrollSensitivity, and scrollSpeed", function() {
expect( 2 );
var viewportHeight = $( window ).height(),
el = $( "#draggable1" ).draggable({ scroll: true }),
scrollSensitivity = el.draggable( "option", "scrollSensitivity" ),
scrollSpeed = el.draggable( "option", "scrollSpeed" );
el.offset({
top: viewportHeight - scrollSensitivity - 1,
left: 1
});
el.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" );
el.draggable( "option", "scrollSensitivity", scrollSensitivity + 10 );
el.offset({
top: viewportHeight - scrollSensitivity - 1,
left: 1
});
el.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 );
});
test( "snap, snapMode, and snapTolerance", function() {
expect( 9 );
var newX, newY,
snapTolerance = 15,
el = $( "#draggable1" ).draggable({
snap: true,
snapMode: "both",
snapTolerance: snapTolerance
}),
element2 = $( "#draggable2" ).draggable();
el.offset({
top: 1,
left: 1
});
newX = element2.offset().left - el.outerWidth() - snapTolerance - 1;
newY = element2.offset().top;
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
deepEqual( el.offset(), { top: newY, left: newX }, "doesn't snap outside the snapTolerance" );
newX += 2;
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
notDeepEqual( el.offset(), { top: newY, left: newX }, "snaps inside the snapTolerance" );
el.draggable( "option", "snap", "#draggable2" );
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
notDeepEqual( el.offset(), { top: newY, left: newX }, "snaps based on selector" );
el.draggable( "option", "snap", "#draggable3" );
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
deepEqual( el.offset(), { top: newY, left: newX }, "doesn't snap based on invalid selector" );
el.draggable( "option", "snap", true );
el.draggable( "option", "snapTolerance", snapTolerance - 2 );
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
deepEqual( el.offset(), { top: newY, left: newX }, "doesn't snap outside the modified snapTolerance" );
el.draggable( "option", "snapTolerance", snapTolerance );
el.draggable( "option", "snapMode", "inner" );
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
deepEqual( el.offset(), { top: newY, left: newX }, "doesn't snap inside the outer snapTolerance area when snapMode is inner" );
newX = element2.offset().left - snapTolerance - 1;
newY = element2.offset().top;
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
deepEqual( el.offset(), { top: newY, left: newX }, "doesn't snap inside the outer snapTolerance area when snapMode is inner" );
newX++;
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
notDeepEqual( el.offset(), { top: newY, left: newX }, "snaps inside the inner snapTolerance area when snapMode is inner" );
el.draggable( "option", "snapMode", "outer" );
el.simulate( "drag", {
handle: "corner",
x: newX,
y: newY,
moves: 1
});
deepEqual( el.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" );
});
test( "stack", function() {
expect( 2 );
var element = $( "#draggable1" ).draggable({
stack: "#draggable1, #draggable2"
}),
element2 = $( "#draggable2" ).draggable({
stack: "#draggable1, #draggable2"
});
TestHelpers.draggable.move( element, 1, 1 );
equal( element.css( "zIndex" ), "2", "stack increments zIndex correctly" );
TestHelpers.draggable.move( element2, 1, 1 );
equal( element2.css( "zIndex" ), "3", "stack increments zIndex correctly" );
});
test("{ zIndex: 10 }", function() {
expect(1); expect(1);
var actual, var actual,
@ -723,39 +1296,30 @@ test("{ zIndex: 10 }", function() {
}); });
test( "{ stack }", function() { test("zIndex, default, switching after initialization", function() {
expect( 4 );
var draggable1 = $( "#draggable1" ), expect(3);
draggable2 = $( "#draggable2" ),
draggable3 = $( "#draggable3" ),
draggable4 = $( "#draggable4" );
// Set z-index as an inline style. var zindex = null,
$( "#draggable1, #draggable2" ) el = $("#draggable2").draggable({
.css( "zIndex", 100 ) start: function() {
.draggable({ zindex = $(this).css("z-index");
stack: "#draggable1, #draggable2" }
});
// Have z-index applied via CSS, see #9077
$( "#draggable3, #draggable4" )
.draggable({
stack: "#draggable3, #draggable4"
}); });
draggable1.simulate( "drag", { el.css( "z-index", 1 );
dx: 1,
dy: 1 TestHelpers.draggable.move( el, 1, 1 );
}); equal( zindex, 1 );
draggable3.simulate( "drag", {
dx: 1, el.draggable( "option", "zIndex", 5 );
dy: 1 TestHelpers.draggable.move( el, 2, 1 );
}); equal( zindex, 5 );
el.draggable( "option", "zIndex", false );
TestHelpers.draggable.move( el, 3, 1 );
equal( zindex, 1 );
equal( draggable1.css( "zIndex" ), 102);
equal( draggable2.css( "zIndex" ), 101);
equal( draggable3.css( "zIndex" ), 102);
equal( draggable4.css( "zIndex" ), 101);
}); });
})(jQuery); })(jQuery);

View File

@ -11,7 +11,7 @@ TestHelpers.draggable = {
}); });
offsetAfter = el.offset(); offsetAfter = el.offset();
actual = { left: offsetAfter.left, top: offsetAfter.top }, actual = { left: offsetAfter.left, top: offsetAfter.top };
expected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY }; expected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
msg = msg ? msg + "." : ""; msg = msg ? msg + "." : "";
@ -50,5 +50,29 @@ TestHelpers.draggable = {
}, },
margin: function(el, side) { margin: function(el, side) {
return parseInt(el.css("margin-" + side), 10) || 0; return parseInt(el.css("margin-" + side), 10) || 0;
},
move: function( el, x, y ) {
$( el ).simulate( "drag", {
dx: x,
dy: y
});
},
trackMouseCss : function( el ) {
el.on( "drag", function() {
el.data( "last_dragged_cursor", $("body").css("cursor") );
});
},
trackAppendedParent : function( el ) {
// appendTo ignored without being clone
el.draggable( "option", "helper", "clone" );
el.on( "drag", function(e,ui) {
// Get what parent is at time of drag
el.data( "last_dragged_parent", ui.helper.parent()[0] );
});
} }
}; };

View File

@ -283,10 +283,8 @@ $.widget("ui.draggable", $.ui.mouse, {
_getHandle: function(event) { _getHandle: function(event) {
var handle = !this.options.handle || !$(this.options.handle, this.element).length ? true : false; var handle = !this.options.handle || !$(this.options.handle, this.element).length ? true : false;
$(this.options.handle, this.element)
.find("*") this.element.find( this.options.handle ).each(function() {
.addBack()
.each(function() {
if(this === event.target) { if(this === event.target) {
handle = true; handle = true;
} }