/bokehjs/src/vendor/bootstrap-3.1.1/js/tests/unit/tooltip.js

https://github.com/marilsoncampos/bokeh · JavaScript · 432 lines · 366 code · 65 blank · 1 comment · 14 complexity · be917c01be7a07de94087c2f7f5c2542 MD5 · raw file

  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(Math.round(tooltip.offset().top + tooltip.outerHeight()) <= Math.round(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. // this is some dumb hack shit because sub pixels in firefox
  299. var top = Math.round(target.offset().top + (target[0].offsetHeight / 2) - (tooltip[0].offsetHeight / 2))
  300. var top2 = Math.round(tooltip.offset().top)
  301. var topDiff = top - top2
  302. ok(topDiff <= 1 && topDiff >= -1)
  303. target.tooltip('hide')
  304. })
  305. test('tooltip title test #1', function () {
  306. var tooltip = $('<a href="#" rel="tooltip" title="Simple tooltip" style="display: inline-block; position: absolute; top: 0; left: 0;"></a>')
  307. .appendTo('#qunit-fixture')
  308. .tooltip({
  309. })
  310. .tooltip('show')
  311. equal($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title from title attribute is set')
  312. tooltip.tooltip('hide')
  313. ok(!$('.tooltip').length, 'tooltip removed')
  314. })
  315. test('tooltip title test #2', function () {
  316. var tooltip = $('<a href="#" rel="tooltip" title="Simple tooltip" style="display: inline-block; position: absolute; top: 0; left: 0;"></a>')
  317. .appendTo('#qunit-fixture')
  318. .tooltip({
  319. title: 'This is a tooltip with some content'
  320. })
  321. .tooltip('show')
  322. equal($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title is set from title attribute while prefered over title option')
  323. tooltip.tooltip('hide')
  324. ok(!$('.tooltip').length, 'tooltip removed')
  325. })
  326. test('tooltip title test #3', function () {
  327. var tooltip = $('<a href="#" rel="tooltip" style="display: inline-block; position: absolute; top: 0; left: 0;"></a>')
  328. .appendTo('#qunit-fixture')
  329. .tooltip({
  330. title: 'This is a tooltip with some content'
  331. })
  332. .tooltip('show')
  333. equal($('.tooltip').children('.tooltip-inner').text(), 'This is a tooltip with some content', 'title from title option is set')
  334. tooltip.tooltip('hide')
  335. ok(!$('.tooltip').length, 'tooltip removed')
  336. })
  337. test('tooltips should be placed dynamically, with the dynamic placement option', function () {
  338. $.support.transition = false
  339. var ttContainer = $('<div id="dynamic-tt-test"/>').css({
  340. 'height' : 400,
  341. 'overflow' : 'hidden',
  342. 'position' : 'absolute',
  343. 'top' : 0,
  344. 'left' : 0,
  345. 'width' : 600})
  346. .appendTo('body')
  347. var topTooltip = $('<div style="display: inline-block; position: absolute; left: 0; top: 0;" rel="tooltip" title="Top tooltip">Top Dynamic Tooltip</div>')
  348. .appendTo('#dynamic-tt-test')
  349. .tooltip({placement: 'auto'})
  350. .tooltip('show')
  351. ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned bottom')
  352. topTooltip.tooltip('hide')
  353. var rightTooltip = $('<div style="display: inline-block; position: absolute; right: 0;" rel="tooltip" title="Right tooltip">Right Dynamic Tooltip</div>')
  354. .appendTo('#dynamic-tt-test')
  355. .tooltip({placement: 'right auto'})
  356. .tooltip('show')
  357. ok($('.tooltip').is('.left'), 'right positioned tooltip is dynamically positioned left')
  358. rightTooltip.tooltip('hide')
  359. var leftTooltip = $('<div style="display: inline-block; position: absolute; left: 0;" rel="tooltip" title="Left tooltip">Left Dynamic Tooltip</div>')
  360. .appendTo('#dynamic-tt-test')
  361. .tooltip({placement: 'auto left'})
  362. .tooltip('show')
  363. ok($('.tooltip').is('.right'), 'left positioned tooltip is dynamically positioned right')
  364. leftTooltip.tooltip('hide')
  365. ttContainer.remove()
  366. })
  367. })