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

375 lines
14 KiB
JavaScript
Raw Normal View History

define( [
2016-04-03 16:10:09 +00:00
"qunit",
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/dialog"
2016-04-03 16:10:09 +00:00
], function( QUnit, $, testHelper ) {
"use strict";
QUnit.module( "dialog: events", { afterEach: testHelper.moduleAfterEach } );
2015-08-24 12:59:54 +00:00
2016-04-03 16:10:09 +00:00
QUnit.test( "open", function( assert ) {
assert.expect( 13 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" );
element.dialog( {
open: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "instance" )._isOpen, "interal _isOpen flag is set" );
assert.ok( true, "autoOpen: true fires open callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogopen", "event type in callback" );
assert.deepEqual( ui, {}, "ui hash in callback" );
}
2015-08-24 12:59:54 +00:00
} );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" );
element.dialog( {
autoOpen: false,
2015-08-24 12:59:54 +00:00
open: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('open') fires open callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogopen", "event type in callback" );
assert.deepEqual( ui, {}, "ui hash in callback" );
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogopen", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "instance" )._isOpen, "interal _isOpen flag is set" );
assert.ok( true, "dialog('open') fires open event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.deepEqual( ui, {}, "ui hash in event" );
2015-08-24 12:59:54 +00:00
} );
element.dialog( "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( "focus", function( assert ) {
assert.expect( 5 );
2013-01-31 05:38:20 +00:00
var element, other;
2015-08-24 12:59:54 +00:00
element = $( "#dialog1" ).dialog( {
autoOpen: false
2015-08-24 12:59:54 +00:00
} );
other = $( "#dialog2" ).dialog( {
autoOpen: false
2015-08-24 12:59:54 +00:00
} );
2013-01-31 05:38:20 +00:00
element.one( "dialogopen", function() {
2016-04-03 16:10:09 +00:00
assert.ok( true, "open, just once" );
2015-08-24 12:59:54 +00:00
} );
2013-01-31 05:38:20 +00:00
element.one( "dialogfocus", function() {
2016-04-03 16:10:09 +00:00
assert.ok( true, "focus on open" );
2015-08-24 12:59:54 +00:00
} );
other.dialog( "open" );
2013-01-31 05:38:20 +00:00
element.one( "dialogfocus", function() {
2016-04-03 16:10:09 +00:00
assert.ok( true, "when opening and already open and wasn't on top" );
2015-08-24 12:59:54 +00:00
} );
other.dialog( "open" );
2013-01-31 05:38:20 +00:00
element.dialog( "open" );
2013-01-31 05:38:20 +00:00
element.one( "dialogfocus", function() {
2016-04-03 16:10:09 +00:00
assert.ok( true, "when calling moveToTop and wasn't on top" );
2015-08-24 12:59:54 +00:00
} );
other.dialog( "moveToTop" );
2013-01-31 05:38:20 +00:00
element.dialog( "moveToTop" );
element.on( "dialogfocus", function() {
2016-04-03 16:10:09 +00:00
assert.ok( true, "when mousedown anywhere on the dialog and it wasn't on top" );
2015-08-24 12:59:54 +00:00
} );
other.dialog( "moveToTop" );
2013-01-31 05:38:20 +00:00
element.trigger( "mousedown" );
// Triggers just once when already on top
2013-01-31 05:38:20 +00:00
element.dialog( "open" );
element.dialog( "moveToTop" );
element.trigger( "mousedown" );
2013-01-31 05:38:20 +00:00
element.add( other ).remove();
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "dragStart", function( assert ) {
assert.expect( 9 );
var handle,
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
dragStart: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "dragging fires dragStart callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogdragstart", "event type in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.offset !== undefined, "ui.offset in callback" );
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogdragstart", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "dragging fires dialogdragstart event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.offset !== undefined, "ui.offset in callback" );
2015-08-24 12:59:54 +00:00
} );
handle = $( ".ui-dialog-titlebar", element.dialog( "widget" ) );
testHelper.drag( element, handle, 50, 50 );
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( "drag", function( assert ) {
assert.expect( 9 );
2012-04-19 14:27:06 +00:00
var handle,
hasDragged = false,
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
drag: function( ev, ui ) {
if ( !hasDragged ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "dragging fires drag callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogdrag", "event type in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.offset !== undefined, "ui.offset in callback" );
hasDragged = true;
}
}
2015-08-24 12:59:54 +00:00
} ).one( "dialogdrag", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "dragging fires dialogdrag event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.offset !== undefined, "ui.offset in callback" );
2015-08-24 12:59:54 +00:00
} );
handle = $( ".ui-dialog-titlebar", element.dialog( "widget" ) );
testHelper.drag( element, handle, 50, 50 );
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( "dragStop", function( assert ) {
assert.expect( 9 );
var handle,
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
dragStop: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "dragging fires dragStop callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogdragstop", "event type in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.offset !== undefined, "ui.offset in callback" );
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogdragstop", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "dragging fires dialogdragstop event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.offset !== undefined, "ui.offset in callback" );
2015-08-24 12:59:54 +00:00
} );
handle = $( ".ui-dialog-titlebar", element.dialog( "widget" ) );
testHelper.drag( element, handle, 50, 50 );
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( "resizeStart", function( assert ) {
assert.expect( 13 );
var handle,
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
resizeStart: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "resizing fires resizeStart callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogresizestart", "event type in callback" );
assert.ok( ui.originalPosition !== undefined, "ui.originalPosition in callback" );
assert.ok( ui.originalSize !== undefined, "ui.originalSize in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.size !== undefined, "ui.size in callback" );
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogresizestart", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "resizing fires dialogresizestart event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.ok( ui.originalPosition !== undefined, "ui.originalPosition in callback" );
assert.ok( ui.originalSize !== undefined, "ui.originalSize in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.size !== undefined, "ui.size in callback" );
2015-08-24 12:59:54 +00:00
} );
handle = $( ".ui-resizable-se", element.dialog( "widget" ) );
testHelper.drag( element, handle, 50, 50 );
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( "resize", function( assert ) {
assert.expect( 13 );
2012-04-19 14:27:06 +00:00
var handle,
hasResized = false,
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
resize: function( ev, ui ) {
if ( !hasResized ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "resizing fires resize callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogresize", "event type in callback" );
assert.ok( ui.originalPosition !== undefined, "ui.originalPosition in callback" );
assert.ok( ui.originalSize !== undefined, "ui.originalSize in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.size !== undefined, "ui.size in callback" );
hasResized = true;
}
}
2015-08-24 12:59:54 +00:00
} ).one( "dialogresize", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "resizing fires dialogresize event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.ok( ui.originalPosition !== undefined, "ui.originalPosition in callback" );
assert.ok( ui.originalSize !== undefined, "ui.originalSize in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.size !== undefined, "ui.size in callback" );
2015-08-24 12:59:54 +00:00
} );
handle = $( ".ui-resizable-se", element.dialog( "widget" ) );
testHelper.drag( element, handle, 50, 50 );
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( "resizeStop", function( assert ) {
assert.expect( 13 );
var handle,
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
resizeStop: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "resizing fires resizeStop callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogresizestop", "event type in callback" );
assert.ok( ui.originalPosition !== undefined, "ui.originalPosition in callback" );
assert.ok( ui.originalSize !== undefined, "ui.originalSize in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.size !== undefined, "ui.size in callback" );
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogresizestop", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, "resizing fires dialogresizestop event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.ok( ui.originalPosition !== undefined, "ui.originalPosition in callback" );
assert.ok( ui.originalSize !== undefined, "ui.originalSize in callback" );
assert.ok( ui.position !== undefined, "ui.position in callback" );
assert.ok( ui.size !== undefined, "ui.size in callback" );
2015-08-24 12:59:54 +00:00
} );
handle = $( ".ui-resizable-se", element.dialog( "widget" ) );
testHelper.drag( element, handle, 50, 50 );
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( "close", function( assert ) {
var ready = assert.async();
assert.expect( 14 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog( {
close: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') fires close callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogclose", "event type in callback" );
assert.deepEqual( ui, {}, "ui hash in callback" );
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogclose", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') fires dialogclose event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.deepEqual( ui, {}, "ui hash in event" );
2015-08-24 12:59:54 +00:00
} );
element.dialog( "close" );
2013-01-31 05:38:20 +00:00
element.remove();
// Close event with an effect
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog( {
hide: 10,
2015-08-24 12:59:54 +00:00
close: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') fires close callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogclose", "event type in callback" );
assert.deepEqual( ui, {}, "ui hash in callback" );
ready();
}
2015-08-24 12:59:54 +00:00
} ).on( "dialogclose", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') fires dialogclose event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.deepEqual( ui, {}, "ui hash in event" );
2015-08-24 12:59:54 +00:00
} );
element.dialog( "close" );
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "beforeClose", function( assert ) {
assert.expect( 14 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog( {
beforeClose: function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') fires beforeClose callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogbeforeclose", "event type in callback" );
assert.deepEqual( ui, {}, "ui hash in callback" );
return false;
}
2015-08-24 12:59:54 +00:00
} );
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( ":visible" ), "beforeClose callback should prevent dialog from closing" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog();
element.dialog( "option", "beforeClose", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') fires beforeClose callback" );
assert.equal( this, element[ 0 ], "context of callback" );
assert.equal( ev.type, "dialogbeforeclose", "event type in callback" );
assert.deepEqual( ui, {}, "ui hash in callback" );
return false;
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( ":visible" ), "beforeClose callback should prevent dialog from closing" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
element = $( "<div></div>" ).dialog().on( "dialogbeforeclose", function( ev, ui ) {
2016-04-03 16:10:09 +00:00
assert.ok( true, ".dialog('close') triggers dialogbeforeclose event" );
assert.equal( this, element[ 0 ], "context of event" );
assert.deepEqual( ui, {}, "ui hash in event" );
return false;
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( ":visible" ), "dialogbeforeclose event should prevent dialog from closing" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
} );
// #8789 and #8838
2016-04-03 16:10:09 +00:00
QUnit.test( "ensure dialog's container doesn't scroll on resize and focus", function( assert ) {
var ready = assert.async();
assert.expect( 2 );
2015-08-24 12:59:54 +00:00
var element = $( "#dialog1" ).dialog(),
initialScroll = $( window ).scrollTop();
element.dialog( "option", "height", 600 );
2016-04-03 16:10:09 +00:00
assert.equal( $( window ).scrollTop(), initialScroll, "scroll hasn't moved after height change" );
2015-08-24 12:59:54 +00:00
setTimeout( function() {
$( ".ui-dialog-titlebar-close" ).simulate( "mousedown" );
2016-04-03 16:10:09 +00:00
assert.equal( $( window ).scrollTop(), initialScroll, "scroll hasn't moved after focus moved to dialog" );
2015-08-24 12:59:54 +00:00
element.dialog( "destroy" );
2016-04-03 16:10:09 +00:00
ready();
2015-08-24 12:59:54 +00:00
} );
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#5184: isOpen in dialogclose event is true", function( assert ) {
assert.expect( 3 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog( {
close: function() {
2016-04-03 16:10:09 +00:00
assert.ok( !element.dialog( "isOpen" ), "dialog is not open during close" );
}
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
assert.ok( element.dialog( "isOpen" ), "dialog is open after init" );
2013-01-31 05:38:20 +00:00
element.dialog( "close" );
2016-04-03 16:10:09 +00:00
assert.ok( !element.dialog( "isOpen" ), "dialog is not open after close" );
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( "ensure dialog keeps focus when clicking modal overlay", function( assert ) {
assert.expect( 2 );
2015-08-24 12:59:54 +00:00
var element = $( "<div></div>" ).dialog( {
modal: true
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
assert.equal( $( document.activeElement ).closest( ".ui-dialog" ).length, 1, "focus is in dialog" );
2015-08-24 12:59:54 +00:00
$( ".ui-widget-overlay" ).simulate( "mousedown" );
2016-04-03 16:10:09 +00:00
assert.equal( $( document.activeElement ).closest( ".ui-dialog" ).length, 1, "focus is still in dialog" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
} );
} );