From c70b3dc9e3d68d1f61b29665b1911b2e4a9ed336 Mon Sep 17 00:00:00 2001 From: Mike Sherov Date: Sun, 13 Jan 2013 18:55:43 -0500 Subject: [PATCH] Draggable Tests: Cleaned up core and method tests. --- tests/unit/draggable/draggable.html | 10 +- tests/unit/draggable/draggable_core.js | 69 +++++++----- tests/unit/draggable/draggable_methods.js | 123 +++++++++++----------- 3 files changed, 111 insertions(+), 91 deletions(-) diff --git a/tests/unit/draggable/draggable.html b/tests/unit/draggable/draggable.html index cd068eb20..554b49e84 100644 --- a/tests/unit/draggable/draggable.html +++ b/tests/unit/draggable/draggable.html @@ -40,12 +40,12 @@
    -
    +
    -
    Relative
    -
    Absolute
    -
    -
    +
    Relative
    +
    Absolute
    +
    +
    diff --git a/tests/unit/draggable/draggable_core.js b/tests/unit/draggable/draggable_core.js index 00d914194..2090f2fd9 100644 --- a/tests/unit/draggable/draggable_core.js +++ b/tests/unit/draggable/draggable_core.js @@ -2,24 +2,40 @@ * draggable_core.js */ -(function($) { +(function( $ ) { -module("draggable"); +var relativeElement, absoluteElement; -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(","); +module( "draggable: core", { + setup: function() { + relativeElement = $("
    Relative
    ").appendTo("#qunit-fixture"); + absoluteElement = $("
    Absolute
    ").appendTo("#qunit-fixture"); + }, + 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 ); - $.each(typeNames, function(i) { + $.each( typeNames, function( i ) { var offsetBefore, offsetAfter, - typeName = typeNames[i], - el = $(document.createElement(typeName)).appendTo("#qunit-fixture"); + typeName = typeNames[ i ], + el = $( document.createElement( typeName ) ).appendTo("#qunit-fixture"); + + if ( typeName === "table" ) { + el.append("content"); + } - (typeName === "table" && el.append("content")); el.draggable({ cancel: "" }); offsetBefore = el.offset(); el.simulate( "drag", { @@ -27,39 +43,38 @@ test("element types", function() { dy: 50 }); 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 + ">"); - closeEnough(offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">"); + + // 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.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" ); el.draggable("destroy"); el.remove(); }); }); -test("No options, relative", function() { +test( "No options, relative", function() { expect( 1 ); - var el = $("#draggable1").draggable(); - TestHelpers.draggable.shouldMove(el); + TestHelpers.draggable.shouldMove( relativeElement.draggable() ); }); -test("No options, absolute", function() { +test( "No options, absolute", function() { expect( 1 ); - var el = $("#draggable2").draggable(); - TestHelpers.draggable.shouldMove(el); + TestHelpers.draggable.shouldMove( absoluteElement.draggable() ); }); -test("resizable handle with complex markup (#8756 / #8757)", function() { +test( "resizable handle with complex markup (#8756 / #8757)", function() { expect( 2 ); - $("#draggable1") + relativeElement .append( $("
    ") - .addClass("ui-resizable-handle") - .addClass("ui-resizable-w") - .append($("
    ")) + .addClass("ui-resizable-handle ui-resizable-w") + .append( $("
    ") ) ); 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 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" ); }); -})(jQuery); +})( jQuery ); diff --git a/tests/unit/draggable/draggable_methods.js b/tests/unit/draggable/draggable_methods.js index 43db285ce..901c261d6 100644 --- a/tests/unit/draggable/draggable_methods.js +++ b/tests/unit/draggable/draggable_methods.js @@ -1,94 +1,99 @@ /* * draggable_methods.js */ -(function($) { +(function( $ ) { -module("draggable: methods"); +var element; -test("init", function() { - expect(5); +module( "draggable: methods", { + setup: function() { + element = $("
    Absolute
    ").appendTo("#qunit-fixture"); + }, + teardown: function() { + element.remove(); + } +}); - $("
    ").appendTo("body").draggable().remove(); - ok(true, ".draggable() called on element"); +test( "init", function() { + expect( 5 ); + + element.draggable(); + ok( true, ".draggable() called on element" ); $([]).draggable(); - ok(true, ".draggable() called on empty collection"); + ok( true, ".draggable() called on empty collection" ); $("
    ").draggable(); - ok(true, ".draggable() called on disconnected DOMElement"); + ok( true, ".draggable() called on disconnected DOMElement" ); - $("
    ").draggable().draggable("option", "foo"); - ok(true, "arbitrary option getter after init"); + element.draggable( "option", "foo" ); + ok( true, "arbitrary option getter after init" ); - $("
    ").draggable().draggable("option", "foo", "bar"); - ok(true, "arbitrary option setter after init"); + element.draggable( "option", "foo", "bar" ); + ok( true, "arbitrary option setter after init" ); }); -test("destroy", function() { - expect(4); - $("
    ").appendTo("body").draggable().draggable("destroy").remove(); - ok(true, ".draggable('destroy') called on element"); +test( "destroy", function() { + expect( 4 ); + + element.draggable().draggable("destroy"); + ok( true, ".draggable('destroy') called on element" ); $([]).draggable().draggable("destroy"); - ok(true, ".draggable('destroy') called on empty collection"); + ok( true, ".draggable('destroy') called on empty collection" ); - $("
    ").draggable().draggable("destroy"); - ok(true, ".draggable('destroy') called on disconnected DOMElement"); + element.draggable().draggable("destroy"); + ok( true, ".draggable('destroy') called on disconnected DOMElement" ); - var expected = $("
    ").draggable(), + var expected = element.draggable(), actual = expected.draggable("destroy"); - equal(actual, expected, "destroy is chainable"); + equal( actual, expected, "destroy is chainable" ); }); -test("enable", function() { - expect(7); +test( "enable", function() { + expect( 7 ); - var expected, actual, el; + element.draggable({ disabled: true }); + TestHelpers.draggable.shouldNotMove( element, ".draggable({ disabled: true })" ); - el = $("#draggable2").draggable({ disabled: true }); - TestHelpers.draggable.shouldNotMove(el, ".draggable({ disabled: true })"); + element.draggable("enable"); + TestHelpers.draggable.shouldMove( element, ".draggable('enable')" ); + equal( element.draggable( "option", "disabled" ), false, "disabled option getter" ); - el.draggable("enable"); - TestHelpers.draggable.shouldMove(el, ".draggable('enable')"); - equal(el.draggable("option", "disabled"), false, "disabled option getter"); + element.draggable("destroy"); + element.draggable({ disabled: true }); + TestHelpers.draggable.shouldNotMove( element, ".draggable({ disabled: true })" ); - el.draggable("destroy"); - el.draggable({ disabled: true }); - TestHelpers.draggable.shouldNotMove(el, ".draggable({ disabled: true })"); + element.draggable( "option", "disabled", false ); + equal(element.draggable( "option", "disabled" ), false, "disabled option setter" ); + TestHelpers.draggable.shouldMove( element, ".draggable('option', 'disabled', false)" ); - el.draggable("option", "disabled", false); - equal(el.draggable("option", "disabled"), false, "disabled option setter"); - TestHelpers.draggable.shouldMove(el, ".draggable('option', 'disabled', false)"); - - expected = $("
    ").draggable(), - actual = expected.draggable("enable"); - equal(actual, expected, "enable is chainable"); + var expected = element.draggable(), + actual = expected.draggable("enable"); + equal( actual, expected, "enable is chainable" ); }); -test("disable", function() { - expect(7); +test( "disable", function() { + expect( 7 ); - var expected, actual, el; + element = $("#draggable2").draggable({ disabled: false }); + TestHelpers.draggable.shouldMove( element, ".draggable({ disabled: false })" ); - el = $("#draggable2").draggable({ disabled: false }); - TestHelpers.draggable.shouldMove(el, ".draggable({ disabled: false })"); + element.draggable("disable"); + TestHelpers.draggable.shouldNotMove( element, ".draggable('disable')" ); + equal( element.draggable( "option", "disabled" ), true, "disabled option getter" ); - el.draggable("disable"); - TestHelpers.draggable.shouldNotMove(el, ".draggable('disable')"); - equal(el.draggable("option", "disabled"), true, "disabled option getter"); + element.draggable("destroy"); + element.draggable({ disabled: false }); + 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 }); - 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 = $("
    ").draggable(), - actual = expected.draggable("disable"); - equal(actual, expected, "disable is chainable"); + var expected = element.draggable(), + actual = expected.draggable("disable"); + equal( actual, expected, "disable is chainable" ); }); -})(jQuery); +})( jQuery );