| 1 |
define( [ |
| 2 |
"qunit", |
| 3 |
"jquery", |
| 4 |
"./helper", |
| 5 |
"ui/widgets/tooltip" |
| 6 |
], function( QUnit, $, testHelper ) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
var beforeAfterEach = testHelper.beforeAfterEach; |
| 10 |
|
| 11 |
QUnit.module( "tooltip: core", beforeAfterEach() ); |
| 12 |
|
| 13 |
QUnit.test( "markup structure", function( assert ) { |
| 14 |
assert.expect( 7 ); |
| 15 |
var element = $( "#tooltipped1" ).tooltip(), |
| 16 |
tooltip = $( ".ui-tooltip" ); |
| 17 |
|
| 18 |
assert.equal( element.attr( "aria-describedby" ), undefined, "no aria-describedby on init" ); |
| 19 |
assert.equal( tooltip.length, 0, "no tooltip on init" ); |
| 20 |
|
| 21 |
element.tooltip( "open" ); |
| 22 |
tooltip = $( "#" + element.data( "ui-tooltip-id" ) ); |
| 23 |
assert.equal( tooltip.length, 1, "tooltip exists" ); |
| 24 |
assert.equal( element.attr( "aria-describedby" ), tooltip.attr( "id" ), "aria-describedby" ); |
| 25 |
assert.hasClasses( tooltip, "ui-tooltip ui-widget ui-widget-content ui-widget-shadow" ); |
| 26 |
assert.equal( tooltip.length, 1, ".ui-tooltip exists" ); |
| 27 |
assert.equal( tooltip.find( ".ui-tooltip-content" ).length, 1, |
| 28 |
".ui-tooltip-content exists" ); |
| 29 |
|
| 30 |
element.tooltip( "destroy" ); |
| 31 |
} ); |
| 32 |
|
| 33 |
QUnit.test( "accessibility", function( assert ) { |
| 34 |
assert.expect( 15 ); |
| 35 |
|
| 36 |
var tooltipId, tooltip, |
| 37 |
element = $( "#multiple-describedby" ).tooltip(), |
| 38 |
liveRegion = element.tooltip( "instance" ).liveRegion; |
| 39 |
|
| 40 |
assert.equal( liveRegion.find( ">div" ).length, 0 ); |
| 41 |
assert.equal( liveRegion.attr( "role" ), "log" ); |
| 42 |
assert.equal( liveRegion.attr( "aria-live" ), "assertive" ); |
| 43 |
assert.equal( liveRegion.attr( "aria-relevant" ), "additions" ); |
| 44 |
element.tooltip( "open" ); |
| 45 |
tooltipId = element.data( "ui-tooltip-id" ); |
| 46 |
tooltip = $( "#" + tooltipId ); |
| 47 |
assert.equal( tooltip.attr( "role" ), "tooltip", "role" ); |
| 48 |
assert.equal( element.attr( "aria-describedby" ), "fixture-span " + tooltipId, |
| 49 |
"multiple describedby when open" ); |
| 50 |
|
| 51 |
assert.equal( element.attr( "title" ), null, "no title when open" ); |
| 52 |
assert.equal( liveRegion.children().length, 1 ); |
| 53 |
assert.equal( liveRegion.children().last().html(), "..." ); |
| 54 |
element.tooltip( "close" ); |
| 55 |
assert.equal( element.attr( "aria-describedby" ), "fixture-span", |
| 56 |
"correct describedby when closed" ); |
| 57 |
assert.equal( element.attr( "title" ), "...", "title restored when closed" ); |
| 58 |
|
| 59 |
element.tooltip( "open" ); |
| 60 |
assert.equal( liveRegion.children().length, 2, |
| 61 |
"After the second tooltip show, there should be two children" ); |
| 62 |
assert.equal( liveRegion.children().filter( ":visible" ).length, 1, |
| 63 |
"Only one of the children should be visible" ); |
| 64 |
assert.ok( liveRegion.children().last().is( ":visible" ), |
| 65 |
"Only the last child should be visible" ); |
| 66 |
element.tooltip( "close" ); |
| 67 |
|
| 68 |
element.tooltip( "destroy" ); |
| 69 |
assert.equal( liveRegion.parent().length, 0, |
| 70 |
"Tooltip liveregion element should be removed" ); |
| 71 |
} ); |
| 72 |
|
| 73 |
QUnit.test( "delegated removal", function( assert ) { |
| 74 |
assert.expect( 2 ); |
| 75 |
|
| 76 |
var container = $( "#contains-tooltipped" ).tooltip(), |
| 77 |
element = $( "#contained-tooltipped" ); |
| 78 |
|
| 79 |
element.trigger( "mouseover" ); |
| 80 |
assert.equal( $( ".ui-tooltip" ).length, 1 ); |
| 81 |
|
| 82 |
container.empty(); |
| 83 |
assert.equal( $( ".ui-tooltip" ).length, 0 ); |
| 84 |
} ); |
| 85 |
|
| 86 |
QUnit.test( "nested tooltips", function( assert ) { |
| 87 |
assert.expect( 2 ); |
| 88 |
|
| 89 |
var child = $( "#contained-tooltipped" ), |
| 90 |
parent = $( "#contains-tooltipped" ).tooltip( { |
| 91 |
show: null, |
| 92 |
hide: null |
| 93 |
} ); |
| 94 |
|
| 95 |
parent.trigger( "mouseover" ); |
| 96 |
assert.equal( $( ".ui-tooltip:visible" ).text(), "parent" ); |
| 97 |
|
| 98 |
child.trigger( "mouseover" ); |
| 99 |
assert.equal( $( ".ui-tooltip" ).text(), "child" ); |
| 100 |
|
| 101 |
parent.tooltip( "destroy" ); |
| 102 |
} ); |
| 103 |
|
| 104 |
|
| 105 |
QUnit.test( "form containing an input with name title", function( assert ) { |
| 106 |
assert.expect( 4 ); |
| 107 |
|
| 108 |
var form = $( "#tooltip-form" ).tooltip( { |
| 109 |
show: null, |
| 110 |
hide: null |
| 111 |
} ), |
| 112 |
input = form.find( "[name=title]" ); |
| 113 |
|
| 114 |
assert.equal( $( ".ui-tooltip" ).length, 0, "no tooltips on init" ); |
| 115 |
|
| 116 |
input.trigger( "mouseover" ); |
| 117 |
assert.equal( $( ".ui-tooltip" ).length, 1, "tooltip for input" ); |
| 118 |
input.trigger( "mouseleave" ); |
| 119 |
assert.equal( $( ".ui-tooltip" ).length, 0, "tooltip for input closed" ); |
| 120 |
|
| 121 |
form.trigger( "mouseover" ); |
| 122 |
assert.equal( $( ".ui-tooltip" ).length, 0, "no tooltip for form" ); |
| 123 |
} ); |
| 124 |
|
| 125 |
QUnit.test( "tooltip on .ui-state-disabled element", function( assert ) { |
| 126 |
assert.expect( 2 ); |
| 127 |
|
| 128 |
var container = $( "#contains-tooltipped" ).tooltip(), |
| 129 |
element = $( "#contained-tooltipped" ).addClass( "ui-state-disabled" ); |
| 130 |
|
| 131 |
element.trigger( "mouseover" ); |
| 132 |
assert.equal( $( ".ui-tooltip" ).length, 1 ); |
| 133 |
|
| 134 |
container.empty(); |
| 135 |
assert.equal( $( ".ui-tooltip" ).length, 0 ); |
| 136 |
} ); |
| 137 |
|
| 138 |
|
| 139 |
QUnit.test( "programmatic focus with async content", function( assert ) { |
| 140 |
var ready = assert.async(); |
| 141 |
assert.expect( 2 ); |
| 142 |
var element = $( "#tooltipped1" ).tooltip( { |
| 143 |
content: function( response ) { |
| 144 |
setTimeout( function() { |
| 145 |
response( "test" ); |
| 146 |
} ); |
| 147 |
} |
| 148 |
} ); |
| 149 |
|
| 150 |
element.on( "tooltipopen", function( event ) { |
| 151 |
assert.deepEqual( event.originalEvent.type, "focusin" ); |
| 152 |
|
| 153 |
element.on( "tooltipclose", function( event ) { |
| 154 |
assert.deepEqual( event.originalEvent.type, "focusout" ); |
| 155 |
element.tooltip( "destroy" ); |
| 156 |
ready(); |
| 157 |
} ); |
| 158 |
|
| 159 |
setTimeout( function() { |
| 160 |
element.trigger( "blur" ); |
| 161 |
} ); |
| 162 |
} ); |
| 163 |
|
| 164 |
element.trigger( "focus" ); |
| 165 |
} ); |
| 166 |
|
| 167 |
QUnit.test( "destroy during hide animation; only one close event", function( assert ) { |
| 168 |
var ready = assert.async(); |
| 169 |
assert.expect( 1 ); |
| 170 |
|
| 171 |
var element = $( "#tooltipped1" ).tooltip( { |
| 172 |
show: false, |
| 173 |
hide: true |
| 174 |
} ); |
| 175 |
|
| 176 |
element.on( "tooltipclose", function() { |
| 177 |
assert.ok( true, "tooltip closed" ); |
| 178 |
} ); |
| 179 |
|
| 180 |
element.tooltip( "open" ); |
| 181 |
element.tooltip( "close" ); |
| 182 |
setTimeout( function() { |
| 183 |
element.tooltip( "destroy" ); |
| 184 |
ready(); |
| 185 |
} ); |
| 186 |
} ); |
| 187 |
|
| 188 |
|
| 189 |
QUnit.test( "multiple active delegated tooltips", function( assert ) { |
| 190 |
var ready = assert.async(); |
| 191 |
assert.expect( 1 ); |
| 192 |
|
| 193 |
var anchor = $( "#tooltipped1" ), |
| 194 |
input = anchor.next(), |
| 195 |
actions = []; |
| 196 |
|
| 197 |
$( document ).tooltip( { |
| 198 |
show: false, |
| 199 |
hide: false, |
| 200 |
open: function( event, ui ) { |
| 201 |
actions.push( "open:" + ui.tooltip.text() ); |
| 202 |
}, |
| 203 |
close: function( event, ui ) { |
| 204 |
actions.push( "close:" + ui.tooltip.text() ); |
| 205 |
} |
| 206 |
} ); |
| 207 |
|
| 208 |
function step1() { |
| 209 |
anchor.simulate( "mouseover" ); |
| 210 |
setTimeout( step2 ); |
| 211 |
} |
| 212 |
|
| 213 |
function step2() { |
| 214 |
input.simulate( "focus" ); |
| 215 |
setTimeout( step3 ); |
| 216 |
} |
| 217 |
|
| 218 |
function step3() { |
| 219 |
input.simulate( "blur" ); |
| 220 |
setTimeout( step4 ); |
| 221 |
} |
| 222 |
|
| 223 |
function step4() { |
| 224 |
anchor.simulate( "mouseout" ); |
| 225 |
assert.deepEqual( actions, [ |
| 226 |
"open:anchortitle", |
| 227 |
"open:inputtitle", |
| 228 |
"close:inputtitle", |
| 229 |
"close:anchortitle" |
| 230 |
], "Both tooltips open and close" ); |
| 231 |
ready(); |
| 232 |
} |
| 233 |
|
| 234 |
step1(); |
| 235 |
} ); |
| 236 |
|
| 237 |
|
| 238 |
QUnit.test( "remove conflicting attributes from live region", function( assert ) { |
| 239 |
assert.expect( 2 ); |
| 240 |
|
| 241 |
var element = $( |
| 242 |
"<div id='content'>" + |
| 243 |
"<input type='radio' name='hobby' id='hobby1' checked='checked'>" + |
| 244 |
"<label for='hobby1'>option 1</label>" + |
| 245 |
"<input type='radio' name='hobby' id='hobby2'>" + |
| 246 |
"<label for='hobby2'>option 2</label>" + |
| 247 |
"</div>" ); |
| 248 |
|
| 249 |
$( "#tooltipped1" ) |
| 250 |
.tooltip( { |
| 251 |
content: element, |
| 252 |
open: function() { |
| 253 |
assert.equal( $( ".ui-helper-hidden-accessible [name]" ).length, 0, |
| 254 |
"no name attributes within live region" ); |
| 255 |
assert.equal( $( ".ui-helper-hidden-accessible [id]" ).length, 0, |
| 256 |
"no id attributes within live region" ); |
| 257 |
$( "#tooltipped1" ).tooltip( "destroy" ); |
| 258 |
} |
| 259 |
} ) |
| 260 |
.tooltip( "open" ); |
| 261 |
} ); |
| 262 |
|
| 263 |
|
| 264 |
QUnit.test( "don't crash on empty tooltip content", function( assert ) { |
| 265 |
var ready = assert.async(); |
| 266 |
assert.expect( 1 ); |
| 267 |
|
| 268 |
var anchor = $( "#tooltipped1" ), |
| 269 |
input = anchor.next(), |
| 270 |
actions = []; |
| 271 |
|
| 272 |
$( document ).tooltip( { |
| 273 |
show: false, |
| 274 |
hide: false, |
| 275 |
content: function() { |
| 276 |
var title = $( this ).attr( "title" ); |
| 277 |
if ( title === "inputtitle" ) { |
| 278 |
return ""; |
| 279 |
} |
| 280 |
return title; |
| 281 |
}, |
| 282 |
open: function( event, ui ) { |
| 283 |
actions.push( "open:" + ui.tooltip.text() ); |
| 284 |
}, |
| 285 |
close: function( event, ui ) { |
| 286 |
actions.push( "close:" + ui.tooltip.text() ); |
| 287 |
} |
| 288 |
} ); |
| 289 |
|
| 290 |
function step1() { |
| 291 |
anchor.simulate( "mouseover" ); |
| 292 |
setTimeout( step2 ); |
| 293 |
} |
| 294 |
|
| 295 |
function step2() { |
| 296 |
anchor.simulate( "mouseout" ); |
| 297 |
setTimeout( step3 ); |
| 298 |
} |
| 299 |
|
| 300 |
function step3() { |
| 301 |
input.simulate( "focus" ); |
| 302 |
setTimeout( step4 ); |
| 303 |
} |
| 304 |
|
| 305 |
function step4() { |
| 306 |
input.simulate( "blur" ); |
| 307 |
setTimeout( step5 ); |
| 308 |
} |
| 309 |
|
| 310 |
function step5() { |
| 311 |
anchor.simulate( "mouseover" ); |
| 312 |
setTimeout( step6 ); |
| 313 |
} |
| 314 |
|
| 315 |
function step6() { |
| 316 |
anchor.simulate( "mouseout" ); |
| 317 |
setTimeout( step7 ); |
| 318 |
} |
| 319 |
|
| 320 |
function step7() { |
| 321 |
assert.deepEqual( actions, [ |
| 322 |
"open:anchortitle", |
| 323 |
"close:anchortitle", |
| 324 |
"open:anchortitle", |
| 325 |
"close:anchortitle" |
| 326 |
], "Tooltip opens and closes without crashing" ); |
| 327 |
ready(); |
| 328 |
} |
| 329 |
|
| 330 |
step1(); |
| 331 |
} ); |
| 332 |
|
| 333 |
} ); |