| 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: options", beforeAfterEach() ); |
| 12 |
|
| 13 |
QUnit.test( "disabled: true", function( assert ) { |
| 14 |
assert.expect( 1 ); |
| 15 |
var element = $( "#tooltipped1" ).tooltip( { |
| 16 |
disabled: true |
| 17 |
} ).tooltip( "open" ); |
| 18 |
assert.equal( $( ".ui-tooltip" ).length, 0 ); |
| 19 |
element.tooltip( "destroy" ); |
| 20 |
} ); |
| 21 |
|
| 22 |
QUnit.test( "content: default", function( assert ) { |
| 23 |
assert.expect( 1 ); |
| 24 |
var element = $( "#tooltipped1" ).tooltip().tooltip( "open" ); |
| 25 |
assert.deepEqual( $( "#" + element.data( "ui-tooltip-id" ) ).text(), "anchortitle" ); |
| 26 |
element.tooltip( "destroy" ); |
| 27 |
} ); |
| 28 |
|
| 29 |
QUnit.test( "content: default; HTML escaping", function( assert ) { |
| 30 |
assert.expect( 2 ); |
| 31 |
var scriptText = "<script>$.ui.tooltip.hacked = true;</script>", |
| 32 |
element = $( "#tooltipped1" ); |
| 33 |
|
| 34 |
$.ui.tooltip.hacked = false; |
| 35 |
element.attr( "title", scriptText ) |
| 36 |
.tooltip() |
| 37 |
.tooltip( "open" ); |
| 38 |
assert.equal( $.ui.tooltip.hacked, false, "script did not execute" ); |
| 39 |
assert.deepEqual( $( "#" + element.data( "ui-tooltip-id" ) ).text(), scriptText, |
| 40 |
"correct tooltip text" ); |
| 41 |
element.tooltip( "destroy" ); |
| 42 |
} ); |
| 43 |
|
| 44 |
QUnit.test( "content: return string", function( assert ) { |
| 45 |
assert.expect( 1 ); |
| 46 |
var element = $( "#tooltipped1" ).tooltip( { |
| 47 |
content: function() { |
| 48 |
return "customstring"; |
| 49 |
} |
| 50 |
} ).tooltip( "open" ); |
| 51 |
assert.deepEqual( $( "#" + element.data( "ui-tooltip-id" ) ).text(), "customstring" ); |
| 52 |
element.tooltip( "destroy" ); |
| 53 |
} ); |
| 54 |
|
| 55 |
QUnit.test( "content: return jQuery", function( assert ) { |
| 56 |
assert.expect( 2 ); |
| 57 |
var element = $( "#tooltipped1" ).tooltip( { |
| 58 |
content: function() { |
| 59 |
return $( "<div id='unique'>" ).html( "cu<b id='bold'>s</b>tomstring" ); |
| 60 |
} |
| 61 |
} ).tooltip( "open" ), |
| 62 |
liveRegion = element.tooltip( "instance" ).liveRegion; |
| 63 |
assert.deepEqual( $( "#" + element.data( "ui-tooltip-id" ) ).text(), "customstring" ); |
| 64 |
assert.equal( liveRegion.children().last().html().toLowerCase(), "<div>cu<b>s</b>tomstring</div>", |
| 65 |
"The accessibility live region will strip the ids but keep the structure" ); |
| 66 |
element.tooltip( "destroy" ); |
| 67 |
} ); |
| 68 |
|
| 69 |
QUnit.test( "content: sync + async callback", function( assert ) { |
| 70 |
var ready = assert.async(); |
| 71 |
assert.expect( 2 ); |
| 72 |
var element = $( "#tooltipped1" ).tooltip( { |
| 73 |
content: function( response ) { |
| 74 |
setTimeout( function() { |
| 75 |
assert.deepEqual( $( "#" + element.data( "ui-tooltip-id" ) ).text(), "loading..." ); |
| 76 |
|
| 77 |
response( "customstring2" ); |
| 78 |
setTimeout( function() { |
| 79 |
assert.deepEqual( $( "#" + element.data( "ui-tooltip-id" ) ).text(), "customstring2" ); |
| 80 |
|
| 81 |
element.tooltip( "destroy" ); |
| 82 |
ready(); |
| 83 |
}, 13 ); |
| 84 |
}, 13 ); |
| 85 |
return "loading..."; |
| 86 |
} |
| 87 |
} ).tooltip( "open" ); |
| 88 |
} ); |
| 89 |
|
| 90 |
|
| 91 |
QUnit.test( "content: async callback loses focus before load", function( assert ) { |
| 92 |
var ready = assert.async(); |
| 93 |
assert.expect( 1 ); |
| 94 |
|
| 95 |
var element = $( "#tooltipped1" ).tooltip( { |
| 96 |
content: function( response ) { |
| 97 |
setTimeout( function() { |
| 98 |
element.trigger( "mouseleave" ); |
| 99 |
setTimeout( function() { |
| 100 |
response( "sometext" ); |
| 101 |
setTimeout( function() { |
| 102 |
assert.ok( !$( "#" + element.data( "ui-tooltip-id" ) ).is( ":visible" ), |
| 103 |
"Tooltip should not display" ); |
| 104 |
|
| 105 |
element.tooltip( "destroy" ); |
| 106 |
ready(); |
| 107 |
} ); |
| 108 |
} ); |
| 109 |
} ); |
| 110 |
} |
| 111 |
} ); |
| 112 |
element.trigger( "mouseover" ); |
| 113 |
} ); |
| 114 |
|
| 115 |
QUnit.test( "content: change while open", function( assert ) { |
| 116 |
assert.expect( 2 ); |
| 117 |
var element = $( "#tooltipped1" ).tooltip( { |
| 118 |
content: function() { |
| 119 |
return "old"; |
| 120 |
} |
| 121 |
} ); |
| 122 |
|
| 123 |
element.one( "tooltipopen", function( event, ui ) { |
| 124 |
assert.equal( ui.tooltip.text(), "old", "original content" ); |
| 125 |
element.tooltip( "option", "content", function() { |
| 126 |
return "new"; |
| 127 |
} ); |
| 128 |
assert.equal( ui.tooltip.text(), "new", "updated content" ); |
| 129 |
|
| 130 |
element.tooltip( "destroy" ); |
| 131 |
} ); |
| 132 |
|
| 133 |
element.tooltip( "open" ); |
| 134 |
} ); |
| 135 |
|
| 136 |
QUnit.test( "content: string", function( assert ) { |
| 137 |
assert.expect( 1 ); |
| 138 |
$( "#tooltipped1" ).tooltip( { |
| 139 |
content: "just a string", |
| 140 |
open: function( event, ui ) { |
| 141 |
assert.equal( ui.tooltip.text(), "just a string" ); |
| 142 |
} |
| 143 |
} ).tooltip( "open" ).tooltip( "destroy" ); |
| 144 |
} ); |
| 145 |
|
| 146 |
QUnit.test( "content: element", function( assert ) { |
| 147 |
assert.expect( 1 ); |
| 148 |
var content = "<p>this is a <i>test</i> of the emergency broadcast system.</p>", |
| 149 |
element = $( content )[ 0 ]; |
| 150 |
$( "#tooltipped1" ).tooltip( { |
| 151 |
content: element, |
| 152 |
open: function( event, ui ) { |
| 153 |
assert.equal( ui.tooltip.children().html().toLowerCase(), content ); |
| 154 |
} |
| 155 |
} ).tooltip( "open" ).tooltip( "destroy" ); |
| 156 |
} ); |
| 157 |
|
| 158 |
QUnit.test( "content: jQuery", function( assert ) { |
| 159 |
assert.expect( 1 ); |
| 160 |
var content = "<p>this is a <i>test</i> of the emergency broadcast system.</p>", |
| 161 |
element = $( content ); |
| 162 |
$( "#tooltipped1" ).tooltip( { |
| 163 |
content: element, |
| 164 |
open: function( event, ui ) { |
| 165 |
assert.equal( ui.tooltip.children().html().toLowerCase(), content ); |
| 166 |
} |
| 167 |
} ).tooltip( "open" ).tooltip( "destroy" ); |
| 168 |
} ); |
| 169 |
|
| 170 |
QUnit.test( "items", function( assert ) { |
| 171 |
assert.expect( 2 ); |
| 172 |
var event, |
| 173 |
element = $( "#qunit-fixture" ).tooltip( { |
| 174 |
items: "#fixture-span" |
| 175 |
} ); |
| 176 |
|
| 177 |
event = $.Event( "mouseenter" ); |
| 178 |
event.target = $( "#fixture-span" )[ 0 ]; |
| 179 |
element.tooltip( "open", event ); |
| 180 |
assert.deepEqual( $( "#" + $( "#fixture-span" ).data( "ui-tooltip-id" ) ).text(), "title-text" ); |
| 181 |
|
| 182 |
|
| 183 |
event.target = $( "#tooltipped1" )[ 0 ]; |
| 184 |
element.tooltip( "open", event ); |
| 185 |
assert.deepEqual( $( "#tooltipped1" ).data( "ui-tooltip-id" ), undefined ); |
| 186 |
|
| 187 |
element.tooltip( "destroy" ); |
| 188 |
} ); |
| 189 |
|
| 190 |
QUnit.test( "track + show delay", function( assert ) { |
| 191 |
assert.expect( 2 ); |
| 192 |
var event, |
| 193 |
leftVal = 314, |
| 194 |
topVal = 159, |
| 195 |
offsetVal = 26, |
| 196 |
element = $( "#tooltipped1" ).tooltip( { |
| 197 |
track: true, |
| 198 |
show: { |
| 199 |
delay: 1 |
| 200 |
}, |
| 201 |
position: { |
| 202 |
my: "left+" + offsetVal + " top+" + offsetVal, |
| 203 |
at: "right bottom" |
| 204 |
} |
| 205 |
} ); |
| 206 |
|
| 207 |
event = $.Event( "mouseover" ); |
| 208 |
event.target = $( "#tooltipped1" )[ 0 ]; |
| 209 |
event.originalEvent = { type: "mouseover" }; |
| 210 |
event.pageX = leftVal; |
| 211 |
event.pageY = topVal; |
| 212 |
element.trigger( event ); |
| 213 |
|
| 214 |
event = $.Event( "mousemove" ); |
| 215 |
event.target = $( "#tooltipped1" )[ 0 ]; |
| 216 |
event.originalEvent = { type: "mousemove" }; |
| 217 |
event.pageX = leftVal; |
| 218 |
event.pageY = topVal; |
| 219 |
element.trigger( event ); |
| 220 |
|
| 221 |
assert.close( |
| 222 |
parseFloat( $( ".ui-tooltip" ).css( "left" ) ), |
| 223 |
leftVal + offsetVal, 0.5, |
| 224 |
"left position" |
| 225 |
); |
| 226 |
assert.close( |
| 227 |
parseFloat( $( ".ui-tooltip" ).css( "top" ) ), |
| 228 |
topVal + offsetVal, 0.5, |
| 229 |
"top position" |
| 230 |
); |
| 231 |
|
| 232 |
element.tooltip( "destroy" ); |
| 233 |
} ); |
| 234 |
|
| 235 |
QUnit.test( "track and programmatic focus", function( assert ) { |
| 236 |
assert.expect( 1 ); |
| 237 |
var element = $( "#qunit-fixture div input" ).tooltip( { |
| 238 |
track: true |
| 239 |
} ).trigger( "focus" ); |
| 240 |
assert.equal( "inputtitle", $( ".ui-tooltip" ).text() ); |
| 241 |
element.tooltip( "destroy" ); |
| 242 |
} ); |
| 243 |
|
| 244 |
} ); |