7d3c243739ac5027a030d66bf6168003e56936d3.svn-base 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. $(function () {
  2. 'use strict';
  3. QUnit.module('tooltip plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).tooltip, 'tooltip method is defined')
  7. })
  8. QUnit.module('tooltip', {
  9. beforeEach: function () {
  10. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  11. $.fn.bootstrapTooltip = $.fn.tooltip.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.tooltip = $.fn.bootstrapTooltip
  15. delete $.fn.bootstrapTooltip
  16. $('.tooltip').remove()
  17. }
  18. })
  19. QUnit.test('should provide no conflict', function (assert) {
  20. assert.expect(1)
  21. assert.strictEqual($.fn.tooltip, undefined, 'tooltip was set back to undefined (org value)')
  22. })
  23. QUnit.test('should return jquery collection containing the element', function (assert) {
  24. assert.expect(2)
  25. var $el = $('<div/>')
  26. var $tooltip = $el.bootstrapTooltip()
  27. assert.ok($tooltip instanceof $, 'returns jquery collection')
  28. assert.strictEqual($tooltip[0], $el[0], 'collection contains element')
  29. })
  30. QUnit.test('should expose default settings', function (assert) {
  31. assert.expect(1)
  32. assert.ok($.fn.bootstrapTooltip.Constructor.Default, 'defaults is defined')
  33. })
  34. QUnit.test('should empty title attribute', function (assert) {
  35. assert.expect(1)
  36. var $trigger = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>').bootstrapTooltip()
  37. assert.strictEqual($trigger.attr('title'), '', 'title attribute was emptied')
  38. })
  39. QUnit.test('should add data attribute for referencing original title', function (assert) {
  40. assert.expect(1)
  41. var $trigger = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>').bootstrapTooltip()
  42. assert.strictEqual($trigger.attr('data-original-title'), 'Another tooltip', 'original title preserved in data attribute')
  43. })
  44. QUnit.test('should add aria-describedby to the trigger on show', function (assert) {
  45. assert.expect(3)
  46. var $trigger = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  47. .bootstrapTooltip()
  48. .appendTo('#qunit-fixture')
  49. .bootstrapTooltip('show')
  50. var id = $('.tooltip').attr('id')
  51. assert.strictEqual($('#' + id).length, 1, 'has a unique id')
  52. assert.strictEqual($('.tooltip').attr('aria-describedby'), $trigger.attr('id'), 'tooltip id and aria-describedby on trigger match')
  53. assert.ok($trigger[0].hasAttribute('aria-describedby'), 'trigger has aria-describedby')
  54. })
  55. QUnit.test('should remove aria-describedby from trigger on hide', function (assert) {
  56. assert.expect(2)
  57. var $trigger = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  58. .bootstrapTooltip()
  59. .appendTo('#qunit-fixture')
  60. $trigger.bootstrapTooltip('show')
  61. assert.ok($trigger[0].hasAttribute('aria-describedby'), 'trigger has aria-describedby')
  62. $trigger.bootstrapTooltip('hide')
  63. assert.ok(!$trigger[0].hasAttribute('aria-describedby'), 'trigger does not have aria-describedby')
  64. })
  65. QUnit.test('should assign a unique id tooltip element', function (assert) {
  66. assert.expect(2)
  67. $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  68. .appendTo('#qunit-fixture')
  69. .bootstrapTooltip('show')
  70. var id = $('.tooltip').attr('id')
  71. assert.strictEqual($('#' + id).length, 1, 'tooltip has unique id')
  72. assert.strictEqual(id.indexOf('tooltip'), 0, 'tooltip id has prefix')
  73. })
  74. QUnit.test('should place tooltips relative to placement option', function (assert) {
  75. assert.expect(2)
  76. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  77. .appendTo('#qunit-fixture')
  78. .bootstrapTooltip({ placement: 'bottom' })
  79. $tooltip.bootstrapTooltip('show')
  80. assert
  81. .ok($('.tooltip')
  82. .is('.fade.bs-tether-element-attached-top.bs-tether-element-attached-center.in'), 'has correct classes applied')
  83. $tooltip.bootstrapTooltip('hide')
  84. assert.strictEqual($tooltip.data('bs.tooltip').tip.parentNode, null, 'tooltip removed')
  85. })
  86. QUnit.test('should allow html entities', function (assert) {
  87. assert.expect(2)
  88. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="&lt;b&gt;@fat&lt;/b&gt;"/>')
  89. .appendTo('#qunit-fixture')
  90. .bootstrapTooltip({ html: true })
  91. $tooltip.bootstrapTooltip('show')
  92. assert.notEqual($('.tooltip b').length, 0, 'b tag was inserted')
  93. $tooltip.bootstrapTooltip('hide')
  94. assert.strictEqual($tooltip.data('bs.tooltip').tip.parentNode, null, 'tooltip removed')
  95. })
  96. QUnit.test('should respect custom classes', function (assert) {
  97. assert.expect(2)
  98. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  99. .appendTo('#qunit-fixture')
  100. .bootstrapTooltip({ template: '<div class="tooltip some-class"><div class="tooltip-arrow"/><div class="tooltip-inner"/></div>' })
  101. $tooltip.bootstrapTooltip('show')
  102. assert.ok($('.tooltip').hasClass('some-class'), 'custom class is present')
  103. $tooltip.bootstrapTooltip('hide')
  104. assert.strictEqual($tooltip.data('bs.tooltip').tip.parentNode, null, 'tooltip removed')
  105. })
  106. QUnit.test('should fire show event', function (assert) {
  107. assert.expect(1)
  108. var done = assert.async()
  109. $('<div title="tooltip title"/>')
  110. .on('show.bs.tooltip', function () {
  111. assert.ok(true, 'show event fired')
  112. done()
  113. })
  114. .bootstrapTooltip('show')
  115. })
  116. QUnit.test('should fire inserted event', function (assert) {
  117. assert.expect(2)
  118. var done = assert.async()
  119. $('<div title="tooltip title"/>')
  120. .appendTo('#qunit-fixture')
  121. .on('inserted.bs.tooltip', function () {
  122. assert.notEqual($('.tooltip').length, 0, 'tooltip was inserted')
  123. assert.ok(true, 'inserted event fired')
  124. done()
  125. })
  126. .bootstrapTooltip('show')
  127. })
  128. QUnit.test('should fire shown event', function (assert) {
  129. assert.expect(1)
  130. var done = assert.async()
  131. $('<div title="tooltip title"></div>')
  132. .appendTo('#qunit-fixture')
  133. .on('shown.bs.tooltip', function () {
  134. assert.ok(true, 'shown was called')
  135. done()
  136. })
  137. .bootstrapTooltip('show')
  138. })
  139. QUnit.test('should not fire shown event when show was prevented', function (assert) {
  140. assert.expect(1)
  141. var done = assert.async()
  142. $('<div title="tooltip title"/>')
  143. .on('show.bs.tooltip', function (e) {
  144. e.preventDefault()
  145. assert.ok(true, 'show event fired')
  146. done()
  147. })
  148. .on('shown.bs.tooltip', function () {
  149. assert.ok(false, 'shown event fired')
  150. })
  151. .bootstrapTooltip('show')
  152. })
  153. QUnit.test('should fire hide event', function (assert) {
  154. assert.expect(1)
  155. var done = assert.async()
  156. $('<div title="tooltip title"/>')
  157. .appendTo('#qunit-fixture')
  158. .on('shown.bs.tooltip', function () {
  159. $(this).bootstrapTooltip('hide')
  160. })
  161. .on('hide.bs.tooltip', function () {
  162. assert.ok(true, 'hide event fired')
  163. done()
  164. })
  165. .bootstrapTooltip('show')
  166. })
  167. QUnit.test('should fire hidden event', function (assert) {
  168. assert.expect(1)
  169. var done = assert.async()
  170. $('<div title="tooltip title"/>')
  171. .appendTo('#qunit-fixture')
  172. .on('shown.bs.tooltip', function () {
  173. $(this).bootstrapTooltip('hide')
  174. })
  175. .on('hidden.bs.tooltip', function () {
  176. assert.ok(true, 'hidden event fired')
  177. done()
  178. })
  179. .bootstrapTooltip('show')
  180. })
  181. QUnit.test('should not fire hidden event when hide was prevented', function (assert) {
  182. assert.expect(1)
  183. var done = assert.async()
  184. $('<div title="tooltip title"/>')
  185. .appendTo('#qunit-fixture')
  186. .on('shown.bs.tooltip', function () {
  187. $(this).bootstrapTooltip('hide')
  188. })
  189. .on('hide.bs.tooltip', function (e) {
  190. e.preventDefault()
  191. assert.ok(true, 'hide event fired')
  192. done()
  193. })
  194. .on('hidden.bs.tooltip', function () {
  195. assert.ok(false, 'hidden event fired')
  196. })
  197. .bootstrapTooltip('show')
  198. })
  199. QUnit.test('should destroy tooltip', function (assert) {
  200. assert.expect(7)
  201. var $tooltip = $('<div/>')
  202. .bootstrapTooltip()
  203. .on('click.foo', function () {})
  204. assert.ok($tooltip.data('bs.tooltip'), 'tooltip has data')
  205. assert.ok($._data($tooltip[0], 'events').mouseover && $._data($tooltip[0], 'events').mouseout, 'tooltip has hover events')
  206. assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip has extra click.foo event')
  207. $tooltip.bootstrapTooltip('show')
  208. $tooltip.bootstrapTooltip('dispose')
  209. assert.ok(!$tooltip.hasClass('in'), 'tooltip is hidden')
  210. assert.ok(!$._data($tooltip[0], 'bs.tooltip'), 'tooltip does not have data')
  211. assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip still has click.foo')
  212. assert.ok(!$._data($tooltip[0], 'events').mouseover && !$._data($tooltip[0], 'events').mouseout, 'tooltip does not have hover events')
  213. })
  214. // QUnit.test('should show tooltip with delegate selector on click', function (assert) {
  215. // assert.expect(2)
  216. // var $div = $('<div><a href="javascript:;" rel="tooltip" title="Another tooltip"/></div>')
  217. // .appendTo('#qunit-fixture')
  218. // .bootstrapTooltip({
  219. // selector: 'a[rel="tooltip"]',
  220. // trigger: 'click'
  221. // })
  222. // $div.find('a').trigger('click')
  223. // assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in')
  224. // $div.find('a').trigger('click')
  225. // assert.strictEqual($div.data('bs.tooltip').tip.parentNode, null, 'tooltip removed')
  226. // })
  227. QUnit.test('should show tooltip when toggle is called', function (assert) {
  228. assert.expect(1)
  229. $('<a href="javascript:;" rel="tooltip" title="tooltip on toggle"/>')
  230. .appendTo('#qunit-fixture')
  231. .bootstrapTooltip({ trigger: 'manual' })
  232. .bootstrapTooltip('toggle')
  233. assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in')
  234. })
  235. QUnit.test('should hide previously shown tooltip when toggle is called on tooltip', function (assert) {
  236. assert.expect(1)
  237. $('<a href="javascript:;" rel="tooltip" title="tooltip on toggle">@ResentedHook</a>')
  238. .appendTo('#qunit-fixture')
  239. .bootstrapTooltip({ trigger: 'manual' })
  240. .bootstrapTooltip('show')
  241. $('.tooltip').bootstrapTooltip('toggle')
  242. assert.ok($('.tooltip').not('.fade.in'), 'tooltip was faded out')
  243. })
  244. QUnit.test('should place tooltips inside body when container is body', function (assert) {
  245. assert.expect(3)
  246. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  247. .appendTo('#qunit-fixture')
  248. .bootstrapTooltip({ container: 'body' })
  249. .bootstrapTooltip('show')
  250. assert.notEqual($('body > .tooltip').length, 0, 'tooltip is direct descendant of body')
  251. assert.strictEqual($('#qunit-fixture > .tooltip').length, 0, 'tooltip is not in parent')
  252. $tooltip.bootstrapTooltip('hide')
  253. assert.strictEqual($('body > .tooltip').length, 0, 'tooltip was removed from dom')
  254. })
  255. QUnit.test('should add position class before positioning so that position-specific styles are taken into account', function (assert) {
  256. assert.expect(1)
  257. var styles = '<style>'
  258. + '.tooltip.right { white-space: nowrap; }'
  259. + '.tooltip.right .tooltip-inner { max-width: none; }'
  260. + '</style>'
  261. var $styles = $(styles).appendTo('head')
  262. var $container = $('<div/>').appendTo('#qunit-fixture')
  263. var $target = $('<a href="javascript:;" rel="tooltip" title="very very very very very very very very long tooltip in one line"/>')
  264. .appendTo($container)
  265. .bootstrapTooltip({
  266. placement: 'right'
  267. })
  268. .bootstrapTooltip('show')
  269. var $tooltip = $($target.data('bs.tooltip').tip)
  270. // this is some dumb hack shit because sub pixels in firefox
  271. var top = Math.round($target.offset().top + ($target[0].offsetHeight / 2) - ($tooltip[0].offsetHeight / 2))
  272. var top2 = Math.round($tooltip.offset().top)
  273. var topDiff = top - top2
  274. assert.ok(topDiff <= 1 && topDiff >= -1)
  275. $target.bootstrapTooltip('hide')
  276. $container.remove()
  277. $styles.remove()
  278. })
  279. QUnit.test('should use title attribute for tooltip text', function (assert) {
  280. assert.expect(2)
  281. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Simple tooltip"/>')
  282. .appendTo('#qunit-fixture')
  283. .bootstrapTooltip()
  284. $tooltip.bootstrapTooltip('show')
  285. assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title from title attribute is set')
  286. $tooltip.bootstrapTooltip('hide')
  287. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  288. })
  289. QUnit.test('should prefer title attribute over title option', function (assert) {
  290. assert.expect(2)
  291. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Simple tooltip"/>')
  292. .appendTo('#qunit-fixture')
  293. .bootstrapTooltip({
  294. title: 'This is a tooltip with some content'
  295. })
  296. $tooltip.bootstrapTooltip('show')
  297. assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title is set from title attribute while preferred over title option')
  298. $tooltip.bootstrapTooltip('hide')
  299. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  300. })
  301. QUnit.test('should use title option', function (assert) {
  302. assert.expect(2)
  303. var $tooltip = $('<a href="javascript:;" rel="tooltip"/>')
  304. .appendTo('#qunit-fixture')
  305. .bootstrapTooltip({
  306. title: 'This is a tooltip with some content'
  307. })
  308. $tooltip.bootstrapTooltip('show')
  309. assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'This is a tooltip with some content', 'title from title option is set')
  310. $tooltip.bootstrapTooltip('hide')
  311. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  312. })
  313. QUnit.test('should not error when trying to show an top-placed tooltip that has been removed from the dom', function (assert) {
  314. assert.expect(1)
  315. var passed = true
  316. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  317. .appendTo('#qunit-fixture')
  318. .one('show.bs.tooltip', function () {
  319. $(this).remove()
  320. })
  321. .bootstrapTooltip({ placement: 'top' })
  322. try {
  323. $tooltip.bootstrapTooltip('show')
  324. } catch (err) {
  325. passed = false
  326. console.log(err)
  327. }
  328. assert.ok(passed, '.tooltip(\'show\') should not throw an error if element no longer is in dom')
  329. })
  330. QUnit.test('should place tooltip on top of element', function (assert) {
  331. assert.expect(1)
  332. var done = assert.async()
  333. var containerHTML = '<div>'
  334. + '<p style="margin-top: 200px">'
  335. + '<a href="javascript:;" title="very very very very very very very long tooltip">Hover me</a>'
  336. + '</p>'
  337. + '</div>'
  338. var $container = $(containerHTML)
  339. .css({
  340. position: 'absolute',
  341. bottom: 0,
  342. left: 0,
  343. textAlign: 'right',
  344. width: 300,
  345. height: 300
  346. })
  347. .appendTo('#qunit-fixture')
  348. var $trigger = $container
  349. .find('a')
  350. .css('margin-top', 200)
  351. .bootstrapTooltip({
  352. placement: 'top',
  353. animate: false
  354. })
  355. .bootstrapTooltip('show')
  356. var $tooltip = $($trigger.data('bs.tooltip').tip)
  357. setTimeout(function () {
  358. assert.ok(Math.round($tooltip.offset().top + $tooltip.outerHeight()) <= Math.round($trigger.offset().top))
  359. done()
  360. }, 0)
  361. })
  362. QUnit.test('should show tooltip if leave event hasn\'t occurred before delay expires', function (assert) {
  363. assert.expect(2)
  364. var done = assert.async()
  365. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  366. .appendTo('#qunit-fixture')
  367. .bootstrapTooltip({ delay: 150 })
  368. setTimeout(function () {
  369. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip is not faded in')
  370. }, 100)
  371. setTimeout(function () {
  372. assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip is faded in')
  373. done()
  374. }, 200)
  375. $tooltip.trigger('mouseenter')
  376. })
  377. QUnit.test('should not show tooltip if leave event occurs before delay expires', function (assert) {
  378. assert.expect(2)
  379. var done = assert.async()
  380. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  381. .appendTo('#qunit-fixture')
  382. .bootstrapTooltip({ delay: 150 })
  383. setTimeout(function () {
  384. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
  385. $tooltip.trigger('mouseout')
  386. }, 100)
  387. setTimeout(function () {
  388. assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in')
  389. done()
  390. }, 200)
  391. $tooltip.trigger('mouseenter')
  392. })
  393. QUnit.test('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', function (assert) {
  394. assert.expect(3)
  395. var done = assert.async()
  396. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  397. .appendTo('#qunit-fixture')
  398. .bootstrapTooltip({ delay: { show: 0, hide: 150 }})
  399. setTimeout(function () {
  400. assert.ok($('.tooltip').is('.fade.in'), '1ms: tooltip faded in')
  401. $tooltip.trigger('mouseout')
  402. setTimeout(function () {
  403. assert.ok($('.tooltip').is('.fade.in'), '100ms: tooltip still faded in')
  404. $tooltip.trigger('mouseenter')
  405. }, 100)
  406. setTimeout(function () {
  407. assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip still faded in')
  408. done()
  409. }, 200)
  410. }, 0)
  411. $tooltip.trigger('mouseenter')
  412. })
  413. QUnit.test('should not show tooltip if leave event occurs before delay expires', function (assert) {
  414. assert.expect(2)
  415. var done = assert.async()
  416. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  417. .appendTo('#qunit-fixture')
  418. .bootstrapTooltip({ delay: 150 })
  419. setTimeout(function () {
  420. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
  421. $tooltip.trigger('mouseout')
  422. }, 100)
  423. setTimeout(function () {
  424. assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in')
  425. done()
  426. }, 200)
  427. $tooltip.trigger('mouseenter')
  428. })
  429. QUnit.test('should not show tooltip if leave event occurs before delay expires, even if hide delay is 0', function (assert) {
  430. assert.expect(2)
  431. var done = assert.async()
  432. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  433. .appendTo('#qunit-fixture')
  434. .bootstrapTooltip({ delay: { show: 150, hide: 0 }})
  435. setTimeout(function () {
  436. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
  437. $tooltip.trigger('mouseout')
  438. }, 100)
  439. setTimeout(function () {
  440. assert.ok(!$('.tooltip').is('.fade.in'), '250ms: tooltip not faded in')
  441. done()
  442. }, 250)
  443. $tooltip.trigger('mouseenter')
  444. })
  445. QUnit.test('should wait 200ms before hiding the tooltip', function (assert) {
  446. assert.expect(3)
  447. var done = assert.async()
  448. var $tooltip = $('<a href="javascript:;" rel="tooltip" title="Another tooltip"/>')
  449. .appendTo('#qunit-fixture')
  450. .bootstrapTooltip({ delay: { show: 0, hide: 150 }})
  451. setTimeout(function () {
  452. assert.ok($($tooltip.data('bs.tooltip').tip).is('.fade.in'), '1ms: tooltip faded in')
  453. $tooltip.trigger('mouseout')
  454. setTimeout(function () {
  455. assert.ok($($tooltip.data('bs.tooltip').tip).is('.fade.in'), '100ms: tooltip still faded in')
  456. }, 100)
  457. setTimeout(function () {
  458. assert.ok(!$($tooltip.data('bs.tooltip').tip).is('.in'), '200ms: tooltip removed')
  459. done()
  460. }, 200)
  461. }, 0)
  462. $tooltip.trigger('mouseenter')
  463. })
  464. QUnit.test('should correctly position tooltips on SVG elements', function (assert) {
  465. if (!window.SVGElement) {
  466. // Skip IE8 since it doesn't support SVG
  467. assert.expect(0)
  468. return
  469. }
  470. assert.expect(2)
  471. var done = assert.async()
  472. var styles = '<style>'
  473. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  474. + '.tooltip { position: absolute; }'
  475. + '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
  476. + '</style>'
  477. var $styles = $(styles).appendTo('head')
  478. $('#qunit-fixture').append(
  479. '<div style="position: fixed; top: 0; left: 0;">'
  480. + ' <svg width="200" height="200">'
  481. + ' <circle cx="100" cy="100" r="10" title="m" id="theCircle" />'
  482. + ' </svg>'
  483. + '</div>')
  484. var $circle = $('#theCircle')
  485. $circle
  486. .on('shown.bs.tooltip', function () {
  487. var offset = $('.tooltip').offset()
  488. $styles.remove()
  489. assert.ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
  490. $circle.bootstrapTooltip('hide')
  491. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  492. done()
  493. })
  494. .bootstrapTooltip({ placement: 'top', trigger: 'manual' })
  495. $circle.bootstrapTooltip('show')
  496. })
  497. QUnit.test('should not reload the tooltip on subsequent mouseenter events', function (assert) {
  498. assert.expect(1)
  499. var titleHtml = function () {
  500. var uid = Util.getUID('tooltip')
  501. return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>'
  502. }
  503. var $tooltip = $('<span id="tt-outer" rel="tooltip" data-trigger="hover" data-placement="top">some text</span>')
  504. .appendTo('#qunit-fixture')
  505. $tooltip.bootstrapTooltip({
  506. html: true,
  507. animation: false,
  508. trigger: 'hover',
  509. delay: { show: 0, hide: 500 },
  510. container: $tooltip,
  511. title: titleHtml
  512. })
  513. $('#tt-outer').trigger('mouseenter')
  514. var currentUid = $('#tt-content').text()
  515. $('#tt-content').trigger('mouseenter')
  516. assert.strictEqual(currentUid, $('#tt-content').text())
  517. })
  518. QUnit.test('should not reload the tooltip if the mouse leaves and re-enters before hiding', function (assert) {
  519. assert.expect(4)
  520. var titleHtml = function () {
  521. var uid = Util.getUID('tooltip')
  522. return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>'
  523. }
  524. var $tooltip = $('<span id="tt-outer" rel="tooltip" data-trigger="hover" data-placement="top">some text</span>')
  525. .appendTo('#qunit-fixture')
  526. $tooltip.bootstrapTooltip({
  527. html: true,
  528. animation: false,
  529. trigger: 'hover',
  530. delay: { show: 0, hide: 500 },
  531. title: titleHtml
  532. })
  533. var obj = $tooltip.data('bs.tooltip')
  534. $('#tt-outer').trigger('mouseenter')
  535. var currentUid = $('#tt-content').text()
  536. $('#tt-outer').trigger('mouseleave')
  537. assert.strictEqual(currentUid, $('#tt-content').text())
  538. assert.ok(obj._hoverState == 'out', 'the tooltip hoverState should be set to "out"')
  539. $('#tt-outer').trigger('mouseenter')
  540. assert.ok(obj._hoverState == 'in', 'the tooltip hoverState should be set to "in"')
  541. assert.strictEqual(currentUid, $('#tt-content').text())
  542. })
  543. QUnit.test('should correctly position tooltips on transformed elements', function (assert) {
  544. var styleProps = document.documentElement.style
  545. if (!('transform' in styleProps) && !('webkitTransform' in styleProps) && !('msTransform' in styleProps)) {
  546. assert.expect(0)
  547. return
  548. }
  549. assert.expect(2)
  550. var done = assert.async()
  551. var styles = '<style>'
  552. + '#qunit-fixture { top: 0; left: 0; }'
  553. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  554. + '.tooltip { position: absolute; }'
  555. + '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
  556. + '#target { position: absolute; top: 100px; left: 50px; width: 100px; height: 200px; -webkit-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); }'
  557. + '</style>'
  558. var $styles = $(styles).appendTo('head')
  559. var $element = $('<div id="target" title="1"/>').appendTo('#qunit-fixture')
  560. $element
  561. .on('shown.bs.tooltip', function () {
  562. var offset = $('.tooltip').offset()
  563. $styles.remove()
  564. assert.ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
  565. assert.ok(Math.abs(offset.top - 126) <= 1, 'tooltip has correct vertical location')
  566. $element.bootstrapTooltip('hide')
  567. done()
  568. })
  569. .bootstrapTooltip({
  570. trigger: 'manual'
  571. })
  572. $element.bootstrapTooltip('show')
  573. })
  574. QUnit.test('should do nothing when an attempt is made to hide an uninitialized tooltip', function (assert) {
  575. assert.expect(1)
  576. var $tooltip = $('<span data-toggle="tooltip" title="some tip">some text</span>')
  577. .appendTo('#qunit-fixture')
  578. .on('hidden.bs.tooltip shown.bs.tooltip', function () {
  579. assert.ok(false, 'should not fire any tooltip events')
  580. })
  581. .bootstrapTooltip('hide')
  582. assert.strictEqual($tooltip.data('bs.tooltip'), undefined, 'should not initialize the tooltip')
  583. })
  584. QUnit.test('should not remove tooltip if multiple triggers are set and one is still active', function (assert) {
  585. assert.expect(41)
  586. var $el = $('<button>Trigger</button>')
  587. .appendTo('#qunit-fixture')
  588. .bootstrapTooltip({ trigger: 'click hover focus', animation: false })
  589. var tooltip = $el.data('bs.tooltip')
  590. var $tooltip = $(tooltip.getTipElement())
  591. function showingTooltip() { return $tooltip.hasClass('in') || tooltip._hoverState == 'in' }
  592. var tests = [
  593. ['mouseenter', 'mouseleave'],
  594. ['focusin', 'focusout'],
  595. ['click', 'click'],
  596. ['mouseenter', 'focusin', 'focusout', 'mouseleave'],
  597. ['mouseenter', 'focusin', 'mouseleave', 'focusout'],
  598. ['focusin', 'mouseenter', 'mouseleave', 'focusout'],
  599. ['focusin', 'mouseenter', 'focusout', 'mouseleave'],
  600. ['click', 'focusin', 'mouseenter', 'focusout', 'mouseleave', 'click'],
  601. ['mouseenter', 'click', 'focusin', 'focusout', 'mouseleave', 'click'],
  602. ['mouseenter', 'focusin', 'click', 'click', 'mouseleave', 'focusout']
  603. ]
  604. assert.ok(!showingTooltip())
  605. $.each(tests, function (idx, triggers) {
  606. for (var i = 0, len = triggers.length; i < len; i++) {
  607. $el.trigger(triggers[i]);
  608. assert.equal(i < (len - 1), showingTooltip())
  609. }
  610. })
  611. })
  612. })