Draggable Tests: Cleaned up core and method tests.

This commit is contained in:
Mike Sherov 2013-01-13 18:55:43 -05:00
parent 430bd68344
commit c70b3dc9e3
3 changed files with 111 additions and 91 deletions

View File

@ -40,12 +40,12 @@
<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 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> </div>

View File

@ -2,24 +2,40 @@
* draggable_core.js * draggable_core.js
*/ */
(function($) { (function( $ ) {
module("draggable"); var relativeElement, absoluteElement;
test("element types", function() { module( "draggable: core", {
var typeNames = ("p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" + setup: function() {
",table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr" + relativeElement = $("<div style='width: 200px; height: 100px;'>Relative</div>").appendTo("#qunit-fixture");
",acronym,code,samp,kbd,var,img,hr" + absoluteElement = $("<div style='background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;'><span>Absolute</span></div>").appendTo("#qunit-fixture");
",input,button,label,select,iframe").split(","); },
teardown: function() {
relativeElement.remove();
absoluteElement.remove();
}
});
test( "element types", function() {
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" +
",acronym,code,samp,kbd,var,img,hr" +
",input,button,label,select,iframe"
).split(",");
expect( typeNames.length * 2 ); expect( typeNames.length * 2 );
$.each(typeNames, function(i) { $.each( typeNames, function( i ) {
var offsetBefore, offsetAfter, var offsetBefore, offsetAfter,
typeName = typeNames[i], typeName = typeNames[ i ],
el = $(document.createElement(typeName)).appendTo("#qunit-fixture"); el = $( document.createElement( typeName ) ).appendTo("#qunit-fixture");
if ( typeName === "table" ) {
el.append("<tr><td>content</td></tr>");
}
(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,39 +43,38 @@ 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
closeEnough(offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + ">"); // Support: FF, Chrome, and IE9,
closeEnough(offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">"); // 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.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
el.draggable("destroy"); el.draggable("destroy");
el.remove(); el.remove();
}); });
}); });
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 } );
@ -70,4 +85,4 @@ test("resizable handle with complex markup (#8756 / #8757)", function() {
equal( target.width(), 200, "compare width" ); equal( target.width(), 200, "compare width" );
}); });
})(jQuery); })( jQuery );

View File

@ -1,94 +1,99 @@
/* /*
* draggable_methods.js * draggable_methods.js
*/ */
(function($) { (function( $ ) {
module("draggable: methods"); var element;
test("init", function() { module( "draggable: methods", {
expect(5); 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();
}
});
$("<div></div>").appendTo("body").draggable().remove(); test( "init", function() {
ok(true, ".draggable() called on element"); expect( 5 );
element.draggable();
ok( true, ".draggable() called on element" );
$([]).draggable(); $([]).draggable();
ok(true, ".draggable() called on empty collection"); ok( true, ".draggable() called on empty collection" );
$("<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();
ok(true, ".draggable('destroy') called on element"); element.draggable().draggable("destroy");
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" );
}); });
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"); actual = expected.draggable("enable");
TestHelpers.draggable.shouldMove(el, ".draggable('option', 'disabled', false)"); equal( actual, expected, "enable is chainable" );
expected = $("<div></div>").draggable(),
actual = expected.draggable("enable");
equal(actual, expected, "enable is chainable");
}); });
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 })"); actual = expected.draggable("disable");
equal( actual, expected, "disable is chainable" );
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");
equal(actual, expected, "disable is chainable");
}); });
})(jQuery); })( jQuery );