jquery-ui/tests/unit/resizable/events.js

244 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

define( [
2016-04-06 13:11:09 +00:00
"qunit",
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/resizable"
2016-04-06 13:11:09 +00:00
], function( QUnit, $, testHelper ) {
2016-04-06 13:11:09 +00:00
QUnit.module( "resizable: events" );
2016-04-06 13:11:09 +00:00
QUnit.test( "start", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 5 );
var count = 0,
handle = ".ui-resizable-se";
2015-08-24 12:58:35 +00:00
$( "#resizable1" ).resizable( {
handles: "all",
2015-08-24 12:58:35 +00:00
start: function( event, ui ) {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 100, "compare width" );
assert.equal( ui.size.height, 100, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
count++;
}
2015-08-24 12:58:35 +00:00
} );
2015-08-24 12:58:35 +00:00
testHelper.drag( handle, 50, 50 );
2016-04-06 13:11:09 +00:00
assert.equal( count, 1, "start callback should happen exactly once" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "resize", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 9 );
var count = 0,
handle = ".ui-resizable-se";
2015-08-24 12:58:35 +00:00
$( "#resizable1" ).resizable( {
handles: "all",
resize: function( event, ui ) {
if ( count === 0 ) {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 125, "compare width" );
assert.equal( ui.size.height, 125, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
} else {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 150, "compare width" );
assert.equal( ui.size.height, 150, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
}
count++;
}
2015-08-24 12:58:35 +00:00
} );
testHelper.drag( handle, 50, 50 );
2016-04-06 13:11:09 +00:00
assert.equal( count, 2, "resize callback should happen exactly once per size adjustment" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "resize (min/max dimensions)", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 5 );
var count = 0,
handle = ".ui-resizable-se";
2015-08-24 12:58:35 +00:00
$( "#resizable1" ).resizable( {
handles: "all",
minWidth: 60,
minHeight: 60,
maxWidth: 100,
maxHeight: 100,
resize: function( event, ui ) {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 60, "compare width" );
assert.equal( ui.size.height, 60, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
count++;
}
2015-08-24 12:58:35 +00:00
} );
testHelper.drag( handle, -200, -200 );
2016-04-06 13:11:09 +00:00
assert.equal( count, 1, "resize callback should happen exactly once per size adjustment" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "resize (containment)", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 5 );
var count = 0,
handle = ".ui-resizable-se",
2015-08-24 12:58:35 +00:00
container = $( "#resizable1" ).wrap( "<div>" ).parent().css( {
height: "100px",
width: "100px"
2015-08-24 12:58:35 +00:00
} );
2015-08-24 12:58:35 +00:00
$( "#resizable1" ).resizable( {
handles: "all",
containment: container,
resize: function( event, ui ) {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 10, "compare width" );
assert.equal( ui.size.height, 10, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
count++;
}
2015-08-24 12:58:35 +00:00
} );
// Prove you can't resize outside containment by dragging southeast corner southeast
testHelper.drag( handle, 100, 100 );
// Prove you can't resize outside containment by dragging southeast corner northwest
testHelper.drag( handle, -200, -200 );
2016-04-06 13:11:09 +00:00
assert.equal( count, 1, "resize callback should happen exactly once per size adjustment" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "resize (grid)", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 5 );
var count = 0,
handle = ".ui-resizable-se";
2015-08-24 12:58:35 +00:00
$( "#resizable1" ).resizable( {
handles: "all",
grid: 50,
2015-08-24 12:58:35 +00:00
resize: function( event, ui ) {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 150, "compare width" );
assert.equal( ui.size.height, 150, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
count++;
}
2015-08-24 12:58:35 +00:00
} );
2015-08-24 12:58:35 +00:00
testHelper.drag( handle, 50, 50 );
2016-04-06 13:11:09 +00:00
assert.equal( count, 1, "resize callback should happen exactly once per grid-unit size adjustment" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "resize, custom adjustment", function( assert ) {
assert.expect( 4 );
var handle = ".ui-resizable-se",
2015-08-24 12:58:35 +00:00
element = $( "#resizable1" ).resizable( {
resize: function( event, ui ) {
ui.size.width = 100;
ui.size.height = 200;
ui.position.left = 300;
ui.position.top = 400;
}
2015-08-24 12:58:35 +00:00
} );
testHelper.drag( handle, 50, 50 );
2016-04-06 13:11:09 +00:00
assert.equal( element.width(), 100, "resize event can control width" );
assert.equal( element.height(), 200, "resize event can control height" );
assert.equal( element.position().left, 300, "resize event can control left" );
assert.equal( element.position().top, 400, "resize event can control top" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "stop", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 5 );
var count = 0,
handle = ".ui-resizable-se";
2015-08-24 12:58:35 +00:00
$( "#resizable1" ).resizable( {
handles: "all",
2015-08-24 12:58:35 +00:00
stop: function( event, ui ) {
2016-04-06 13:11:09 +00:00
assert.equal( ui.size.width, 150, "compare width" );
assert.equal( ui.size.height, 150, "compare height" );
assert.equal( ui.originalSize.width, 100, "compare original width" );
assert.equal( ui.originalSize.height, 100, "compare original height" );
count++;
}
2015-08-24 12:58:35 +00:00
} );
2015-08-24 12:58:35 +00:00
testHelper.drag( handle, 50, 50 );
2016-04-06 13:11:09 +00:00
assert.equal( count, 1, "stop callback should happen exactly once" );
2015-08-24 12:58:35 +00:00
} );
2016-04-06 13:11:09 +00:00
QUnit.test( "resize (containment) works with parent with negative offset", function( assert ) {
2016-04-06 13:11:09 +00:00
assert.expect( 1 );
var widthBefore, widthAfter,
handle = ".ui-resizable-e",
target = $( "#resizable1" ),
absoluteContainer = target.wrap( "<div />" ).parent(),
fixedContainer = absoluteContainer.wrap( "<div />" ).parent(),
increaseWidthBy = 50;
// Position fixed container in window top left
2015-08-24 12:58:35 +00:00
fixedContainer.css( {
width: 400,
height: 100,
position: "fixed",
top: 0,
left: 0
2015-08-24 12:58:35 +00:00
} );
// Position absolute container within fixed on slightly outside window
2015-08-24 12:58:35 +00:00
absoluteContainer.css( {
width: 400,
height: 100,
position: "absolute",
top: 0,
left: -50
2015-08-24 12:58:35 +00:00
} );
// Set up resizable to be contained within absolute container
2015-08-24 12:58:35 +00:00
target.resizable( {
handles: "all",
containment: "parent"
2015-08-24 12:58:35 +00:00
} ).css( {
width: 300
2015-08-24 12:58:35 +00:00
} );
widthBefore = target.width();
testHelper.drag( handle, increaseWidthBy, 0 );
widthAfter = target.width();
2016-04-06 13:11:09 +00:00
assert.equal( widthAfter, ( widthBefore + increaseWidthBy ), "resizable width should be increased by the value dragged" );
2015-08-24 12:58:35 +00:00
} );
} );