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

339 lines
11 KiB
JavaScript
Raw Normal View History

define( [
2016-04-03 16:10:09 +00:00
"qunit",
"jquery",
"lib/helper",
"ui/widgets/dialog"
], function( QUnit, $, helper ) {
"use strict";
// TODO add afterEach callback to remove dialogs
QUnit.module( "dialog: core", { afterEach: helper.moduleAfterEach } );
2016-04-03 16:10:09 +00:00
QUnit.test( "markup structure", function( assert ) {
assert.expect( 11 );
2015-08-24 12:59:54 +00:00
var element = $( "<div>" ).dialog( {
buttons: [ {
text: "Ok",
click: $.noop
} ]
2015-08-24 12:59:54 +00:00
} ),
widget = element.dialog( "widget" ),
titlebar = widget.find( ".ui-dialog-titlebar" ),
title = titlebar.find( ".ui-dialog-title" ),
close = titlebar.find( ".ui-dialog-titlebar-close" ),
buttonpane = widget.find( ".ui-dialog-buttonpane" ),
buttonset = widget.find( ".ui-dialog-buttonset" ),
buttons = buttonset.find( ".ui-button" );
assert.hasClasses( widget, "ui-dialog ui-dialog-buttons ui-widget ui-widget-content" );
assert.hasClasses( titlebar, "ui-dialog-titlebar ui-widget-header" );
2016-04-03 16:10:09 +00:00
assert.equal( titlebar.length, 1, "Dialog has exactly one titlebar" );
assert.hasClasses( close, "ui-dialog-titlebar-close ui-widget" );
2016-04-03 16:10:09 +00:00
assert.equal( close.length, 1, "Titlebar has exactly one close button" );
assert.equal( title.length, 1, "Titlebar has exactly one title" );
assert.hasClasses( element, "ui-dialog-content ui-widget-content" );
assert.hasClasses( buttonpane, "ui-dialog-buttonpane ui-widget-content" );
2016-04-03 16:10:09 +00:00
assert.equal( buttonpane.length, 1, "Dialog has exactly one buttonpane" );
assert.equal( buttonset.length, 1, "Buttonpane has exactly one buttonset" );
assert.equal( buttons.length, 1, "Buttonset contains exactly 1 button when created with 1" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "markup structure - no buttons", function( assert ) {
assert.expect( 7 );
var element = $( "<div>" ).dialog(),
widget = element.dialog( "widget" ),
titlebar = widget.find( ".ui-dialog-titlebar" ),
title = titlebar.find( ".ui-dialog-title" ),
close = titlebar.find( ".ui-dialog-titlebar-close" );
assert.hasClasses( widget, "ui-dialog ui-widget ui-widget-content" );
assert.hasClasses( titlebar, "ui-dialog-titlebar ui-widget-header" );
2016-04-03 16:10:09 +00:00
assert.equal( titlebar.length, 1, "Dialog has exactly one titlebar" );
assert.hasClasses( close, "ui-dialog-titlebar-close ui-widget" );
2016-04-03 16:10:09 +00:00
assert.equal( close.length, 1, "Titlebar has exactly one close button" );
assert.equal( title.length, 1, "Titlebar has exactly one title" );
assert.hasClasses( element, "ui-dialog-content ui-widget-content" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "title id", function( assert ) {
assert.expect( 1 );
var titleId,
2015-08-24 12:59:54 +00:00
element = $( "<div>" ).dialog();
2015-08-24 12:59:54 +00:00
titleId = element.dialog( "widget" ).find( ".ui-dialog-title" ).attr( "id" );
2016-04-03 16:10:09 +00:00
assert.ok( /ui-id-\d+$/.test( titleId ), "auto-numbered title id" );
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( "ARIA", function( assert ) {
assert.expect( 4 );
var element = $( "<div>" ).dialog(),
2013-01-31 05:38:20 +00:00
wrapper = element.dialog( "widget" );
2016-04-03 16:10:09 +00:00
assert.equal( wrapper.attr( "role" ), "dialog", "dialog role" );
assert.equal( wrapper.attr( "aria-labelledby" ), wrapper.find( ".ui-dialog-title" ).attr( "id" ) );
assert.equal( wrapper.attr( "aria-describedby" ), element.attr( "id" ), "aria-describedby added" );
2013-01-31 05:38:20 +00:00
element.remove();
2015-08-24 12:59:54 +00:00
element = $( "<div><div aria-describedby='section2'><p id='section2'>descriotion</p></div></div>" ).dialog();
2016-04-03 16:10:09 +00:00
assert.equal( element.dialog( "widget" ).attr( "aria-describedby" ), null, "no aria-describedby added, as already present in markup" );
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( "widget method", function( assert ) {
assert.expect( 1 );
2015-08-24 12:59:54 +00:00
var dialog = $( "<div>" ).appendTo( "#qunit-fixture" ).dialog();
2016-04-03 16:10:09 +00:00
assert.deepEqual( dialog.parent()[ 0 ], dialog.dialog( "widget" )[ 0 ] );
dialog.remove();
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "focus tabbable", function( assert ) {
var ready = assert.async();
assert.expect( 8 );
2013-01-31 05:38:20 +00:00
var element,
options = {
2015-08-24 12:59:54 +00:00
buttons: [ {
text: "Ok",
click: $.noop
2015-08-24 12:59:54 +00:00
} ]
};
function checkFocus( markup, options, testFn, next ) {
// Support: IE8
// For some reason the focus doesn't get set properly if we don't
// focus the body first.
$( "body" ).trigger( "focus" );
2013-01-31 05:38:20 +00:00
element = $( markup ).dialog( options );
2015-08-24 12:59:54 +00:00
setTimeout( function() {
testFn( function done() {
element.remove();
setTimeout( next );
2015-08-24 12:59:54 +00:00
} );
} );
}
function step1() {
checkFocus( "<div><input><input></div>", options, function( done ) {
var input = element.find( "input" ).last().trigger( "focus" ).trigger( "blur" );
// Support: IE 11+
// In IE in jQuery 3.4+ a sequence:
// $( inputNode ).trigger( "focus" ).trigger( "blur" ).trigger( "focus" )
// doesn't end up with a focused input. See:
// https://github.com/jquery/jquery/issues/4856
// However, in this test we only want to check that the last focused
// input receives the focus back when `_focusTabbable()` is called
// which in reality doesn't happen so quickly so let's avoid the issue
// by waiting a bit.
if ( document.documentMode ) {
setTimeout( function() {
focusTabbableAndAssert();
}, 500 );
} else {
focusTabbableAndAssert();
}
function focusTabbableAndAssert() {
element.dialog( "instance" )._focusTabbable();
setTimeout( function() {
assert.equal( document.activeElement, input[ 0 ],
"1. an element that was focused previously." );
done();
} );
}
}, step2 );
}
function step2() {
checkFocus( "<div><input><input autofocus></div>", options, function( done ) {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, element.find( "input" )[ 1 ],
"2. first element inside the dialog matching [autofocus]" );
done();
}, step3 );
}
function step3() {
checkFocus( "<div><input><input></div>", options, function( done ) {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, element.find( "input" )[ 0 ],
"3. tabbable element inside the content element" );
done();
}, step4 );
}
function step4() {
checkFocus( "<div>text</div>", options, function( done ) {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement,
2013-01-31 05:38:20 +00:00
element.dialog( "widget" ).find( ".ui-dialog-buttonpane button" )[ 0 ],
"4. tabbable element inside the buttonpane" );
done();
}, step5 );
}
function step5() {
checkFocus( "<div>text</div>", {}, function( done ) {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement,
2013-01-31 05:38:20 +00:00
element.dialog( "widget" ).find( ".ui-dialog-titlebar .ui-dialog-titlebar-close" )[ 0 ],
"5. the close button" );
done();
}, step6 );
}
function step6() {
checkFocus( "<div>text</div>", { autoOpen: false }, function( done ) {
element.dialog( "widget" ).find( ".ui-dialog-titlebar-close" ).hide();
element.dialog( "open" );
2015-08-24 12:59:54 +00:00
setTimeout( function() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, element.parent()[ 0 ], "6. the dialog itself" );
done();
2015-08-24 12:59:54 +00:00
} );
}, step7 );
}
function step7() {
checkFocus(
"<div><input><input autofocus></div>",
{
open: function() {
var inputs = $( this ).find( "input" );
2015-08-24 12:59:54 +00:00
inputs.last().on( "keydown", function( event ) {
event.preventDefault();
inputs.first().trigger( "focus" );
2015-08-24 12:59:54 +00:00
} );
}
},
function( done ) {
2016-04-03 16:10:09 +00:00
var inputs = element.find( "input"
);
assert.equal( document.activeElement, inputs[ 1 ], "Focus starts on second input" );
2015-08-24 12:59:54 +00:00
inputs.last().simulate( "keydown", { keyCode: $.ui.keyCode.TAB } );
setTimeout( function() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, inputs[ 0 ],
"Honor preventDefault, allowing custom focus management" );
done();
}, 50 );
},
2016-04-03 16:10:09 +00:00
ready
);
}
step1();
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#7960: resizable handles below modal overlays", function( assert ) {
assert.expect( 1 );
var resizable = $( "<div>" ).resizable(),
2015-08-24 12:59:54 +00:00
dialog = $( "<div>" ).dialog( { modal: true } ),
resizableZindex = parseInt( resizable.find( ".ui-resizable-handle" ).css( "zIndex" ), 10 ),
overlayZindex = parseInt( $( ".ui-widget-overlay" ).css( "zIndex" ), 10 );
2016-04-03 16:10:09 +00:00
assert.ok( resizableZindex < overlayZindex, "Resizable handles have lower z-index than modal overlay" );
dialog.dialog( "destroy" );
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "Prevent tabbing out of dialogs", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
var element = $( "<div><input name='0'><input name='1'></div>" ).dialog(),
inputs = element.find( "input" );
// Remove close button to test focus on just the two buttons
2015-08-24 12:59:54 +00:00
element.dialog( "widget" ).find( ".ui-button" ).remove();
function checkTab() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, inputs[ 0 ], "Tab key event move d focus within the modal" );
// Check shift tab
2015-08-24 12:59:54 +00:00
$( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.TAB, shiftKey: true } );
setTimeout( checkShiftTab );
}
function checkShiftTab() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, inputs[ 1 ], "Shift-Tab key event moved focus back to second input" );
2013-01-31 05:38:20 +00:00
element.remove();
2016-04-03 16:10:09 +00:00
setTimeout( ready );
}
inputs[ 1 ].focus();
2015-08-24 12:59:54 +00:00
setTimeout( function() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, inputs[ 1 ], "Focus set on second input" );
2015-08-24 12:59:54 +00:00
inputs.eq( 1 ).simulate( "keydown", { keyCode: $.ui.keyCode.TAB } );
setTimeout( checkTab );
2015-08-24 12:59:54 +00:00
} );
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "#9048: multiple modal dialogs opened and closed in different order",
function( assert ) {
var ready = assert.async();
assert.expect( 1 );
$( "#dialog1, #dialog2" ).dialog( { autoOpen: false, modal: true } );
$( "#dialog1" ).dialog( "open" );
$( "#dialog2" ).dialog( "open" );
$( "#dialog1" ).dialog( "close" );
2015-08-24 12:59:54 +00:00
setTimeout( function() {
$( "#dialog2" ).dialog( "close" );
$( "#favorite-animal" ).trigger( "focus" );
2016-04-03 16:10:09 +00:00
assert.ok( true, "event handlers cleaned up (no errors thrown)" );
ready();
2015-08-24 12:59:54 +00:00
} );
} );
2016-04-03 16:10:09 +00:00
QUnit.test( "interaction between overlay and other dialogs", function( assert ) {
var ready = assert.async();
$.widget( "ui.testWidget", $.ui.dialog, {
options: {
modal: true,
autoOpen: false
}
2015-08-24 12:59:54 +00:00
} );
2016-04-03 16:10:09 +00:00
assert.expect( 2 );
2015-08-24 12:59:54 +00:00
var first = $( "<div><input id='input-1'></div>" ).dialog( {
modal: true
2015-08-24 12:59:54 +00:00
} ),
firstInput = first.find( "input" ),
second = $( "<div><input id='input-2'></div>" ).testWidget(),
secondInput = second.find( "input" );
// Support: IE8
// For some reason the focus doesn't get set properly if we don't
// focus the body first.
$( "body" ).trigger( "focus" );
// Wait for the modal to init
2015-08-24 12:59:54 +00:00
setTimeout( function() {
second.testWidget( "open" );
2016-04-03 16:10:09 +00:00
// Simulate user tabbing from address bar to an element outside the dialog
$( "#favorite-animal" ).trigger( "focus" );
2015-08-24 12:59:54 +00:00
setTimeout( function() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, secondInput[ 0 ] );
// Last active dialog must receive focus
firstInput.trigger( "focus" );
$( "#favorite-animal" ).trigger( "focus" );
2015-08-24 12:59:54 +00:00
setTimeout( function() {
2016-04-03 16:10:09 +00:00
assert.equal( document.activeElement, firstInput[ 0 ] );
// Cleanup
first.remove();
second.remove();
delete $.ui.testWidget;
delete $.fn.testWidget;
2016-04-03 16:10:09 +00:00
ready();
2015-08-24 12:59:54 +00:00
} );
} );
} );
} );
} );