2008-09-20 17:42:04 +00:00
|
|
|
/*
|
|
|
|
* sortable unit tests
|
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
//
|
|
|
|
// Sortable Test Helper Functions
|
|
|
|
//
|
2008-11-12 02:52:31 +00:00
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
appendTo: "parent",
|
2009-01-07 17:45:15 +00:00
|
|
|
cancel: ":input,option",
|
2008-11-12 02:52:31 +00:00
|
|
|
delay: 0,
|
|
|
|
disabled: false,
|
|
|
|
distance: 1,
|
|
|
|
dropOnEmpty: true,
|
|
|
|
helper: "original",
|
|
|
|
items: "> *",
|
|
|
|
scroll: true,
|
|
|
|
scrollSensitivity: 20,
|
|
|
|
scrollSpeed: 20,
|
|
|
|
tolerance: "guess",
|
|
|
|
zIndex: 1000
|
|
|
|
};
|
|
|
|
|
2008-09-20 17:42:04 +00:00
|
|
|
var el, offsetBefore, offsetAfter, dragged;
|
|
|
|
|
|
|
|
var drag = function(handle, dx, dy) {
|
|
|
|
offsetBefore = $(handle).offset();
|
|
|
|
$(handle).simulate("drag", {
|
|
|
|
dx: dx || 0,
|
|
|
|
dy: dy || 0
|
|
|
|
});
|
|
|
|
dragged = { dx: dx, dy: dy };
|
|
|
|
offsetAfter = $(handle).offset();
|
|
|
|
}
|
|
|
|
|
|
|
|
var sort = function(handle, dx, dy, index, msg) {
|
|
|
|
drag(handle, dx, dy);
|
|
|
|
equals($(handle).parent().children().index(handle), index, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
var border = function(el, side) { return parseInt(el.css('border-' + side + '-width')); }
|
|
|
|
var margin = function(el, side) { return parseInt(el.css('margin-' + side)); }
|
|
|
|
|
|
|
|
// Sortable Tests
|
|
|
|
module("sortable");
|
|
|
|
|
|
|
|
test("init", function() {
|
|
|
|
expect(6);
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").appendTo('body').sortable().remove();
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, '.sortable() called on element');
|
|
|
|
|
|
|
|
$([]).sortable();
|
|
|
|
ok(true, '.sortable() called on empty collection');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable();
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, '.sortable() called on disconnected DOMElement');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().sortable("foo");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, 'arbitrary method called after init');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().data("foo.sortable");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, 'arbitrary option getter after init');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().data("foo.sortable", "bar");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, 'arbitrary option setter after init');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("destroy", function() {
|
|
|
|
expect(6);
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").appendTo('body').sortable().sortable("destroy").remove();
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, '.sortable("destroy") called on element');
|
|
|
|
|
|
|
|
$([]).sortable().sortable("destroy");
|
|
|
|
ok(true, '.sortable("destroy") called on empty collection');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().sortable("destroy");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, '.sortable("destroy") called on disconnected DOMElement');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().sortable("destroy").sortable("foo");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, 'arbitrary method called after destroy');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().sortable("destroy").data("foo.sortable");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, 'arbitrary option getter after destroy');
|
|
|
|
|
2008-11-12 02:52:31 +00:00
|
|
|
$("<div></div>").sortable().sortable("destroy").data("foo.sortable", "bar");
|
2008-09-20 17:42:04 +00:00
|
|
|
ok(true, 'arbitrary option setter after destroy');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("enable", function() {
|
|
|
|
expect(4);
|
|
|
|
el = $("#sortable").sortable({ disabled: true });
|
|
|
|
|
|
|
|
sort($("li", el)[0], 0, 40, 0, '.sortable({ disabled: true })');
|
2008-11-18 02:55:25 +00:00
|
|
|
|
2008-09-20 17:42:04 +00:00
|
|
|
el.sortable("enable");
|
|
|
|
equals(el.data("disabled.sortable"), false, "disabled.sortable getter");
|
|
|
|
|
|
|
|
el.sortable("destroy");
|
|
|
|
el.sortable({ disabled: true });
|
|
|
|
el.data("disabled.sortable", false);
|
|
|
|
equals(el.data("disabled.sortable"), false, "disabled.sortable setter");
|
2008-11-18 02:55:25 +00:00
|
|
|
|
2008-09-20 17:42:04 +00:00
|
|
|
sort($("li", el)[0], 0, 40, 2, '.data("disabled.sortable", false)');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("disable", function() {
|
|
|
|
expect(5);
|
|
|
|
el = $("#sortable").sortable({ disabled: false });
|
|
|
|
sort($("li", el)[0], 0, 40, 2, '.sortable({ disabled: false })');
|
|
|
|
|
|
|
|
el.sortable("disable");
|
|
|
|
sort($("li", el)[0], 0, 40, 0, 'disabled.sortable getter');
|
|
|
|
|
|
|
|
el.sortable("destroy");
|
|
|
|
|
|
|
|
el.sortable({ disabled: false });
|
|
|
|
sort($("li", el)[0], 0, 40, 2, '.sortable({ disabled: false })');
|
|
|
|
el.data("disabled.sortable", true);
|
|
|
|
equals(el.data("disabled.sortable"), true, "disabled.sortable setter");
|
|
|
|
sort($("li", el)[0], 0, 40, 0, '.data("disabled.sortable", true)');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("defaults", function() {
|
2008-11-12 02:52:31 +00:00
|
|
|
el = $('<div></div>').sortable();
|
|
|
|
$.each(defaults, function(key, val) {
|
|
|
|
var actual = el.data(key + ".sortable"), expected = val;
|
|
|
|
same(actual, expected, key);
|
|
|
|
});
|
|
|
|
el.remove();
|
2008-09-20 17:42:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("#3019: Stop fires too early", function() {
|
2008-11-18 02:55:25 +00:00
|
|
|
|
2008-09-20 17:42:04 +00:00
|
|
|
var helper = null;
|
2008-11-14 03:00:16 +00:00
|
|
|
el = $("#sortable").sortable({ stop: function(event, ui) {
|
2008-09-20 17:42:04 +00:00
|
|
|
helper = ui.helper;
|
|
|
|
}});
|
2008-11-18 02:55:25 +00:00
|
|
|
|
2008-09-20 17:42:04 +00:00
|
|
|
sort($("li", el)[0], 0, 40, 2, 'Dragging the sortable');
|
|
|
|
equals(helper, null, "helper should be false");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
})(jQuery);
|