tooltip.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. $(function () {
  2. module("tooltip")
  3. test("should provide no conflict", function () {
  4. var tooltip = $.fn.tooltip.noConflict()
  5. ok(!$.fn.tooltip, 'tooltip was set back to undefined (org value)')
  6. $.fn.tooltip = tooltip
  7. })
  8. test("should be defined on jquery object", function () {
  9. var div = $("<div></div>")
  10. ok(div.tooltip, 'popover method is defined')
  11. })
  12. test("should return element", function () {
  13. var div = $("<div></div>")
  14. ok(div.tooltip() == div, 'document.body returned')
  15. })
  16. test("should expose default settings", function () {
  17. ok(!!$.fn.tooltip.Constructor.DEFAULTS, 'defaults is defined')
  18. })
  19. test("should empty title attribute", function () {
  20. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>').tooltip()
  21. ok(tooltip.attr('title') === '', 'title attribute was emptied')
  22. })
  23. test("should add data attribute for referencing original title", function () {
  24. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>').tooltip()
  25. equal(tooltip.attr('data-original-title'), 'Another tooltip', 'original title preserved in data attribute')
  26. })
  27. test("should place tooltips relative to placement option", function () {
  28. $.support.transition = false
  29. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  30. .appendTo('#qunit-fixture')
  31. .tooltip({placement: 'bottom'})
  32. .tooltip('show')
  33. ok($(".tooltip").is('.fade.bottom.in'), 'has correct classes applied')
  34. tooltip.tooltip('hide')
  35. })
  36. test("should allow html entities", function () {
  37. $.support.transition = false
  38. var tooltip = $('<a href="#" rel="tooltip" title="<b>@fat</b>"></a>')
  39. .appendTo('#qunit-fixture')
  40. .tooltip({html: true})
  41. .tooltip('show')
  42. ok($('.tooltip b').length, 'b tag was inserted')
  43. tooltip.tooltip('hide')
  44. ok(!$(".tooltip").length, 'tooltip removed')
  45. })
  46. test("should respect custom classes", function () {
  47. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  48. .appendTo('#qunit-fixture')
  49. .tooltip({ template: '<div class="tooltip some-class"><div class="tooltip-arrow"/><div class="tooltip-inner"/></div>'})
  50. .tooltip('show')
  51. ok($('.tooltip').hasClass('some-class'), 'custom class is present')
  52. tooltip.tooltip('hide')
  53. ok(!$(".tooltip").length, 'tooltip removed')
  54. })
  55. test("should fire show event", function () {
  56. stop()
  57. var tooltip = $('<div title="tooltip title"></div>')
  58. .on("show.bs.tooltip", function() {
  59. ok(true, "show was called")
  60. start()
  61. })
  62. .tooltip('show')
  63. })
  64. test("should fire shown event", function () {
  65. stop()
  66. var tooltip = $('<div title="tooltip title"></div>')
  67. .on("shown.bs.tooltip", function() {
  68. ok(true, "shown was called")
  69. start()
  70. })
  71. .tooltip('show')
  72. })
  73. test("should not fire shown event when default prevented", function () {
  74. stop()
  75. var tooltip = $('<div title="tooltip title"></div>')
  76. .on("show.bs.tooltip", function(e) {
  77. e.preventDefault()
  78. ok(true, "show was called")
  79. start()
  80. })
  81. .on("shown.bs.tooltip", function() {
  82. ok(false, "shown was called")
  83. })
  84. .tooltip('show')
  85. })
  86. test("should fire hide event", function () {
  87. stop()
  88. var tooltip = $('<div title="tooltip title"></div>')
  89. .on("shown.bs.tooltip", function() {
  90. $(this).tooltip('hide')
  91. })
  92. .on("hide.bs.tooltip", function() {
  93. ok(true, "hide was called")
  94. start()
  95. })
  96. .tooltip('show')
  97. })
  98. test("should fire hidden event", function () {
  99. stop()
  100. var tooltip = $('<div title="tooltip title"></div>')
  101. .on("shown.bs.tooltip", function() {
  102. $(this).tooltip('hide')
  103. })
  104. .on("hidden.bs.tooltip", function() {
  105. ok(true, "hidden was called")
  106. start()
  107. })
  108. .tooltip('show')
  109. })
  110. test("should not fire hidden event when default prevented", function () {
  111. stop()
  112. var tooltip = $('<div title="tooltip title"></div>')
  113. .on("shown.bs.tooltip", function() {
  114. $(this).tooltip('hide')
  115. })
  116. .on("hide.bs.tooltip", function(e) {
  117. e.preventDefault()
  118. ok(true, "hide was called")
  119. start()
  120. })
  121. .on("hidden.bs.tooltip", function() {
  122. ok(false, "hidden was called")
  123. })
  124. .tooltip('show')
  125. })
  126. test("should not show tooltip if leave event occurs before delay expires", function () {
  127. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  128. .appendTo('#qunit-fixture')
  129. .tooltip({ delay: 200 })
  130. stop()
  131. tooltip.trigger('mouseenter')
  132. setTimeout(function () {
  133. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  134. tooltip.trigger('mouseout')
  135. setTimeout(function () {
  136. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  137. start()
  138. }, 200)
  139. }, 100)
  140. })
  141. test("should not show tooltip if leave event occurs before delay expires, even if hide delay is 0", function () {
  142. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  143. .appendTo('#qunit-fixture')
  144. .tooltip({ delay: { show: 200, hide: 0} })
  145. stop()
  146. tooltip.trigger('mouseenter')
  147. setTimeout(function () {
  148. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  149. tooltip.trigger('mouseout')
  150. setTimeout(function () {
  151. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  152. start()
  153. }, 200)
  154. }, 100)
  155. })
  156. test("should wait 200 ms before hiding the tooltip", 3, function () {
  157. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  158. .appendTo('#qunit-fixture')
  159. .tooltip({ delay: { show: 0, hide: 200} })
  160. stop()
  161. tooltip.trigger('mouseenter')
  162. setTimeout(function () {
  163. ok($(".tooltip").is('.fade.in'), 'tooltip is faded in')
  164. tooltip.trigger('mouseout')
  165. setTimeout(function () {
  166. ok($(".tooltip").is('.fade.in'), '100ms:tooltip is still faded in')
  167. setTimeout(function () {
  168. ok(!$(".tooltip").is('.in'), 'tooltip removed')
  169. start()
  170. }, 150)
  171. }, 100)
  172. }, 1)
  173. })
  174. test("should not hide tooltip if leave event occurs, then tooltip is show immediately again", function () {
  175. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  176. .appendTo('#qunit-fixture')
  177. .tooltip({ delay: { show: 0, hide: 200} })
  178. stop()
  179. tooltip.trigger('mouseenter')
  180. setTimeout(function () {
  181. ok($(".tooltip").is('.fade.in'), 'tooltip is faded in')
  182. tooltip.trigger('mouseout')
  183. setTimeout(function () {
  184. ok($(".tooltip").is('.fade.in'), '100ms:tooltip is still faded in')
  185. tooltip.trigger('mouseenter')
  186. setTimeout(function () {
  187. ok($(".tooltip").is('.in'), 'tooltip removed')
  188. start()
  189. }, 150)
  190. }, 100)
  191. }, 1)
  192. })
  193. test("should not show tooltip if leave event occurs before delay expires", function () {
  194. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  195. .appendTo('#qunit-fixture')
  196. .tooltip({ delay: 100 })
  197. stop()
  198. tooltip.trigger('mouseenter')
  199. setTimeout(function () {
  200. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  201. tooltip.trigger('mouseout')
  202. setTimeout(function () {
  203. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  204. start()
  205. }, 100)
  206. }, 50)
  207. })
  208. test("should show tooltip if leave event hasn't occured before delay expires", function () {
  209. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  210. .appendTo('#qunit-fixture')
  211. .tooltip({ delay: 150 })
  212. stop()
  213. tooltip.trigger('mouseenter')
  214. setTimeout(function () {
  215. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  216. }, 100)
  217. setTimeout(function () {
  218. ok($(".tooltip").is('.fade.in'), 'tooltip has faded in')
  219. start()
  220. }, 200)
  221. })
  222. test("should destroy tooltip", function () {
  223. var tooltip = $('<div/>').tooltip().on('click.foo', function(){})
  224. ok(tooltip.data('bs.tooltip'), 'tooltip has data')
  225. ok($._data(tooltip[0], 'events').mouseover && $._data(tooltip[0], 'events').mouseout, 'tooltip has hover event')
  226. ok($._data(tooltip[0], 'events').click[0].namespace == 'foo', 'tooltip has extra click.foo event')
  227. tooltip.tooltip('show')
  228. tooltip.tooltip('destroy')
  229. ok(!tooltip.hasClass('in'), 'tooltip is hidden')
  230. ok(!$._data(tooltip[0], 'bs.tooltip'), 'tooltip does not have data')
  231. ok($._data(tooltip[0], 'events').click[0].namespace == 'foo', 'tooltip still has click.foo')
  232. ok(!$._data(tooltip[0], 'events').mouseover && !$._data(tooltip[0], 'events').mouseout, 'tooltip does not have any events')
  233. })
  234. test("should show tooltip with delegate selector on click", function () {
  235. var div = $('<div><a href="#" rel="tooltip" title="Another tooltip"></a></div>')
  236. var tooltip = div.appendTo('#qunit-fixture')
  237. .tooltip({ selector: 'a[rel=tooltip]',
  238. trigger: 'click' })
  239. div.find('a').trigger('click')
  240. ok($(".tooltip").is('.fade.in'), 'tooltip is faded in')
  241. })
  242. test("should show tooltip when toggle is called", function () {
  243. var tooltip = $('<a href="#" rel="tooltip" title="tooltip on toggle"></a>')
  244. .appendTo('#qunit-fixture')
  245. .tooltip({trigger: 'manual'})
  246. .tooltip('toggle')
  247. ok($(".tooltip").is('.fade.in'), 'tooltip should be toggled in')
  248. })
  249. test("should place tooltips inside the body", function () {
  250. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  251. .appendTo('#qunit-fixture')
  252. .tooltip({container:'body'})
  253. .tooltip('show')
  254. ok($("body > .tooltip").length, 'inside the body')
  255. ok(!$("#qunit-fixture > .tooltip").length, 'not found in parent')
  256. tooltip.tooltip('hide')
  257. })
  258. test("should place tooltip inside window", function(){
  259. var container = $("<div />").appendTo("body")
  260. .css({position: "absolute", width: 200, height: 200, bottom: 0, left: 0})
  261. , tooltip = $("<a href='#' title='Very very very very very very very very long tooltip'>Hover me</a>")
  262. .css({position: "absolute", top:0, left: 0})
  263. .appendTo(container)
  264. .tooltip({placement: "top", animate: false})
  265. .tooltip("show")
  266. stop()
  267. setTimeout(function(){
  268. ok($(".tooltip").offset().left >= 0)
  269. start()
  270. container.remove()
  271. }, 100)
  272. })
  273. test("should place tooltip on top of element", function(){
  274. var container = $("<div />").appendTo("body")
  275. .css({position: "absolute", bottom: 0, left: 0, textAlign: "right", width: 300, height: 300})
  276. , p = $("<p style='margin-top:200px' />").appendTo(container)
  277. , tooltiped = $("<a href='#' title='very very very very very very very long tooltip'>Hover me</a>")
  278. .css({marginTop: 200})
  279. .appendTo(p)
  280. .tooltip({placement: "top", animate: false})
  281. .tooltip("show")
  282. stop()
  283. setTimeout(function(){
  284. var tooltip = container.find(".tooltip")
  285. start()
  286. ok(tooltip.offset().top + tooltip.outerHeight() <= tooltiped.offset().top)
  287. container.remove()
  288. }, 100)
  289. })
  290. test("should add position class before positioning so that position-specific styles are taken into account", function(){
  291. $("head").append('<style> .tooltip.right { white-space: nowrap; } .tooltip.right .tooltip-inner { max-width: none; } </style>')
  292. var container = $("<div />").appendTo("body")
  293. , target = $('<a href="#" rel="tooltip" title="very very very very very very very very long tooltip in one line"></a>')
  294. .appendTo(container)
  295. .tooltip({placement: 'right'})
  296. .tooltip('show')
  297. , tooltip = container.find(".tooltip")
  298. ok( Math.round(target.offset().top + target[0].offsetHeight/2 - tooltip[0].offsetHeight/2) === Math.round(tooltip.offset().top) )
  299. target.tooltip('hide')
  300. })
  301. test("tooltip title test #1", function () {
  302. var tooltip = $('<a href="#" rel="tooltip" title="Simple tooltip" style="display: inline-block; position: absolute; top: 0; left: 0;"></a>')
  303. .appendTo('#qunit-fixture')
  304. .tooltip({
  305. })
  306. .tooltip('show')
  307. equal($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title from title attribute is set')
  308. tooltip.tooltip('hide')
  309. ok(!$(".tooltip").length, 'tooltip removed')
  310. })
  311. test("tooltip title test #2", function () {
  312. var tooltip = $('<a href="#" rel="tooltip" title="Simple tooltip" style="display: inline-block; position: absolute; top: 0; left: 0;"></a>')
  313. .appendTo('#qunit-fixture')
  314. .tooltip({
  315. title: 'This is a tooltip with some content'
  316. })
  317. .tooltip('show')
  318. equal($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title is set from title attribute while prefered over title option')
  319. tooltip.tooltip('hide')
  320. ok(!$(".tooltip").length, 'tooltip removed')
  321. })
  322. test("tooltip title test #3", function () {
  323. var tooltip = $('<a href="#" rel="tooltip" style="display: inline-block; position: absolute; top: 0; left: 0;"></a>')
  324. .appendTo('#qunit-fixture')
  325. .tooltip({
  326. title: 'This is a tooltip with some content'
  327. })
  328. .tooltip('show')
  329. equal($('.tooltip').children('.tooltip-inner').text(), 'This is a tooltip with some content', 'title from title option is set')
  330. tooltip.tooltip('hide')
  331. ok(!$(".tooltip").length, 'tooltip removed')
  332. })
  333. test("tooltips should be placed dynamically, with the dynamic placement option", function () {
  334. $.support.transition = false
  335. var ttContainer = $('<div id="dynamic-tt-test"/>').css({
  336. 'height' : 400
  337. , 'overflow' : 'hidden'
  338. , 'position' : 'absolute'
  339. , 'top' : 0
  340. , 'left' : 0
  341. , 'width' : 600})
  342. .appendTo('body')
  343. var topTooltip = $('<div style="display: inline-block; position: absolute; left: 0; top: 0;" rel="tooltip" title="Top tooltip">Top Dynamic Tooltip</div>')
  344. .appendTo('#dynamic-tt-test')
  345. .tooltip({placement: 'auto'})
  346. .tooltip('show')
  347. ok($(".tooltip").is('.bottom'), 'top positioned tooltip is dynamically positioned bottom')
  348. topTooltip.tooltip('hide')
  349. var rightTooltip = $('<div style="display: inline-block; position: absolute; right: 0;" rel="tooltip" title="Right tooltip">Right Dynamic Tooltip</div>')
  350. .appendTo('#dynamic-tt-test')
  351. .tooltip({placement: 'right auto'})
  352. .tooltip('show')
  353. ok($(".tooltip").is('.left'), 'right positioned tooltip is dynamically positioned left')
  354. rightTooltip.tooltip('hide')
  355. var bottomTooltip = $('<div style="display: inline-block; position: absolute; bottom: 0;" rel="tooltip" title="Bottom tooltip">Bottom Dynamic Tooltip</div>')
  356. .appendTo('#dynamic-tt-test')
  357. .tooltip({placement: 'auto bottom'})
  358. .tooltip('show')
  359. ok($(".tooltip").is('.top'), 'bottom positioned tooltip is dynamically positioned top')
  360. bottomTooltip.tooltip('hide')
  361. var leftTooltip = $('<div style="display: inline-block; position: absolute; left: 0;" rel="tooltip" title="Left tooltip">Left Dynamic Tooltip</div>')
  362. .appendTo('#dynamic-tt-test')
  363. .tooltip({placement: 'auto left'})
  364. .tooltip('show')
  365. ok($(".tooltip").is('.right'), 'left positioned tooltip is dynamically positioned right')
  366. leftTooltip.tooltip('hide')
  367. ttContainer.remove()
  368. })
  369. })