bootstrap-tooltip.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. $(function () {
  2. module("bootstrap-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.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. equals(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. .bind("show", 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. .bind("shown", 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. .bind("show", function(e) {
  77. e.preventDefault()
  78. ok(true, "show was called")
  79. start()
  80. })
  81. .bind("shown", 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. .bind("shown", function() {
  90. $(this).tooltip('hide')
  91. })
  92. .bind("hide", 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. .bind("shown", function() {
  102. $(this).tooltip('hide')
  103. })
  104. .bind("hidden", 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. .bind("shown", function() {
  114. $(this).tooltip('hide')
  115. })
  116. .bind("hide", function(e) {
  117. e.preventDefault()
  118. ok(true, "hide was called")
  119. start()
  120. })
  121. .bind("hidden", 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 not show tooltip if leave event occurs before delay expires", function () {
  157. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  158. .appendTo('#qunit-fixture')
  159. .tooltip({ delay: 100 })
  160. stop()
  161. tooltip.trigger('mouseenter')
  162. setTimeout(function () {
  163. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  164. tooltip.trigger('mouseout')
  165. setTimeout(function () {
  166. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  167. start()
  168. }, 100)
  169. }, 50)
  170. })
  171. test("should show tooltip if leave event hasn't occured before delay expires", function () {
  172. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  173. .appendTo('#qunit-fixture')
  174. .tooltip({ delay: 150 })
  175. stop()
  176. tooltip.trigger('mouseenter')
  177. setTimeout(function () {
  178. ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in')
  179. }, 100)
  180. setTimeout(function () {
  181. ok($(".tooltip").is('.fade.in'), 'tooltip has faded in')
  182. start()
  183. }, 200)
  184. })
  185. test("should destroy tooltip", function () {
  186. var tooltip = $('<div/>').tooltip().on('click.foo', function(){})
  187. ok(tooltip.data('tooltip'), 'tooltip has data')
  188. ok($._data(tooltip[0], 'events').mouseover && $._data(tooltip[0], 'events').mouseout, 'tooltip has hover event')
  189. ok($._data(tooltip[0], 'events').click[0].namespace == 'foo', 'tooltip has extra click.foo event')
  190. tooltip.tooltip('show')
  191. tooltip.tooltip('destroy')
  192. ok(!tooltip.hasClass('in'), 'tooltip is hidden')
  193. ok(!$._data(tooltip[0], 'tooltip'), 'tooltip does not have data')
  194. ok($._data(tooltip[0], 'events').click[0].namespace == 'foo', 'tooltip still has click.foo')
  195. ok(!$._data(tooltip[0], 'events').mouseover && !$._data(tooltip[0], 'events').mouseout, 'tooltip does not have any events')
  196. })
  197. test("should show tooltip with delegate selector on click", function () {
  198. var div = $('<div><a href="#" rel="tooltip" title="Another tooltip"></a></div>')
  199. var tooltip = div.appendTo('#qunit-fixture')
  200. .tooltip({ selector: 'a[rel=tooltip]',
  201. trigger: 'click' })
  202. div.find('a').trigger('click')
  203. ok($(".tooltip").is('.fade.in'), 'tooltip is faded in')
  204. })
  205. test("should show tooltip when toggle is called", function () {
  206. var tooltip = $('<a href="#" rel="tooltip" title="tooltip on toggle"></a>')
  207. .appendTo('#qunit-fixture')
  208. .tooltip({trigger: 'manual'})
  209. .tooltip('toggle')
  210. ok($(".tooltip").is('.fade.in'), 'tooltip should be toggled in')
  211. })
  212. test("should place tooltips inside the body", function () {
  213. var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
  214. .appendTo('#qunit-fixture')
  215. .tooltip({container:'body'})
  216. .tooltip('show')
  217. ok($("body > .tooltip").length, 'inside the body')
  218. ok(!$("#qunit-fixture > .tooltip").length, 'not found in parent')
  219. tooltip.tooltip('hide')
  220. })
  221. test("should place tooltip inside window", function(){
  222. var container = $("<div />").appendTo("body")
  223. .css({position: "absolute", width: 200, height: 200, bottom: 0, left: 0})
  224. , tooltip = $("<a href='#' title='Very very very very very very very very long tooltip'>Hover me</a>")
  225. .css({position: "absolute", top:0, left: 0})
  226. .appendTo(container)
  227. .tooltip({placement: "top", animate: false})
  228. .tooltip("show")
  229. stop()
  230. setTimeout(function(){
  231. ok($(".tooltip").offset().left >= 0)
  232. start()
  233. container.remove()
  234. }, 100)
  235. })
  236. test("should place tooltip on top of element", function(){
  237. var container = $("<div />").appendTo("body")
  238. .css({position: "absolute", bottom: 0, left: 0, textAlign: "right", width: 300, height: 300})
  239. , p = $("<p style='margin-top:200px' />").appendTo(container)
  240. , tooltiped = $("<a href='#' title='very very very very very very very long tooltip'>Hover me</a>")
  241. .css({marginTop: 200})
  242. .appendTo(p)
  243. .tooltip({placement: "top", animate: false})
  244. .tooltip("show")
  245. stop()
  246. setTimeout(function(){
  247. var tooltip = container.find(".tooltip")
  248. start()
  249. ok(tooltip.offset().top + tooltip.outerHeight() <= tooltiped.offset().top)
  250. container.remove()
  251. }, 100)
  252. })
  253. })