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

273 lines
9.8 KiB
JavaScript
Raw Permalink Normal View History

define( [
2016-04-03 16:10:09 +00:00
"qunit",
"jquery",
"ui/widgets/dialog"
2016-04-03 16:10:09 +00:00
], function( QUnit, $ ) {
2016-04-03 16:10:09 +00:00
QUnit.module( "dialog: methods", {
afterEach: function() {
2015-08-24 12:59:54 +00:00
$( "body>.ui-dialog" ).remove();
2009-09-15 15:42:41 +00:00
}
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "init", function( assert ) {
assert.expect( 6 );
2015-08-24 12:59:54 +00:00
$( "<div></div>" ).appendTo( "body" ).dialog().remove();
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog() called on element" );
2015-08-24 12:59:54 +00:00
$( [] ).dialog().remove();
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog() called on empty collection" );
2015-08-24 12:59:54 +00:00
$( "<div></div>" ).dialog().remove();
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog() called on disconnected DOMElement - never connected" );
2015-08-24 12:59:54 +00:00
$( "<div></div>" ).appendTo( "body" ).remove().dialog().remove();
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog() called on disconnected DOMElement - removed" );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog();
element.dialog( "option", "foo" );
2013-01-31 05:38:20 +00:00
element.remove();
2016-04-03 16:10:09 +00:00
assert.ok( true, "arbitrary option getter after init" );
2015-08-24 12:59:54 +00:00
$( "<div></div>" ).dialog().dialog( "option", "foo", "bar" ).remove();
2016-04-03 16:10:09 +00:00
assert.ok( true, "arbitrary option setter after init" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "destroy", function( assert ) {
assert.expect( 17 );
2013-01-31 05:38:20 +00:00
var element, element2;
$( "#dialog1, #form-dialog" ).hide();
assert.domEqual( "#dialog1", function() {
var dialog = $( "#dialog1" ).dialog().dialog( "destroy" );
2016-04-03 16:10:09 +00:00
assert.equal( dialog.parent()[ 0 ], $( "#qunit-fixture" )[ 0 ] );
assert.equal( dialog.index(), 0 );
2015-08-24 12:59:54 +00:00
} );
assert.domEqual( "#form-dialog", function() {
var dialog = $( "#form-dialog" ).dialog().dialog( "destroy" );
2016-04-03 16:10:09 +00:00
assert.equal( dialog.parent()[ 0 ], $( "#qunit-fixture" )[ 0 ] );
assert.equal( dialog.index(), 2 );
2015-08-24 12:59:54 +00:00
} );
// Ensure dimensions are restored (#8119)
2015-08-24 12:59:54 +00:00
$( "#dialog1" ).show().css( {
width: "400px",
minHeight: "100px",
height: "200px"
2015-08-24 12:59:54 +00:00
} );
assert.domEqual( "#dialog1", function() {
$( "#dialog1" ).dialog().dialog( "destroy" );
2015-08-24 12:59:54 +00:00
} );
// Don't throw errors when destroying a never opened modal dialog (#9004)
2015-08-24 12:59:54 +00:00
$( "#dialog1" ).dialog( { autoOpen: false, modal: true } ).dialog( "destroy" );
2016-04-03 16:10:09 +00:00
assert.equal( $( ".ui-widget-overlay" ).length, 0, "overlay does not exist" );
assert.equal( $( document ).data( "ui-dialog-overlays" ), undefined, "ui-dialog-overlays equals the number of open overlays" );
2015-08-24 12:59:54 +00:00
element = $( "#dialog1" ).dialog( { modal: true } ),
element2 = $( "#dialog2" ).dialog( { modal: true } );
2016-04-03 16:10:09 +00:00
assert.equal( $( ".ui-widget-overlay" ).length, 2, "overlays created when dialogs are open" );
assert.equal( $( document ).data( "ui-dialog-overlays" ), 2, "ui-dialog-overlays equals the number of open overlays" );
2013-01-31 05:38:20 +00:00
element.dialog( "close" );
2016-04-03 16:10:09 +00:00
assert.equal( $( ".ui-widget-overlay" ).length, 1, "overlay remains after closing one dialog" );
assert.equal( $( document ).data( "ui-dialog-overlays" ), 1, "ui-dialog-overlays equals the number of open overlays" );
2013-01-31 05:38:20 +00:00
element.dialog( "destroy" );
2016-04-03 16:10:09 +00:00
assert.equal( $( ".ui-widget-overlay" ).length, 1, "overlay remains after destroying one dialog" );
assert.equal( $( document ).data( "ui-dialog-overlays" ), 1, "ui-dialog-overlays equals the number of open overlays" );
2013-01-31 05:38:20 +00:00
element2.dialog( "destroy" );
2016-04-03 16:10:09 +00:00
assert.equal( $( ".ui-widget-overlay" ).length, 0, "overlays removed when all dialogs are destoryed" );
assert.equal( $( document ).data( "ui-dialog-overlays" ), undefined, "ui-dialog-overlays equals the number of open overlays" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#9000: Dialog leaves broken event handler after close/destroy in certain cases", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
2015-08-24 12:59:54 +00:00
$( "#dialog1" ).dialog( { modal:true } ).dialog( "close" ).dialog( "destroy" );
setTimeout( function() {
$( "#favorite-animal" ).trigger( "focus" );
2016-04-03 16:10:09 +00:00
assert.ok( true, "close and destroy modal dialog before its really opened" );
ready();
2015-08-24 12:59:54 +00:00
} );
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#4980: Destroy should place element back in original DOM position", function( assert ) {
assert.expect( 2 );
2015-08-24 12:59:54 +00:00
var container = $( "<div id='container'><div id='modal'>Content</div></div>" ),
modal = container.find( "#modal" );
modal.dialog();
2016-04-03 16:10:09 +00:00
assert.ok( !$.contains( container[ 0 ], modal[ 0 ] ), "dialog should move modal element to outside container element" );
2015-08-24 12:59:54 +00:00
modal.dialog( "destroy" );
2016-04-03 16:10:09 +00:00
assert.ok( $.contains( container[ 0 ], modal[ 0 ] ), "dialog(destroy) should place element back in original DOM position" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "enable/disable disabled", function( assert ) {
assert.expect( 3 );
2013-01-31 05:38:20 +00:00
var element = $( "<div></div>" ).dialog();
element.dialog( "disable" );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "option", "disabled" ), false, "disable method doesn't do anything" );
assert.lacksClasses( element, "ui-dialog-disabled ui-state-disabled", "disable method doesn't add classes" );
2016-04-03 16:10:09 +00:00
assert.ok( !element.dialog( "widget" ).attr( "aria-disabled" ), "disable method doesn't add aria-disabled" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "close", function( assert ) {
assert.expect( 3 );
2013-01-31 05:38:20 +00:00
var element,
2015-08-24 12:59:54 +00:00
expected = $( "<div></div>" ).dialog(),
actual = expected.dialog( "close" );
2016-04-03 16:10:09 +00:00
assert.equal( actual, expected, "close is chainable" );
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog();
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "widget" ).is( ":visible" ) && !element.dialog( "widget" ).is( ":hidden" ), "dialog visible before close method called" );
2015-08-24 12:59:54 +00:00
element.dialog( "close" );
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "widget" ).is( ":hidden" ) && !element.dialog( "widget" ).is( ":visible" ), "dialog hidden after close method called" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "isOpen", function( assert ) {
assert.expect( 4 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog();
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "isOpen" ), true, "dialog is open after init" );
2015-08-24 12:59:54 +00:00
element.dialog( "close" );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "isOpen" ), false, "dialog is closed" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( { autoOpen: false } );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "isOpen" ), false, "dialog is closed after init" );
2015-08-24 12:59:54 +00:00
element.dialog( "open" );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "isOpen" ), true, "dialog is open" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "moveToTop", function( assert ) {
assert.expect( 5 );
function order() {
2015-08-24 12:59:54 +00:00
var actual = $( ".ui-dialog" ).map( function() {
return +$( this ).css( "z-index" );
2015-08-24 12:59:54 +00:00
} ).get();
2016-04-03 16:10:09 +00:00
assert.deepEqual( actual, $.makeArray( arguments ) );
}
var dialog1, dialog2,
focusOn = "dialog1";
2015-08-24 12:59:54 +00:00
dialog1 = $( "#dialog1" ).dialog( {
focus: function() {
2016-04-03 16:10:09 +00:00
assert.equal( focusOn, "dialog1" );
}
2015-08-24 12:59:54 +00:00
} );
focusOn = "dialog2";
2015-08-24 12:59:54 +00:00
dialog2 = $( "#dialog2" ).dialog( {
focus: function() {
2016-04-03 16:10:09 +00:00
assert.equal( focusOn, "dialog2" );
}
2015-08-24 12:59:54 +00:00
} );
order( 100, 101 );
focusOn = "dialog1";
dialog1.dialog( "moveToTop" );
order( 102, 101 );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "moveToTop: content scroll stays intact", function( assert ) {
assert.expect( 2 );
var otherDialog = $( "#dialog1" ).dialog(),
2015-08-24 12:59:54 +00:00
scrollDialog = $( "#form-dialog" ).dialog( {
height: 200
2015-08-24 12:59:54 +00:00
} );
scrollDialog.scrollTop( 50 );
2016-04-03 16:10:09 +00:00
assert.equal( scrollDialog.scrollTop(), 50 );
otherDialog.dialog( "moveToTop" );
2016-04-03 16:10:09 +00:00
assert.equal( scrollDialog.scrollTop(), 50 );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "open", function( assert ) {
assert.expect( 3 );
2013-01-31 05:38:20 +00:00
var element,
2015-08-24 12:59:54 +00:00
expected = $( "<div></div>" ).dialog(),
actual = expected.dialog( "open" );
2016-04-03 16:10:09 +00:00
assert.equal( actual, expected, "open is chainable" );
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( { autoOpen: false } );
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "widget" ).is( ":hidden" ) && !element.dialog( "widget" ).is( ":visible" ), "dialog hidden before open method called" );
2015-08-24 12:59:54 +00:00
element.dialog( "open" );
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "widget" ).is( ":visible" ) && !element.dialog( "widget" ).is( ":hidden" ), "dialog visible after open method called" );
2015-08-24 12:59:54 +00:00
} );
// http://bugs.jqueryui.com/ticket/6137
2016-04-03 16:10:09 +00:00
QUnit.test( "Ensure form elements don't reset when opening a dialog", function( assert ) {
assert.expect( 2 );
2015-08-24 12:59:54 +00:00
var d1 = $( "<form><input type='radio' name='radio' id='a' value='a' checked='checked'></input>" +
"<input type='radio' name='radio' id='b' value='b'>b</input></form>" ).appendTo( "body" ).dialog( { autoOpen: false } );
2015-08-24 12:59:54 +00:00
d1.find( "#b" ).prop( "checked", true );
2016-04-03 16:10:09 +00:00
assert.equal( d1.find( "input:checked" ).val(), "b", "checkbox b is checked" );
2015-08-24 12:59:54 +00:00
d1.dialog( "open" );
2016-04-03 16:10:09 +00:00
assert.equal( d1.find( "input:checked" ).val(), "b", "checkbox b is checked" );
d1.remove();
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#8958: dialog can be opened while opening", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
2015-08-24 12:59:54 +00:00
var element = $( "<div>" ).dialog( {
autoOpen: false,
modal: true,
open: function() {
2016-04-03 16:10:09 +00:00
assert.equal( $( ".ui-widget-overlay" ).length, 1 );
ready();
}
2015-08-24 12:59:54 +00:00
} );
// Support: IE8
// For some reason the #favorite-color input doesn't get focus if we don't
// focus the body first, causing the test to hang.
$( "body" ).trigger( "focus" );
$( "#favorite-animal" )
2015-08-24 12:59:54 +00:00
// We focus the input to start the test. Once it receives focus, the
// dialog will open. Opening the dialog, will cause an element inside
// the dialog to gain focus, thus blurring the input.
.on( "focus", function() {
element.dialog( "open" );
2015-08-24 12:59:54 +00:00
} )
// When the input blurs, the dialog is in the process of opening. We
// try to open the dialog again, to make sure that dialogs properly
// handle a call to the open() method during the process of the dialog
// being opened.
.on( "blur", function() {
element.dialog( "open" );
2015-08-24 12:59:54 +00:00
} )
.trigger( "focus" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#5531: dialog width should be at least minWidth on creation", function( assert ) {
assert.expect( 4 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog( {
width: 200,
minWidth: 300
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "option", "width" ), 300, "width is minWidth" );
2015-08-24 12:59:54 +00:00
element.dialog( "option", "width", 200 );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "option", "width" ), 300, "width unchanged when set to < minWidth" );
2015-08-24 12:59:54 +00:00
element.dialog( "option", "width", 320 );
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "option", "width" ), 320, "width changed if set to > minWidth" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
minWidth: 300
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "option", "width" ) >= 300, "width is at least 300" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
} );
} );