Draggable: Shift to use no globals

This commit is contained in:
Amanpreet Singh 2016-04-03 21:51:57 +05:30
parent 86fd11d5a7
commit 46f607af97
6 changed files with 427 additions and 418 deletions

View File

@ -31,7 +31,7 @@ common.testWidget( "draggable", {
stack: false, stack: false,
zIndex: false, zIndex: false,
//todo: remove the following option checks when interactions are rewritten: //Todo: remove the following option checks when interactions are rewritten:
addClasses: true, addClasses: true,
delay: 0, delay: 0,
distance: 1, distance: 1,

View File

@ -1,14 +1,15 @@
define( [ define( [
"qunit",
"jquery", "jquery",
"./helper", "./helper",
"ui/widgets/draggable", "ui/widgets/draggable",
"ui/widgets/droppable", "ui/widgets/droppable",
"ui/widgets/resizable" "ui/widgets/resizable"
], function( $, testHelper ) { ], function( QUnit, $, testHelper ) {
module( "draggable: core" ); QUnit.module( "draggable: core" );
test( "element types", function( assert ) { QUnit.test( "element types", function( assert ) {
var typeNames = ( var typeNames = (
"p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" + "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" +
@ -16,7 +17,7 @@ test( "element types", function( assert ) {
",input,button,label,select,iframe" ",input,button,label,select,iframe"
).split( "," ); ).split( "," );
expect( typeNames.length * 2 ); assert.expect( typeNames.length * 2 );
$.each( typeNames, function( i ) { $.each( typeNames, function( i ) {
var offsetBefore, offsetAfter, var offsetBefore, offsetAfter,
@ -44,18 +45,18 @@ test( "element types", function( assert ) {
} ); } );
} ); } );
test( "No options, relative", function() { QUnit.test( "No options, relative", function( assert ) {
expect( 2 ); assert.expect( 2 );
testHelper.shouldMove( $( "#draggable1" ).draggable(), "no options, relative" ); testHelper.shouldMove( assert, $( "#draggable1" ).draggable(), "no options, relative" );
} ); } );
test( "No options, absolute", function() { QUnit.test( "No options, absolute", function( assert ) {
expect( 2 ); assert.expect( 2 );
testHelper.shouldMove( $( "#draggable2" ).draggable(), "no options, absolute" ); testHelper.shouldMove( assert, $( "#draggable2" ).draggable(), "no options, absolute" );
} ); } );
test( "resizable handle with complex markup (#8756 / #8757)", function() { QUnit.test( "resizable handle with complex markup (#8756 / #8757)", function( assert ) {
expect( 2 ); assert.expect( 2 );
$( "#draggable1" ) $( "#draggable1" )
.append( .append(
@ -69,19 +70,19 @@ test( "resizable handle with complex markup (#8756 / #8757)", function() {
// 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 } );
equal( target.width(), 250, "compare width" ); assert.equal( target.width(), 250, "compare width" );
// 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 } );
equal( target.width(), 200, "compare width" ); assert.equal( target.width(), 200, "compare width" );
} ); } );
test( "#8269: Removing draggable element on drop", function() { QUnit.test( "#8269: Removing draggable element on drop", function( assert ) {
expect( 2 ); assert.expect( 2 );
var element = $( "#draggable1" ).wrap( "<div id='wrapper' />" ).draggable( { var element = $( "#draggable1" ).wrap( "<div id='wrapper' />" ).draggable( {
stop: function() { stop: function() {
ok( true, "stop still called despite element being removed from DOM on drop" ); assert.ok( true, "stop still called despite element being removed from DOM on drop" );
} }
} ), } ),
dropOffset = $( "#droppable" ).offset(); dropOffset = $( "#droppable" ).offset();
@ -89,14 +90,14 @@ test( "#8269: Removing draggable element on drop", function() {
$( "#droppable" ).droppable( { $( "#droppable" ).droppable( {
drop: function() { drop: function() {
$( "#wrapper" ).remove(); $( "#wrapper" ).remove();
ok( true, "element removed from DOM on drop" ); assert.ok( true, "element removed from DOM on drop" );
} }
} ); } );
// Support: Opera 12.10, Safari 5.1, jQuery <1.8 // Support: Opera 12.10, Safari 5.1, jQuery <1.8
if ( testHelper.unreliableContains ) { if ( testHelper.unreliableContains ) {
ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" ); assert.ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" );
ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" ); assert.ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" );
} else { } else {
element.simulate( "drag", { element.simulate( "drag", {
handle: "corner", handle: "corner",
@ -108,8 +109,8 @@ test( "#8269: Removing draggable element on drop", function() {
// http://bugs.jqueryui.com/ticket/7778 // http://bugs.jqueryui.com/ticket/7778
// drag element breaks in IE8 when its content is replaced onmousedown // drag element breaks in IE8 when its content is replaced onmousedown
test( "Stray mousemove after mousedown still drags", function() { QUnit.test( "Stray mousemove after mousedown still drags", function( assert ) {
expect( 2 ); assert.expect( 2 );
var element = $( "#draggable1" ).draggable( { scroll: false } ); var element = $( "#draggable1" ).draggable( { scroll: false } );
@ -120,16 +121,16 @@ test( "Stray mousemove after mousedown still drags", function() {
$( document ).simulate( "mousemove", { button: -1 } ); $( document ).simulate( "mousemove", { button: -1 } );
} ); } );
testHelper.shouldMove( element, "element is draggable" ); testHelper.shouldMove( assert, element, "element is draggable" );
} ); } );
test( "#6258: not following mouse when scrolled and using overflow-y: scroll", function() { QUnit.test( "#6258: not following mouse when scrolled and using overflow-y: scroll", function( assert ) {
expect( 2 ); assert.expect( 2 );
var element = $( "#draggable1" ).draggable( { var element = $( "#draggable1" ).draggable( {
stop: function( event, ui ) { stop: function( event, ui ) {
equal( ui.position.left, 1, "left position is correct despite overflow on HTML" ); assert.equal( ui.position.left, 1, "left position is correct despite overflow on HTML" );
equal( ui.position.top, 1, "top position is correct despite overflow on HTML" ); assert.equal( ui.position.top, 1, "top position is correct despite overflow on HTML" );
$( "html" ) $( "html" )
.css( "overflow-y", oldOverflowY ) .css( "overflow-y", oldOverflowY )
.css( "overflow-x", oldOverflowX ) .css( "overflow-x", oldOverflowX )
@ -155,13 +156,13 @@ test( "#6258: not following mouse when scrolled and using overflow-y: scroll", f
} ); } );
} ); } );
test( "#9315: jumps down with offset of scrollbar", function() { QUnit.test( "#9315: jumps down with offset of scrollbar", function( assert ) {
expect( 2 ); assert.expect( 2 );
var element = $( "#draggable2" ).draggable( { var element = $( "#draggable2" ).draggable( {
stop: function( event, ui ) { stop: function( event, ui ) {
equal( ui.position.left, 11, "left position is correct when position is absolute" ); assert.equal( ui.position.left, 11, "left position is correct when position is absolute" );
equal( ui.position.top, 11, "top position is correct when position is absolute" ); assert.equal( ui.position.top, 11, "top position is correct when position is absolute" );
$( "html" ).scrollTop( 0 ).scrollLeft( 0 ); $( "html" ).scrollTop( 0 ).scrollLeft( 0 );
} }
} ); } );
@ -177,8 +178,8 @@ test( "#9315: jumps down with offset of scrollbar", function() {
} ); } );
} ); } );
test( "scroll offset with fixed ancestors", function() { QUnit.test( "scroll offset with fixed ancestors", function( assert ) {
expect( 2 ); assert.expect( 2 );
var startValue = 300, var startValue = 300,
element = $( "#draggable1" ) element = $( "#draggable1" )
@ -196,8 +197,8 @@ test( "scroll offset with fixed ancestors", function() {
$( document ).scrollTop( startValue ).scrollLeft( startValue ); $( document ).scrollTop( startValue ).scrollLeft( startValue );
}, },
stop: function( event, ui ) { stop: function( event, ui ) {
equal( ui.position.left, 10, "left position is correct when parent position is fixed" ); assert.equal( ui.position.left, 10, "left position is correct when parent position is fixed" );
equal( ui.position.top, 10, "top position is correct when parent position is fixed" ); assert.equal( ui.position.top, 10, "top position is correct when parent position is fixed" );
$( document ).scrollTop( 0 ).scrollLeft( 0 ); $( document ).scrollTop( 0 ).scrollLeft( 0 );
} }
} ); } );
@ -219,8 +220,8 @@ $( [ "hidden", "auto", "scroll" ] ).each( function() {
// Http://bugs.jqueryui.com/ticket/9379 - position bug in scrollable div // Http://bugs.jqueryui.com/ticket/9379 - position bug in scrollable div
// http://bugs.jqueryui.com/ticket/10147 - Wrong position in a parent with "overflow: hidden" // http://bugs.jqueryui.com/ticket/10147 - Wrong position in a parent with "overflow: hidden"
test( "position in scrollable parent with overflow: " + overflow, function() { QUnit.test( "position in scrollable parent with overflow: " + overflow, function( assert ) {
expect( 2 ); assert.expect( 2 );
$( "#qunit-fixture" ).html( "<div id='outer'><div id='inner'></div><div id='dragged'>a</div></div>" ); $( "#qunit-fixture" ).html( "<div id='outer'><div id='inner'></div><div id='dragged'>a</div></div>" );
$( "#inner" ).css( { position: "absolute", width: "500px", height: "500px" } ); $( "#inner" ).css( { position: "absolute", width: "500px", height: "500px" } );
@ -240,8 +241,8 @@ $( [ "hidden", "auto", "scroll" ] ).each( function() {
$( "#outer" ).scrollTop( startValue ).scrollLeft( startValue ); $( "#outer" ).scrollTop( startValue ).scrollLeft( startValue );
}, },
stop: function( event, ui ) { stop: function( event, ui ) {
equal( ui.position.left, expected, "left position is correct when grandparent is scrolled" ); assert.equal( ui.position.left, expected, "left position is correct when grandparent is scrolled" );
equal( ui.position.top, expected, "top position is correct when grandparent is scrolled" ); assert.equal( ui.position.top, expected, "top position is correct when grandparent is scrolled" );
} }
} ); } );
@ -255,8 +256,8 @@ $( [ "hidden", "auto", "scroll" ] ).each( function() {
} ); } );
} ); } );
test( "#5727: draggable from iframe", function() { QUnit.test( "#5727: draggable from iframe", function( assert ) {
expect( 1 ); assert.expect( 1 );
var iframeBody, draggable1, var iframeBody, draggable1,
iframe = $( "<iframe />" ).appendTo( "#qunit-fixture" ), iframe = $( "<iframe />" ).appendTo( "#qunit-fixture" ),
@ -270,53 +271,55 @@ test( "#5727: draggable from iframe", function() {
draggable1.draggable(); draggable1.draggable();
equal( draggable1.closest( iframeBody ).length, 1 ); assert.equal( draggable1.closest( iframeBody ).length, 1 );
// TODO: fix draggable within an IFRAME to fire events on the element properly // TODO: fix draggable within an IFRAME to fire events on the element properly
// and these testHelper.shouldMove relies on events for testing // and these testHelper.shouldMove relies on events for testing
//testHelper.shouldMove( draggable1, "draggable from an iframe" ); //testHelper.shouldMove( assert, draggable1, "draggable from an iframe" );
} ); } );
test( "#8399: A draggable should become the active element after you are finished interacting with it, but not before.", function() { QUnit.test( "#8399: A draggable should become the active element after you are finished interacting with it, but not before.", function( assert ) {
expect( 2 ); assert.expect( 2 );
var element = $( "<a href='#'>link</a>" ).appendTo( "#qunit-fixture" ).draggable(); var element = $( "<a href='#'>link</a>" ).appendTo( "#qunit-fixture" ).draggable();
$( document ).one( "mousemove", function() { $( document ).one( "mousemove", function() {
notStrictEqual( document.activeElement, element.get( 0 ), "moving a draggable anchor did not make it the active element" ); assert.notStrictEqual( document.activeElement, element.get( 0 ), "moving a draggable anchor did not make it the active element" );
} ); } );
testHelper.move( element, 50, 50 ); testHelper.move( element, 50, 50 );
strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" ); assert.strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" );
} ); } );
asyncTest( "blur behavior - handle is main element", function() { QUnit.test( "blur behavior - handle is main element", function( assert ) {
expect( 3 ); var ready = assert.async();
assert.expect( 3 );
var element = $( "#draggable1" ).draggable(), var element = $( "#draggable1" ).draggable(),
focusElement = $( "<div tabindex='1'></div>" ).appendTo( element ); focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
testHelper.onFocus( focusElement, function() { testHelper.onFocus( focusElement, function() {
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" ); assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
testHelper.move( focusElement, 1, 1 ); testHelper.move( focusElement, 1, 1 );
// Http://bugs.jqueryui.com/ticket/10527 // Http://bugs.jqueryui.com/ticket/10527
// Draggable: Can't select option in modal dialog (IE8) // Draggable: Can't select option in modal dialog (IE8)
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused after mousing down on itself" ); assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused after mousing down on itself" );
testHelper.move( element, 50, 50 ); testHelper.move( element, 50, 50 );
// Http://bugs.jqueryui.com/ticket/4261 // Http://bugs.jqueryui.com/ticket/4261
// active element should blur when mousing down on a draggable // active element should blur when mousing down on a draggable
notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" ); assert.notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
start(); ready();
} ); } );
} ); } );
asyncTest( "blur behavior - descendant of handle", function() { QUnit.test( "blur behavior - descendant of handle", function( assert ) {
expect( 2 ); var ready = assert.async();
assert.expect( 2 );
var element = $( "#draggable2" ).draggable( { handle: "span" } ), var element = $( "#draggable2" ).draggable( { handle: "span" } ),
@ -325,18 +328,18 @@ asyncTest( "blur behavior - descendant of handle", function() {
focusElement = $( "<div tabindex='1'></div>" ).appendTo( element ); focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
testHelper.onFocus( focusElement, function() { testHelper.onFocus( focusElement, function() {
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" ); assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
testHelper.move( handle, 50, 50 ); testHelper.move( handle, 50, 50 );
// Elements outside of the handle should blur (#12472, #14905) // Elements outside of the handle should blur (#12472, #14905)
notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" ); assert.notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
start(); ready();
} ); } );
} ); } );
test( "ui-draggable-handle assigned to appropriate element", function( assert ) { QUnit.test( "ui-draggable-handle assigned to appropriate element", function( assert ) {
expect( 5 ); assert.expect( 5 );
var p = $( "<p>" ).appendTo( "#qunit-fixture" ), var p = $( "<p>" ).appendTo( "#qunit-fixture" ),
element = $( "<div><p></p></div>" ).appendTo( "#qunit-fixture" ).draggable(); element = $( "<div><p></p></div>" ).appendTo( "#qunit-fixture" ).draggable();
@ -352,8 +355,8 @@ test( "ui-draggable-handle assigned to appropriate element", function( assert )
assert.lacksClasses( element.find( "p" ), "ui-draggable-handle" ); assert.lacksClasses( element.find( "p" ), "ui-draggable-handle" );
} ); } );
test( "ui-draggable-handle managed correctly in nested draggables", function( assert ) { QUnit.test( "ui-draggable-handle managed correctly in nested draggables", function( assert ) {
expect( 4 ); assert.expect( 4 );
var parent = $( "<div><div></div></div>" ).draggable().appendTo( "#qunit-fixture" ), var parent = $( "<div><div></div></div>" ).draggable().appendTo( "#qunit-fixture" ),
child = parent.find( "div" ).draggable(); child = parent.find( "div" ).draggable();
@ -371,7 +374,7 @@ test( "ui-draggable-handle managed correctly in nested draggables", function( as
QUnit[ document.documentMode === 8 ? "skip" : "test" ]( QUnit[ document.documentMode === 8 ? "skip" : "test" ](
"does not stop propagation to window", "does not stop propagation to window",
function( assert ) { function( assert ) {
expect( 1 ); assert.expect( 1 );
var element = $( "#draggable1" ).draggable(); var element = $( "#draggable1" ).draggable();
var handler = function() { var handler = function() {

View File

@ -1,21 +1,22 @@
define( [ define( [
"qunit",
"jquery", "jquery",
"ui/widgets/draggable" "ui/widgets/draggable"
], function( $ ) { ], function( QUnit, $ ) {
var element; var element;
module( "draggable: events", { QUnit.module( "draggable: events", {
setup: function() { beforeEach: function() {
element = $( "<div>" ).appendTo( "#qunit-fixture" ); element = $( "<div>" ).appendTo( "#qunit-fixture" );
}, },
teardown: function() { afterEach: function() {
element.draggable( "destroy" ); element.draggable( "destroy" );
} }
} ); } );
test( "callbacks occurrence count", function() { QUnit.test( "callbacks occurrence count", function( assert ) {
expect( 3 ); assert.expect( 3 );
var start = 0, var start = 0,
stop = 0, stop = 0,
@ -38,13 +39,13 @@ test( "callbacks occurrence count", function() {
dy: 10 dy: 10
} ); } );
equal( start, 1, "start callback should happen exactly once" ); assert.equal( start, 1, "start callback should happen exactly once" );
equal( dragc, 3, "drag callback should happen exactly once per mousemove" ); assert.equal( dragc, 3, "drag callback should happen exactly once per mousemove" );
equal( stop, 1, "stop callback should happen exactly once" ); assert.equal( stop, 1, "stop callback should happen exactly once" );
} ); } );
test( "stopping the start callback", function() { QUnit.test( "stopping the start callback", function( assert ) {
expect( 3 ); assert.expect( 3 );
var start = 0, var start = 0,
stop = 0, stop = 0,
@ -68,13 +69,13 @@ test( "stopping the start callback", function() {
dy: 10 dy: 10
} ); } );
equal( start, 1, "start callback should happen exactly once" ); assert.equal( start, 1, "start callback should happen exactly once" );
equal( dragc, 0, "drag callback should not happen at all" ); assert.equal( dragc, 0, "drag callback should not happen at all" );
equal( stop, 0, "stop callback should not happen if there wasnt even a start" ); assert.equal( stop, 0, "stop callback should not happen if there wasnt even a start" );
} ); } );
test( "stopping the drag callback", function() { QUnit.test( "stopping the drag callback", function( assert ) {
expect( 2 ); assert.expect( 2 );
var start = 0, var start = 0,
stop = 0, stop = 0,
@ -98,12 +99,12 @@ test( "stopping the drag callback", function() {
dy: 10 dy: 10
} ); } );
equal( start, 1, "start callback should happen exactly once" ); assert.equal( start, 1, "start callback should happen exactly once" );
equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" ); assert.equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" );
} ); } );
test( "stopping the stop callback", function() { QUnit.test( "stopping the stop callback", function( assert ) {
expect( 1 ); assert.expect( 1 );
element.draggable( { element.draggable( {
helper: "clone", helper: "clone",
@ -117,13 +118,13 @@ test( "stopping the stop callback", function() {
dy: 10 dy: 10
} ); } );
ok( element.draggable( "instance" ).helper, "the clone should not be deleted if the stop callback is stopped" ); assert.ok( element.draggable( "instance" ).helper, "the clone should not be deleted if the stop callback is stopped" );
} ); } );
// http://bugs.jqueryui.com/ticket/6884 // http://bugs.jqueryui.com/ticket/6884
// Draggable: ui.offset.left differs between the "start" and "drag" hooks // Draggable: ui.offset.left differs between the "start" and "drag" hooks
test( "position and offset in hash is consistent between start, drag, and stop", function() { QUnit.test( "position and offset in hash is consistent between start, drag, and stop", function( assert ) {
expect( 4 ); assert.expect( 4 );
var startPos, startOffset, dragPos, dragOffset, stopPos, stopOffset; var startPos, startOffset, dragPos, dragOffset, stopPos, stopOffset;
@ -155,10 +156,10 @@ test( "position and offset in hash is consistent between start, drag, and stop",
startOffset.left += 10; startOffset.left += 10;
startOffset.top += 10; startOffset.top += 10;
deepEqual( startPos, dragPos, "start position equals drag position plus distance" ); assert.deepEqual( startPos, dragPos, "start position equals drag position plus distance" );
deepEqual( dragPos, stopPos, "drag position equals stop position" ); assert.deepEqual( dragPos, stopPos, "drag position equals stop position" );
deepEqual( startOffset, dragOffset, "start offset equals drag offset plus distance" ); assert.deepEqual( startOffset, dragOffset, "start offset equals drag offset plus distance" );
deepEqual( dragOffset, stopOffset, "drag offset equals stop offset" ); assert.deepEqual( dragOffset, stopOffset, "drag offset equals stop offset" );
} ); } );
} ); } );

View File

@ -1,8 +1,9 @@
define( [ define( [
"qunit",
"jquery", "jquery",
"lib/helper", "lib/helper",
"ui/widgets/draggable" "ui/widgets/draggable"
], function( $, helper ) { ], function( QUnit, $, helper ) {
return $.extend( helper, { return $.extend( helper, {
@ -16,40 +17,40 @@ return $.extend( helper, {
return $.contains( element[ 0 ].ownerDocument, element[ 0 ] ); return $.contains( element[ 0 ].ownerDocument, element[ 0 ] );
} )(), } )(),
testDragPosition: function( el, dx, dy, expectedDX, expectedDY, msg ) { testDragPosition: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : ""; msg = msg ? msg + "." : "";
$( el ).one( "dragstop", function( event, ui ) { $( el ).one( "dragstop", function( event, ui ) {
var positionExpected = { left: ui.originalPosition.left + expectedDX, top: ui.originalPosition.top + expectedDY }; var positionExpected = { left: ui.originalPosition.left + expectedDX, top: ui.originalPosition.top + expectedDY };
deepEqual( ui.position, positionExpected, "position dragged[" + dx + ", " + dy + "] " + msg ); assert.deepEqual( ui.position, positionExpected, "position dragged[" + dx + ", " + dy + "] " + msg );
} ); } );
}, },
testDragOffset: function( el, dx, dy, expectedDX, expectedDY, msg ) { testDragOffset: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : ""; msg = msg ? msg + "." : "";
var offsetBefore = el.offset(), var offsetBefore = el.offset(),
offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY }; offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
$( el ).one( "dragstop", function( event, ui ) { $( el ).one( "dragstop", function( event, ui ) {
deepEqual( ui.offset, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg ); assert.deepEqual( ui.offset, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
} ); } );
}, },
testDragHelperOffset: function( el, dx, dy, expectedDX, expectedDY, msg ) { testDragHelperOffset: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : ""; msg = msg ? msg + "." : "";
var offsetBefore = el.offset(), var offsetBefore = el.offset(),
offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY }; offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
$( el ).one( "dragstop", function( event, ui ) { $( el ).one( "dragstop", function( event, ui ) {
deepEqual( ui.helper.offset(), offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg ); assert.deepEqual( ui.helper.offset(), offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
} ); } );
}, },
testDrag: function( el, handle, dx, dy, expectedDX, expectedDY, msg ) { testDrag: function( assert, el, handle, dx, dy, expectedDX, expectedDY, msg ) {
this.testDragPosition( el, dx, dy, expectedDX, expectedDY, msg ); this.testDragPosition( assert, el, dx, dy, expectedDX, expectedDY, msg );
this.testDragOffset( el, dx, dy, expectedDX, expectedDY, msg ); this.testDragOffset( assert, el, dx, dy, expectedDX, expectedDY, msg );
$( handle ).simulate( "drag", { $( handle ).simulate( "drag", {
dx: dx, dx: dx,
@ -57,10 +58,10 @@ return $.extend( helper, {
} ); } );
}, },
shouldMovePositionButNotOffset: function( el, msg, handle ) { shouldMovePositionButNotOffset: function( assert, el, msg, handle ) {
handle = handle || el; handle = handle || el;
this.testDragPosition( el, 100, 100, 100, 100, msg ); this.testDragPosition( assert, el, 100, 100, 100, 100, msg );
this.testDragHelperOffset( el, 100, 100, 0, 0, msg ); this.testDragHelperOffset( assert, el, 100, 100, 0, 0, msg );
$( handle ).simulate( "drag", { $( handle ).simulate( "drag", {
dx: 100, dx: 100,
@ -68,17 +69,17 @@ return $.extend( helper, {
} ); } );
}, },
shouldMove: function( el, msg, handle ) { shouldMove: function( assert, el, msg, handle ) {
handle = handle || el; handle = handle || el;
this.testDrag( el, handle, 100, 100, 100, 100, msg ); this.testDrag( assert, el, handle, 100, 100, 100, 100, msg );
}, },
shouldNotMove: function( el, msg, handle ) { shouldNotMove: function( assert, el, msg, handle ) {
handle = handle || el; handle = handle || el;
this.testDrag( el, handle, 100, 100, 0, 0, msg ); this.testDrag( assert, el, handle, 100, 100, 0, 0, msg );
}, },
shouldNotDrag: function( el, msg, handle ) { shouldNotDrag: function( assert, el, msg, handle ) {
handle = handle || el; handle = handle || el;
var newOffset, var newOffset,
@ -86,7 +87,7 @@ return $.extend( helper, {
beginOffset = element.offset(); beginOffset = element.offset();
element.on( "dragstop", function() { element.on( "dragstop", function() {
ok( false, "should not drag " + msg ); assert.ok( false, "should not drag " + msg );
} ); } );
$( handle ).simulate( "drag", { $( handle ).simulate( "drag", {
@ -98,8 +99,8 @@ return $.extend( helper, {
// Also assert that draggable did not move, to ensure it isn't just // Also assert that draggable did not move, to ensure it isn't just
// that drag did not fire and draggable still somehow moved // that drag did not fire and draggable still somehow moved
equal( newOffset.left, beginOffset.left, "Offset left should not be different" ); assert.equal( newOffset.left, beginOffset.left, "Offset left should not be different" );
equal( newOffset.top, beginOffset.top, "Offset top should not be different" ); assert.equal( newOffset.top, beginOffset.top, "Offset top should not be different" );
element.off( "dragstop" ); element.off( "dragstop" );
}, },
@ -109,10 +110,10 @@ return $.extend( helper, {
$( what ).css( { overflow: overflow, overflowX: overflow, overflowY: overflow } ); $( what ).css( { overflow: overflow, overflowX: overflow, overflowY: overflow } );
}, },
testScroll: function( el, position ) { testScroll: function( assert, el, position ) {
var oldPosition = $( "#main" ).css( "position" ); var oldPosition = $( "#main" ).css( "position" );
$( "#main" ).css( { position: position, top: "0px", left: "0px" } ); $( "#main" ).css( { position: position, top: "0px", left: "0px" } );
this.shouldMove( el, position + " parent" ); this.shouldMove( assert, el, position + " parent" );
$( "#main" ).css( "position", oldPosition ); $( "#main" ).css( "position", oldPosition );
}, },

View File

@ -1,104 +1,105 @@
define( [ define( [
"qunit",
"jquery", "jquery",
"./helper", "./helper",
"ui/widgets/draggable" "ui/widgets/draggable"
], function( $, testHelper ) { ], function( QUnit, $, testHelper ) {
var element; var element;
module( "draggable: methods", { QUnit.module( "draggable: methods", {
setup: function() { beforeEach: function() {
element = $( "<div style='background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;'><span>Absolute</span></div>" ).appendTo( "#qunit-fixture" ); element = $( "<div style='background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;'><span>Absolute</span></div>" ).appendTo( "#qunit-fixture" );
}, },
teardown: function() { afterEach: function() {
element.remove(); element.remove();
} }
} ); } );
test( "init", function() { QUnit.test( "init", function( assert ) {
expect( 5 ); assert.expect( 5 );
element.draggable(); element.draggable();
ok( true, ".draggable() called on element" ); assert.ok( true, ".draggable() called on element" );
$( [] ).draggable(); $( [] ).draggable();
ok( true, ".draggable() called on empty collection" ); assert.ok( true, ".draggable() called on empty collection" );
$( "<div></div>" ).draggable(); $( "<div></div>" ).draggable();
ok( true, ".draggable() called on disconnected DOMElement" ); assert.ok( true, ".draggable() called on disconnected DOMElement" );
element.draggable( "option", "foo" ); element.draggable( "option", "foo" );
ok( true, "arbitrary option getter after init" ); assert.ok( true, "arbitrary option getter after init" );
element.draggable( "option", "foo", "bar" ); element.draggable( "option", "foo", "bar" );
ok( true, "arbitrary option setter after init" ); assert.ok( true, "arbitrary option setter after init" );
} ); } );
test( "destroy", function() { QUnit.test( "destroy", function( assert ) {
expect( 4 ); assert.expect( 4 );
element.draggable().draggable( "destroy" ); element.draggable().draggable( "destroy" );
ok( true, ".draggable('destroy') called on element" ); assert.ok( true, ".draggable('destroy') called on element" );
$( [] ).draggable().draggable( "destroy" ); $( [] ).draggable().draggable( "destroy" );
ok( true, ".draggable('destroy') called on empty collection" ); assert.ok( true, ".draggable('destroy') called on empty collection" );
element.draggable().draggable( "destroy" ); element.draggable().draggable( "destroy" );
ok( true, ".draggable('destroy') called on disconnected DOMElement" ); assert.ok( true, ".draggable('destroy') called on disconnected DOMElement" );
var expected = element.draggable(), var expected = element.draggable(),
actual = expected.draggable( "destroy" ); actual = expected.draggable( "destroy" );
equal( actual, expected, "destroy is chainable" ); assert.equal( actual, expected, "destroy is chainable" );
} ); } );
test( "enable", function() { QUnit.test( "enable", function( assert ) {
expect( 11 ); assert.expect( 11 );
element.draggable( { disabled: true } ); element.draggable( { disabled: true } );
testHelper.shouldNotDrag( element, ".draggable({ disabled: true })" ); testHelper.shouldNotDrag( assert, element, ".draggable({ disabled: true })" );
element.draggable( "enable" ); element.draggable( "enable" );
testHelper.shouldMove( element, ".draggable('enable')" ); testHelper.shouldMove( assert, element, ".draggable('enable')" );
equal( element.draggable( "option", "disabled" ), false, "disabled option getter" ); assert.equal( element.draggable( "option", "disabled" ), false, "disabled option getter" );
element.draggable( "destroy" ); element.draggable( "destroy" );
element.draggable( { disabled: true } ); element.draggable( { disabled: true } );
testHelper.shouldNotDrag( element, ".draggable({ disabled: true })" ); testHelper.shouldNotDrag( assert, element, ".draggable({ disabled: true })" );
element.draggable( "option", "disabled", false ); element.draggable( "option", "disabled", false );
equal( element.draggable( "option", "disabled" ), false, "disabled option setter" ); assert.equal( element.draggable( "option", "disabled" ), false, "disabled option setter" );
testHelper.shouldMove( element, ".draggable('option', 'disabled', false)" ); testHelper.shouldMove( assert, element, ".draggable('option', 'disabled', false)" );
var expected = element.draggable(), var expected = element.draggable(),
actual = expected.draggable( "enable" ); actual = expected.draggable( "enable" );
equal( actual, expected, "enable is chainable" ); assert.equal( actual, expected, "enable is chainable" );
} ); } );
test( "disable", function( assert ) { QUnit.test( "disable", function( assert ) {
expect( 14 ); assert.expect( 14 );
element = $( "#draggable2" ).draggable( { disabled: false } ); element = $( "#draggable2" ).draggable( { disabled: false } );
testHelper.shouldMove( element, ".draggable({ disabled: false })" ); testHelper.shouldMove( assert, element, ".draggable({ disabled: false })" );
element.draggable( "disable" ); element.draggable( "disable" );
testHelper.shouldNotDrag( element, ".draggable('disable')" ); testHelper.shouldNotDrag( assert, element, ".draggable('disable')" );
equal( element.draggable( "option", "disabled" ), true, "disabled option getter" ); assert.equal( element.draggable( "option", "disabled" ), true, "disabled option getter" );
element.draggable( "destroy" ); element.draggable( "destroy" );
element.draggable( { disabled: false } ); element.draggable( { disabled: false } );
testHelper.shouldMove( element, ".draggable({ disabled: false })" ); testHelper.shouldMove( assert, element, ".draggable({ disabled: false })" );
element.draggable( "option", "disabled", true ); element.draggable( "option", "disabled", true );
equal( element.draggable( "option", "disabled" ), true, "disabled option setter" ); assert.equal( element.draggable( "option", "disabled" ), true, "disabled option setter" );
testHelper.shouldNotDrag( element, ".draggable('option', 'disabled', true)" ); testHelper.shouldNotDrag( assert, element, ".draggable('option', 'disabled', true)" );
assert.lacksClasses( element.draggable( "widget" ), "ui-state-disabled" ); assert.lacksClasses( element.draggable( "widget" ), "ui-state-disabled" );
ok( !element.draggable( "widget" ).attr( "aria-disabled" ), "element does not get aria-disabled" ); assert.ok( !element.draggable( "widget" ).attr( "aria-disabled" ), "element does not get aria-disabled" );
assert.hasClasses( element.draggable( "widget" ), "ui-draggable-disabled" ); assert.hasClasses( element.draggable( "widget" ), "ui-draggable-disabled" );
var expected = element.draggable(), var expected = element.draggable(),
actual = expected.draggable( "disable" ); actual = expected.draggable( "disable" );
equal( actual, expected, "disable is chainable" ); assert.equal( actual, expected, "disable is chainable" );
} ); } );
} ); } );

File diff suppressed because it is too large Load Diff