jquery-ui/tests/unit/sortable/methods.js

131 lines
3.9 KiB
JavaScript
Raw Normal View History

define( [
2016-04-06 13:22:59 +00:00
"qunit",
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/sortable"
2016-04-06 13:22:59 +00:00
], function( QUnit, $, testHelper ) {
2016-04-06 13:22:59 +00:00
QUnit.module( "sortable: methods" );
2016-04-06 13:22:59 +00:00
QUnit.test( "init", function( assert ) {
assert.expect( 5 );
2015-08-24 12:57:53 +00:00
$( "<div></div>" ).appendTo( "body" ).sortable().remove();
2016-04-06 13:22:59 +00:00
assert.ok( true, ".sortable() called on element" );
2015-08-24 12:57:53 +00:00
$( [] ).sortable();
2016-04-06 13:22:59 +00:00
assert.ok( true, ".sortable() called on empty collection" );
2015-08-24 12:57:53 +00:00
$( "<div></div>" ).sortable();
2016-04-06 13:22:59 +00:00
assert.ok( true, ".sortable() called on disconnected DOMElement" );
2015-08-24 12:57:53 +00:00
$( "<div></div>" ).sortable().sortable( "option", "foo" );
2016-04-06 13:22:59 +00:00
assert.ok( true, "arbitrary option getter after init" );
2015-08-24 12:57:53 +00:00
$( "<div></div>" ).sortable().sortable( "option", "foo", "bar" );
2016-04-06 13:22:59 +00:00
assert.ok( true, "arbitrary option setter after init" );
2015-08-24 12:57:53 +00:00
} );
2016-04-06 13:22:59 +00:00
QUnit.test( "destroy", function( assert ) {
assert.expect( 4 );
2015-08-24 12:57:53 +00:00
$( "<div></div>" ).appendTo( "body" ).sortable().sortable( "destroy" ).remove();
2016-04-06 13:22:59 +00:00
assert.ok( true, ".sortable('destroy') called on element" );
2015-08-24 12:57:53 +00:00
$( [] ).sortable().sortable( "destroy" );
2016-04-06 13:22:59 +00:00
assert.ok( true, ".sortable('destroy') called on empty collection" );
2015-08-24 12:57:53 +00:00
$( "<div></div>" ).sortable().sortable( "destroy" );
2016-04-06 13:22:59 +00:00
assert.ok( true, ".sortable('destroy') called on disconnected DOMElement" );
2015-08-24 12:57:53 +00:00
var expected = $( "<div></div>" ).sortable(),
actual = expected.sortable( "destroy" );
2016-04-06 13:22:59 +00:00
assert.equal( actual, expected, "destroy is chainable" );
2015-08-24 12:57:53 +00:00
} );
2016-04-06 13:22:59 +00:00
QUnit.test( "enable", function( assert ) {
assert.expect( 5 );
var el, actual, expected;
2015-08-24 12:57:53 +00:00
el = $( "#sortable" ).sortable( { disabled: true } );
2016-04-06 13:22:59 +00:00
testHelper.sort( assert, $( "li", el )[ 0 ], 0, 44, 0, ".sortable({ disabled: true })" );
2015-08-24 12:57:53 +00:00
el.sortable( "enable" );
2016-04-06 13:22:59 +00:00
assert.equal( el.sortable( "option", "disabled" ), false, "disabled option getter" );
2015-08-24 12:57:53 +00:00
el.sortable( "destroy" );
el.sortable( { disabled: true } );
el.sortable( "option", "disabled", false );
2016-04-06 13:22:59 +00:00
assert.equal( el.sortable( "option", "disabled" ), false, "disabled option setter" );
2016-04-06 13:22:59 +00:00
testHelper.sort( assert, $( "li", el )[ 0 ], 0, 44, 2, ".sortable('option', 'disabled', false)" );
2015-08-24 12:57:53 +00:00
expected = $( "<div></div>" ).sortable(),
actual = expected.sortable( "enable" );
2016-04-06 13:22:59 +00:00
assert.equal( actual, expected, "enable is chainable" );
2015-08-24 12:57:53 +00:00
} );
2016-04-06 13:22:59 +00:00
QUnit.test( "disable", function( assert ) {
assert.expect( 9 );
var chainable,
2015-08-24 12:57:53 +00:00
element = $( "#sortable" ).sortable( { disabled: false } );
2016-04-06 13:22:59 +00:00
testHelper.sort( assert, $( "li", element )[ 0 ], 0, 44, 2, ".sortable({ disabled: false })" );
chainable = element.sortable( "disable" );
2016-04-06 13:22:59 +00:00
testHelper.sort( assert, $( "li", element )[ 0 ], 0, 44, 0, "disabled.sortable getter" );
element.sortable( "destroy" );
2015-08-24 12:57:53 +00:00
element.sortable( { disabled: false } );
2016-04-06 13:22:59 +00:00
testHelper.sort( assert, $( "li", element )[ 0 ], 0, 44, 2, ".sortable({ disabled: false })" );
2015-08-24 12:57:53 +00:00
element.sortable( "option", "disabled", true );
2016-04-06 13:22:59 +00:00
assert.equal( element.sortable( "option", "disabled" ), true, "disabled option setter" );
assert.lacksClasses( element.sortable( "widget" ), "ui-state-disabled" );
2016-04-06 13:22:59 +00:00
assert.ok( !element.sortable( "widget" ).attr( "aria-disabled" ), "element does not get aria-disabled" );
assert.hasClasses( element.sortable( "widget" ), "ui-sortable-disabled" );
2016-04-06 13:22:59 +00:00
testHelper.sort( assert, $( "li", element )[ 0 ], 0, 44, 0, ".sortable('option', 'disabled', true)" );
assert.equal( chainable, element, "disable is chainable" );
2015-08-24 12:57:53 +00:00
} );
2016-04-06 13:22:59 +00:00
QUnit.test( "refresh() should update the positions of initially empty lists (see #7498)", function( assert ) {
assert.expect( 1 );
var changeCount = 0,
element = $( "#qunit-fixture" ).html( "<ul></ul>" ).find( "ul" );
element
2015-08-24 12:57:53 +00:00
.css( {
"float": "left",
width: "100px"
2015-08-24 12:57:53 +00:00
} )
.sortable( {
change: function() {
changeCount++;
}
2015-08-24 12:57:53 +00:00
} )
.append( "<li>a</li><li>a</li>" )
.find( "li" )
2015-08-24 12:57:53 +00:00
.css( {
"float": "left",
width: "50px",
height: "50px"
2015-08-24 12:57:53 +00:00
} );
element.sortable( "refresh" );
// Switch the order of the two li elements
element.find( "li" ).eq( 0 ).simulate( "drag", {
dx: 55,
moves: 15
2015-08-24 12:57:53 +00:00
} );
2016-04-06 13:22:59 +00:00
assert.equal( changeCount, 1 );
2015-08-24 12:57:53 +00:00
} );
} );