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

257 lines
7.0 KiB
JavaScript
Raw Normal View History

define( [
2016-04-06 13:59:12 +00:00
"qunit",
"jquery",
"./helper",
"ui/widgets/tooltip"
], function( QUnit, $, testHelper ) {
var beforeAfterEach = testHelper.beforeAfterEach;
QUnit.module( "tooltip: core", beforeAfterEach() );
2016-04-06 13:59:12 +00:00
QUnit.test( "markup structure", function( assert ) {
assert.expect( 7 );
2011-05-28 19:43:22 +00:00
var element = $( "#tooltipped1" ).tooltip(),
tooltip = $( ".ui-tooltip" );
2016-04-06 13:59:12 +00:00
assert.equal( element.attr( "aria-describedby" ), undefined, "no aria-describedby on init" );
assert.equal( tooltip.length, 0, "no tooltip on init" );
2011-05-28 19:43:22 +00:00
element.tooltip( "open" );
tooltip = $( "#" + element.data( "ui-tooltip-id" ) );
2016-04-06 13:59:12 +00:00
assert.equal( tooltip.length, 1, "tooltip exists" );
assert.equal( element.attr( "aria-describedby" ), tooltip.attr( "id" ), "aria-describedby" );
assert.hasClasses( tooltip, "ui-tooltip ui-widget ui-widget-content ui-widget-shadow" );
2016-04-06 13:59:12 +00:00
assert.equal( tooltip.length, 1, ".ui-tooltip exists" );
assert.equal( tooltip.find( ".ui-tooltip-content" ).length, 1,
2011-05-28 19:43:22 +00:00
".ui-tooltip-content exists" );
2015-08-24 12:50:46 +00:00
} );
2011-05-28 19:43:22 +00:00
2016-04-06 13:59:12 +00:00
QUnit.test( "accessibility", function( assert ) {
assert.expect( 15 );
var tooltipId, tooltip,
element = $( "#multiple-describedby" ).tooltip(),
liveRegion = element.tooltip( "instance" ).liveRegion;
2016-04-06 13:59:12 +00:00
assert.equal( liveRegion.find( ">div" ).length, 0 );
assert.equal( liveRegion.attr( "role" ), "log" );
assert.equal( liveRegion.attr( "aria-live" ), "assertive" );
assert.equal( liveRegion.attr( "aria-relevant" ), "additions" );
element.tooltip( "open" );
tooltipId = element.data( "ui-tooltip-id" );
tooltip = $( "#" + tooltipId );
2016-04-06 13:59:12 +00:00
assert.equal( tooltip.attr( "role" ), "tooltip", "role" );
assert.equal( element.attr( "aria-describedby" ), "fixture-span " + tooltipId,
"multiple describedby when open" );
2016-04-06 13:59:12 +00:00
assert.equal( element.attr( "title" ), null, "no title when open" );
assert.equal( liveRegion.children().length, 1 );
assert.equal( liveRegion.children().last().html(), "..." );
element.tooltip( "close" );
2016-04-06 13:59:12 +00:00
assert.equal( element.attr( "aria-describedby" ), "fixture-span",
"correct describedby when closed" );
2016-04-06 13:59:12 +00:00
assert.equal( element.attr( "title" ), "...", "title restored when closed" );
element.tooltip( "open" );
2016-04-06 13:59:12 +00:00
assert.equal( liveRegion.children().length, 2,
"After the second tooltip show, there should be two children" );
2016-04-06 13:59:12 +00:00
assert.equal( liveRegion.children().filter( ":visible" ).length, 1,
"Only one of the children should be visible" );
2016-04-06 13:59:12 +00:00
assert.ok( liveRegion.children().last().is( ":visible" ),
"Only the last child should be visible" );
element.tooltip( "close" );
element.tooltip( "destroy" );
2016-04-06 13:59:12 +00:00
assert.equal( liveRegion.parent().length, 0,
"Tooltip liveregion element should be removed" );
2015-08-24 12:50:46 +00:00
} );
2016-04-06 13:59:12 +00:00
QUnit.test( "delegated removal", function( assert ) {
assert.expect( 2 );
var container = $( "#contains-tooltipped" ).tooltip(),
element = $( "#contained-tooltipped" );
element.trigger( "mouseover" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 1 );
container.empty();
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 0 );
2015-08-24 12:50:46 +00:00
} );
2016-04-06 13:59:12 +00:00
QUnit.test( "nested tooltips", function( assert ) {
assert.expect( 2 );
2012-10-21 17:25:36 +00:00
var child = $( "#contained-tooltipped" ),
2015-08-24 12:50:46 +00:00
parent = $( "#contains-tooltipped" ).tooltip( {
2012-10-21 17:25:36 +00:00
show: null,
hide: null
2015-08-24 12:50:46 +00:00
} );
2012-10-21 17:25:36 +00:00
parent.trigger( "mouseover" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip:visible" ).text(), "parent" );
2012-10-21 17:25:36 +00:00
child.trigger( "mouseover" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).text(), "child" );
2015-08-24 12:50:46 +00:00
} );
2012-10-21 17:25:36 +00:00
// #8742
2016-04-06 13:59:12 +00:00
QUnit.test( "form containing an input with name title", function( assert ) {
assert.expect( 4 );
2015-08-24 12:50:46 +00:00
var form = $( "#tooltip-form" ).tooltip( {
show: null,
hide: null
2015-08-24 12:50:46 +00:00
} ),
input = form.find( "[name=title]" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 0, "no tooltips on init" );
input.trigger( "mouseover" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 1, "tooltip for input" );
input.trigger( "mouseleave" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 0, "tooltip for input closed" );
form.trigger( "mouseover" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 0, "no tooltip for form" );
2015-08-24 12:50:46 +00:00
} );
2016-04-06 13:59:12 +00:00
QUnit.test( "tooltip on .ui-state-disabled element", function( assert ) {
assert.expect( 2 );
var container = $( "#contains-tooltipped" ).tooltip(),
element = $( "#contained-tooltipped" ).addClass( "ui-state-disabled" );
element.trigger( "mouseover" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 1 );
container.empty();
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-tooltip" ).length, 0 );
2015-08-24 12:50:46 +00:00
} );
// http://bugs.jqueryui.com/ticket/8740
2016-04-06 13:59:12 +00:00
QUnit.test( "programmatic focus with async content", function( assert ) {
var ready = assert.async();
assert.expect( 2 );
2015-08-24 12:50:46 +00:00
var element = $( "#tooltipped1" ).tooltip( {
content: function( response ) {
2015-08-24 12:50:46 +00:00
setTimeout( function() {
response( "test" );
2015-08-24 12:50:46 +00:00
} );
}
2015-08-24 12:50:46 +00:00
} );
element.on( "tooltipopen", function( event ) {
2016-04-06 13:59:12 +00:00
assert.deepEqual( event.originalEvent.type, "focusin" );
element.on( "tooltipclose", function( event ) {
2016-04-06 13:59:12 +00:00
assert.deepEqual( event.originalEvent.type, "focusout" );
ready();
2015-08-24 12:50:46 +00:00
} );
2015-08-24 12:50:46 +00:00
setTimeout( function() {
element.trigger( "blur" );
2015-08-24 12:50:46 +00:00
} );
} );
element.trigger( "focus" );
2015-08-24 12:50:46 +00:00
} );
2016-04-06 13:59:12 +00:00
QUnit.test( "destroy during hide animation; only one close event", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
2015-08-24 12:50:46 +00:00
var element = $( "#tooltipped1" ).tooltip( {
show: false,
hide: true
2015-08-24 12:50:46 +00:00
} );
element.on( "tooltipclose", function() {
2016-04-06 13:59:12 +00:00
assert.ok( true, "tooltip closed" );
2015-08-24 12:50:46 +00:00
} );
element.tooltip( "open" );
element.tooltip( "close" );
2015-08-24 12:50:46 +00:00
setTimeout( function() {
element.tooltip( "destroy" );
2016-04-06 13:59:12 +00:00
ready();
2015-08-24 12:50:46 +00:00
} );
} );
// http://bugs.jqueryui.com/ticket/10602
2016-04-06 13:59:12 +00:00
QUnit.test( "multiple active delegated tooltips", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
var anchor = $( "#tooltipped1" ),
input = anchor.next(),
actions = [];
2015-08-24 12:50:46 +00:00
$( document ).tooltip( {
show: false,
hide: false,
open: function( event, ui ) {
actions.push( "open:" + ui.tooltip.text() );
},
close: function( event, ui ) {
actions.push( "close:" + ui.tooltip.text() );
}
2015-08-24 12:50:46 +00:00
} );
function step1() {
anchor.simulate( "mouseover" );
setTimeout( step2 );
}
function step2() {
input.simulate( "focus" );
setTimeout( step3 );
}
function step3() {
input.simulate( "blur" );
setTimeout( step4 );
}
function step4() {
anchor.simulate( "mouseout" );
2016-04-06 13:59:12 +00:00
assert.deepEqual( actions, [
"open:anchortitle",
"open:inputtitle",
"close:inputtitle",
"close:anchortitle"
], "Both tooltips open and close" );
2016-04-06 13:59:12 +00:00
ready();
}
step1();
2015-08-24 12:50:46 +00:00
} );
// http://bugs.jqueryui.com/ticket/11272
2016-04-06 13:59:12 +00:00
QUnit.test( "remove conflicting attributes from live region", function( assert ) {
assert.expect( 2 );
var element = $(
"<div id='content'>" +
"<input type='radio' name='hobby' id='hobby1' checked='checked'>" +
"<label for='hobby1'>option 1</label>" +
"<input type='radio' name='hobby' id='hobby2'>" +
"<label for='hobby2'>option 2</label>" +
"</div>" );
$( "#tooltipped1" )
2015-08-24 12:50:46 +00:00
.tooltip( {
content: element,
open: function() {
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-helper-hidden-accessible [name]" ).length, 0,
"no name attributes within live region" );
2016-04-06 13:59:12 +00:00
assert.equal( $( ".ui-helper-hidden-accessible [id]" ).length, 0,
"no id attributes within live region" );
}
2015-08-24 12:50:46 +00:00
} )
.tooltip( "open" );
2015-08-24 12:50:46 +00:00
} );
} );