tooltip.spec.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. import Tooltip from '../../src/tooltip'
  2. import EventHandler from '../../src/dom/event-handler'
  3. import { noop } from '../../src/util/index'
  4. /** Test helpers */
  5. import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
  6. describe('Tooltip', () => {
  7. let fixtureEl
  8. beforeAll(() => {
  9. fixtureEl = getFixture()
  10. })
  11. afterEach(() => {
  12. clearFixture()
  13. document.querySelectorAll('.tooltip').forEach(tooltipEl => {
  14. document.body.removeChild(tooltipEl)
  15. })
  16. })
  17. describe('VERSION', () => {
  18. it('should return plugin version', () => {
  19. expect(Tooltip.VERSION).toEqual(jasmine.any(String))
  20. })
  21. })
  22. describe('Default', () => {
  23. it('should return plugin default config', () => {
  24. expect(Tooltip.Default).toEqual(jasmine.any(Object))
  25. })
  26. })
  27. describe('NAME', () => {
  28. it('should return plugin name', () => {
  29. expect(Tooltip.NAME).toEqual(jasmine.any(String))
  30. })
  31. })
  32. describe('DATA_KEY', () => {
  33. it('should return plugin data key', () => {
  34. expect(Tooltip.DATA_KEY).toEqual('bs.tooltip')
  35. })
  36. })
  37. describe('Event', () => {
  38. it('should return plugin events', () => {
  39. expect(Tooltip.Event).toEqual(jasmine.any(Object))
  40. })
  41. })
  42. describe('EVENT_KEY', () => {
  43. it('should return plugin event key', () => {
  44. expect(Tooltip.EVENT_KEY).toEqual('.bs.tooltip')
  45. })
  46. })
  47. describe('DefaultType', () => {
  48. it('should return plugin default type', () => {
  49. expect(Tooltip.DefaultType).toEqual(jasmine.any(Object))
  50. })
  51. })
  52. describe('constructor', () => {
  53. it('should take care of element either passed as a CSS selector or DOM element', () => {
  54. fixtureEl.innerHTML = '<a href="#" id="tooltipEl" rel="tooltip" title="Nice and short title">'
  55. const tooltipEl = fixtureEl.querySelector('#tooltipEl')
  56. const tooltipBySelector = new Tooltip('#tooltipEl')
  57. const tooltipByElement = new Tooltip(tooltipEl)
  58. expect(tooltipBySelector._element).toEqual(tooltipEl)
  59. expect(tooltipByElement._element).toEqual(tooltipEl)
  60. })
  61. it('should not take care of disallowed data attributes', () => {
  62. fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-bs-sanitize="false" title="Another tooltip">'
  63. const tooltipEl = fixtureEl.querySelector('a')
  64. const tooltip = new Tooltip(tooltipEl)
  65. expect(tooltip._config.sanitize).toEqual(true)
  66. })
  67. it('should convert title and content to string if numbers', () => {
  68. fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
  69. const tooltipEl = fixtureEl.querySelector('a')
  70. const tooltip = new Tooltip(tooltipEl, {
  71. title: 1,
  72. content: 7
  73. })
  74. expect(tooltip._config.title).toEqual('1')
  75. expect(tooltip._config.content).toEqual('7')
  76. })
  77. it('should enable selector delegation', done => {
  78. fixtureEl.innerHTML = '<div></div>'
  79. const containerEl = fixtureEl.querySelector('div')
  80. const tooltipContainer = new Tooltip(containerEl, {
  81. selector: 'a[rel="tooltip"]',
  82. trigger: 'click'
  83. })
  84. containerEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  85. const tooltipInContainerEl = containerEl.querySelector('a')
  86. tooltipInContainerEl.addEventListener('shown.bs.tooltip', () => {
  87. expect(document.querySelector('.tooltip')).not.toBeNull()
  88. tooltipContainer.dispose()
  89. done()
  90. })
  91. tooltipInContainerEl.click()
  92. })
  93. it('should create offset modifier when offset is passed as a function', done => {
  94. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Offset from function">'
  95. const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
  96. const tooltipEl = fixtureEl.querySelector('a')
  97. const tooltip = new Tooltip(tooltipEl, {
  98. offset: getOffset,
  99. popperConfig: {
  100. onFirstUpdate: state => {
  101. expect(getOffset).toHaveBeenCalledWith({
  102. popper: state.rects.popper,
  103. reference: state.rects.reference,
  104. placement: state.placement
  105. }, tooltipEl)
  106. done()
  107. }
  108. }
  109. })
  110. const offset = tooltip._getOffset()
  111. expect(typeof offset).toEqual('function')
  112. tooltip.show()
  113. })
  114. it('should create offset modifier when offset option is passed in data attribute', () => {
  115. fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-bs-offset="10,20" title="Another tooltip">'
  116. const tooltipEl = fixtureEl.querySelector('a')
  117. const tooltip = new Tooltip(tooltipEl)
  118. expect(tooltip._getOffset()).toEqual([10, 20])
  119. })
  120. it('should allow to pass config to Popper with `popperConfig`', () => {
  121. fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
  122. const tooltipEl = fixtureEl.querySelector('a')
  123. const tooltip = new Tooltip(tooltipEl, {
  124. popperConfig: {
  125. placement: 'left'
  126. }
  127. })
  128. const popperConfig = tooltip._getPopperConfig('top')
  129. expect(popperConfig.placement).toEqual('left')
  130. })
  131. it('should allow to pass config to Popper with `popperConfig` as a function', () => {
  132. fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
  133. const tooltipEl = fixtureEl.querySelector('a')
  134. const getPopperConfig = jasmine.createSpy('getPopperConfig').and.returnValue({ placement: 'left' })
  135. const tooltip = new Tooltip(tooltipEl, {
  136. popperConfig: getPopperConfig
  137. })
  138. const popperConfig = tooltip._getPopperConfig('top')
  139. expect(getPopperConfig).toHaveBeenCalled()
  140. expect(popperConfig.placement).toEqual('left')
  141. })
  142. })
  143. describe('enable', () => {
  144. it('should enable a tooltip', done => {
  145. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  146. const tooltipEl = fixtureEl.querySelector('a')
  147. const tooltip = new Tooltip(tooltipEl)
  148. tooltip.enable()
  149. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  150. expect(document.querySelector('.tooltip')).not.toBeNull()
  151. done()
  152. })
  153. tooltip.show()
  154. })
  155. })
  156. describe('disable', () => {
  157. it('should disable tooltip', done => {
  158. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  159. const tooltipEl = fixtureEl.querySelector('a')
  160. const tooltip = new Tooltip(tooltipEl)
  161. tooltip.disable()
  162. tooltipEl.addEventListener('show.bs.tooltip', () => {
  163. throw new Error('should not show a disabled tooltip')
  164. })
  165. tooltip.show()
  166. setTimeout(() => {
  167. expect().nothing()
  168. done()
  169. }, 10)
  170. })
  171. })
  172. describe('toggleEnabled', () => {
  173. it('should toggle enabled', () => {
  174. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  175. const tooltipEl = fixtureEl.querySelector('a')
  176. const tooltip = new Tooltip(tooltipEl)
  177. expect(tooltip._isEnabled).toEqual(true)
  178. tooltip.toggleEnabled()
  179. expect(tooltip._isEnabled).toEqual(false)
  180. })
  181. })
  182. describe('toggle', () => {
  183. it('should do nothing if disabled', done => {
  184. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  185. const tooltipEl = fixtureEl.querySelector('a')
  186. const tooltip = new Tooltip(tooltipEl)
  187. tooltip.disable()
  188. tooltipEl.addEventListener('show.bs.tooltip', () => {
  189. throw new Error('should not show a disabled tooltip')
  190. })
  191. tooltip.toggle()
  192. setTimeout(() => {
  193. expect().nothing()
  194. done()
  195. }, 10)
  196. })
  197. it('should show a tooltip', done => {
  198. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  199. const tooltipEl = fixtureEl.querySelector('a')
  200. const tooltip = new Tooltip(tooltipEl)
  201. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  202. expect(document.querySelector('.tooltip')).not.toBeNull()
  203. done()
  204. })
  205. tooltip.toggle()
  206. })
  207. it('should call toggle and show the tooltip when trigger is "click"', done => {
  208. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  209. const tooltipEl = fixtureEl.querySelector('a')
  210. const tooltip = new Tooltip(tooltipEl, {
  211. trigger: 'click'
  212. })
  213. spyOn(tooltip, 'toggle').and.callThrough()
  214. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  215. expect(tooltip.toggle).toHaveBeenCalled()
  216. done()
  217. })
  218. tooltipEl.click()
  219. })
  220. it('should hide a tooltip', done => {
  221. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  222. const tooltipEl = fixtureEl.querySelector('a')
  223. const tooltip = new Tooltip(tooltipEl)
  224. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  225. tooltip.toggle()
  226. })
  227. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  228. expect(document.querySelector('.tooltip')).toBeNull()
  229. done()
  230. })
  231. tooltip.toggle()
  232. })
  233. it('should call toggle and hide the tooltip when trigger is "click"', done => {
  234. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  235. const tooltipEl = fixtureEl.querySelector('a')
  236. const tooltip = new Tooltip(tooltipEl, {
  237. trigger: 'click'
  238. })
  239. spyOn(tooltip, 'toggle').and.callThrough()
  240. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  241. tooltipEl.click()
  242. })
  243. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  244. expect(tooltip.toggle).toHaveBeenCalled()
  245. done()
  246. })
  247. tooltipEl.click()
  248. })
  249. })
  250. describe('dispose', () => {
  251. it('should destroy a tooltip', () => {
  252. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  253. const tooltipEl = fixtureEl.querySelector('a')
  254. const addEventSpy = spyOn(tooltipEl, 'addEventListener').and.callThrough()
  255. const removeEventSpy = spyOn(tooltipEl, 'removeEventListener').and.callThrough()
  256. const tooltip = new Tooltip(tooltipEl)
  257. expect(Tooltip.getInstance(tooltipEl)).toEqual(tooltip)
  258. const expectedArgs = [
  259. ['mouseover', jasmine.any(Function), jasmine.any(Boolean)],
  260. ['mouseout', jasmine.any(Function), jasmine.any(Boolean)],
  261. ['focusin', jasmine.any(Function), jasmine.any(Boolean)],
  262. ['focusout', jasmine.any(Function), jasmine.any(Boolean)]
  263. ]
  264. expect(addEventSpy.calls.allArgs()).toEqual(expectedArgs)
  265. tooltip.dispose()
  266. expect(Tooltip.getInstance(tooltipEl)).toEqual(null)
  267. expect(removeEventSpy.calls.allArgs()).toEqual(expectedArgs)
  268. })
  269. it('should destroy a tooltip after it is shown and hidden', done => {
  270. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  271. const tooltipEl = fixtureEl.querySelector('a')
  272. const tooltip = new Tooltip(tooltipEl)
  273. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  274. tooltip.hide()
  275. })
  276. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  277. tooltip.dispose()
  278. expect(tooltip.tip).toEqual(null)
  279. expect(Tooltip.getInstance(tooltipEl)).toEqual(null)
  280. done()
  281. })
  282. tooltip.show()
  283. })
  284. it('should destroy a tooltip and remove it from the dom', done => {
  285. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  286. const tooltipEl = fixtureEl.querySelector('a')
  287. const tooltip = new Tooltip(tooltipEl)
  288. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  289. expect(document.querySelector('.tooltip')).not.toBeNull()
  290. tooltip.dispose()
  291. expect(document.querySelector('.tooltip')).toBeNull()
  292. done()
  293. })
  294. tooltip.show()
  295. })
  296. })
  297. describe('show', () => {
  298. it('should show a tooltip', done => {
  299. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  300. const tooltipEl = fixtureEl.querySelector('a')
  301. const tooltip = new Tooltip(tooltipEl)
  302. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  303. const tooltipShown = document.querySelector('.tooltip')
  304. expect(tooltipShown).not.toBeNull()
  305. expect(tooltipEl.getAttribute('aria-describedby')).toEqual(tooltipShown.getAttribute('id'))
  306. expect(tooltipShown.getAttribute('id')).toContain('tooltip')
  307. done()
  308. })
  309. tooltip.show()
  310. })
  311. it('should show a tooltip when hovering a children element', done => {
  312. fixtureEl.innerHTML =
  313. '<a href="#" rel="tooltip" title="Another tooltip">' +
  314. '<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">' +
  315. '<rect width="100%" fill="#563d7c"/>' +
  316. '<circle cx="50" cy="50" r="30" fill="#fff"/>' +
  317. '</svg>' +
  318. '</a>'
  319. const tooltipEl = fixtureEl.querySelector('a')
  320. const tooltip = new Tooltip(tooltipEl)
  321. spyOn(tooltip, 'show')
  322. tooltipEl.querySelector('rect').dispatchEvent(createEvent('mouseover', { bubbles: true }))
  323. setTimeout(() => {
  324. expect(tooltip.show).toHaveBeenCalled()
  325. done()
  326. }, 0)
  327. })
  328. it('should show a tooltip on mobile', done => {
  329. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  330. const tooltipEl = fixtureEl.querySelector('a')
  331. const tooltip = new Tooltip(tooltipEl)
  332. document.documentElement.ontouchstart = noop
  333. spyOn(EventHandler, 'on').and.callThrough()
  334. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  335. expect(document.querySelector('.tooltip')).not.toBeNull()
  336. expect(EventHandler.on).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
  337. document.documentElement.ontouchstart = undefined
  338. done()
  339. })
  340. tooltip.show()
  341. })
  342. it('should show a tooltip relative to placement option', done => {
  343. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  344. const tooltipEl = fixtureEl.querySelector('a')
  345. const tooltip = new Tooltip(tooltipEl, {
  346. placement: 'bottom'
  347. })
  348. tooltipEl.addEventListener('inserted.bs.tooltip', () => {
  349. expect(tooltip.getTipElement().classList.contains('bs-tooltip-bottom')).toEqual(true)
  350. })
  351. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  352. const tooltipShown = document.querySelector('.tooltip')
  353. expect(tooltipShown.classList.contains('bs-tooltip-bottom')).toEqual(true)
  354. done()
  355. })
  356. tooltip.show()
  357. })
  358. it('should not error when trying to show a tooltip that has been removed from the dom', done => {
  359. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  360. const tooltipEl = fixtureEl.querySelector('a')
  361. const tooltip = new Tooltip(tooltipEl)
  362. const firstCallback = () => {
  363. tooltipEl.removeEventListener('shown.bs.tooltip', firstCallback)
  364. let tooltipShown = document.querySelector('.tooltip')
  365. tooltipShown.parentNode.removeChild(tooltipShown)
  366. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  367. tooltipShown = document.querySelector('.tooltip')
  368. expect(tooltipShown).not.toBeNull()
  369. done()
  370. })
  371. tooltip.show()
  372. }
  373. tooltipEl.addEventListener('shown.bs.tooltip', firstCallback)
  374. tooltip.show()
  375. })
  376. it('should show a tooltip with a dom element container', done => {
  377. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  378. const tooltipEl = fixtureEl.querySelector('a')
  379. const tooltip = new Tooltip(tooltipEl, {
  380. container: fixtureEl
  381. })
  382. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  383. expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
  384. done()
  385. })
  386. tooltip.show()
  387. })
  388. it('should show a tooltip with a jquery element container', done => {
  389. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  390. const tooltipEl = fixtureEl.querySelector('a')
  391. const tooltip = new Tooltip(tooltipEl, {
  392. container: {
  393. 0: fixtureEl,
  394. jquery: 'jQuery'
  395. }
  396. })
  397. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  398. expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
  399. done()
  400. })
  401. tooltip.show()
  402. })
  403. it('should show a tooltip with a selector in container', done => {
  404. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  405. const tooltipEl = fixtureEl.querySelector('a')
  406. const tooltip = new Tooltip(tooltipEl, {
  407. container: '#fixture'
  408. })
  409. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  410. expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
  411. done()
  412. })
  413. tooltip.show()
  414. })
  415. it('should show a tooltip with placement as a function', done => {
  416. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  417. const spy = jasmine.createSpy('placement').and.returnValue('top')
  418. const tooltipEl = fixtureEl.querySelector('a')
  419. const tooltip = new Tooltip(tooltipEl, {
  420. placement: spy
  421. })
  422. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  423. expect(document.querySelector('.tooltip')).not.toBeNull()
  424. expect(spy).toHaveBeenCalled()
  425. done()
  426. })
  427. tooltip.show()
  428. })
  429. it('should show a tooltip without the animation', done => {
  430. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  431. const tooltipEl = fixtureEl.querySelector('a')
  432. const tooltip = new Tooltip(tooltipEl, {
  433. animation: false
  434. })
  435. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  436. const tip = document.querySelector('.tooltip')
  437. expect(tip).not.toBeNull()
  438. expect(tip.classList.contains('fade')).toEqual(false)
  439. done()
  440. })
  441. tooltip.show()
  442. })
  443. it('should throw an error the element is not visible', () => {
  444. fixtureEl.innerHTML = '<a href="#" style="display: none" rel="tooltip" title="Another tooltip">'
  445. const tooltipEl = fixtureEl.querySelector('a')
  446. const tooltip = new Tooltip(tooltipEl)
  447. try {
  448. tooltip.show()
  449. } catch (error) {
  450. expect(error.message).toEqual('Please use show on visible elements')
  451. }
  452. })
  453. it('should not show a tooltip if show.bs.tooltip is prevented', done => {
  454. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  455. const tooltipEl = fixtureEl.querySelector('a')
  456. const tooltip = new Tooltip(tooltipEl)
  457. const expectedDone = () => {
  458. setTimeout(() => {
  459. expect(document.querySelector('.tooltip')).toBeNull()
  460. done()
  461. }, 10)
  462. }
  463. tooltipEl.addEventListener('show.bs.tooltip', ev => {
  464. ev.preventDefault()
  465. expectedDone()
  466. })
  467. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  468. throw new Error('Tooltip should not be shown')
  469. })
  470. tooltip.show()
  471. })
  472. it('should show tooltip if leave event hasn\'t occurred before delay expires', done => {
  473. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  474. const tooltipEl = fixtureEl.querySelector('a')
  475. const tooltip = new Tooltip(tooltipEl, {
  476. delay: 150
  477. })
  478. spyOn(tooltip, 'show')
  479. setTimeout(() => {
  480. expect(tooltip.show).not.toHaveBeenCalled()
  481. }, 100)
  482. setTimeout(() => {
  483. expect(tooltip.show).toHaveBeenCalled()
  484. done()
  485. }, 200)
  486. tooltipEl.dispatchEvent(createEvent('mouseover'))
  487. })
  488. it('should not show tooltip if leave event occurs before delay expires', done => {
  489. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  490. const tooltipEl = fixtureEl.querySelector('a')
  491. const tooltip = new Tooltip(tooltipEl, {
  492. delay: 150
  493. })
  494. spyOn(tooltip, 'show')
  495. setTimeout(() => {
  496. expect(tooltip.show).not.toHaveBeenCalled()
  497. tooltipEl.dispatchEvent(createEvent('mouseover'))
  498. }, 100)
  499. setTimeout(() => {
  500. expect(tooltip.show).toHaveBeenCalled()
  501. expect(document.querySelectorAll('.tooltip').length).toEqual(0)
  502. done()
  503. }, 200)
  504. tooltipEl.dispatchEvent(createEvent('mouseover'))
  505. })
  506. it('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', done => {
  507. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  508. const tooltipEl = fixtureEl.querySelector('a')
  509. const tooltip = new Tooltip(tooltipEl, {
  510. delay: {
  511. show: 0,
  512. hide: 150
  513. }
  514. })
  515. setTimeout(() => {
  516. expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
  517. tooltipEl.dispatchEvent(createEvent('mouseout'))
  518. setTimeout(() => {
  519. expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
  520. tooltipEl.dispatchEvent(createEvent('mouseover'))
  521. }, 100)
  522. setTimeout(() => {
  523. expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
  524. done()
  525. }, 200)
  526. }, 0)
  527. tooltipEl.dispatchEvent(createEvent('mouseover'))
  528. })
  529. it('should not hide tooltip if leave event occurs and interaction remains inside trigger', done => {
  530. fixtureEl.innerHTML = [
  531. '<a href="#" rel="tooltip" title="Another tooltip">',
  532. '<b>Trigger</b>',
  533. 'the tooltip',
  534. '</a>'
  535. ]
  536. const tooltipEl = fixtureEl.querySelector('a')
  537. const tooltip = new Tooltip(tooltipEl)
  538. const triggerChild = tooltipEl.querySelector('b')
  539. spyOn(tooltip, 'hide').and.callThrough()
  540. tooltipEl.addEventListener('mouseover', () => {
  541. const moveMouseToChildEvent = createEvent('mouseout')
  542. Object.defineProperty(moveMouseToChildEvent, 'relatedTarget', {
  543. value: triggerChild
  544. })
  545. tooltipEl.dispatchEvent(moveMouseToChildEvent)
  546. })
  547. tooltipEl.addEventListener('mouseout', () => {
  548. expect(tooltip.hide).not.toHaveBeenCalled()
  549. done()
  550. })
  551. tooltipEl.dispatchEvent(createEvent('mouseover'))
  552. })
  553. it('should properly maintain tooltip state if leave event occurs and enter event occurs during hide transition', done => {
  554. // Style this tooltip to give it plenty of room for popper to do what it wants
  555. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-placement="top" style="position:fixed;left:50%;top:50%;">Trigger</a>'
  556. const tooltipEl = fixtureEl.querySelector('a')
  557. const tooltip = new Tooltip(tooltipEl)
  558. spyOn(window, 'getComputedStyle').and.returnValue({
  559. transitionDuration: '0.15s',
  560. transitionDelay: '0s'
  561. })
  562. setTimeout(() => {
  563. expect(tooltip._popper).not.toBeNull()
  564. expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toBe('top')
  565. tooltipEl.dispatchEvent(createEvent('mouseout'))
  566. setTimeout(() => {
  567. expect(tooltip.getTipElement().classList.contains('show')).toEqual(false)
  568. tooltipEl.dispatchEvent(createEvent('mouseover'))
  569. }, 100)
  570. setTimeout(() => {
  571. expect(tooltip._popper).not.toBeNull()
  572. expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toBe('top')
  573. done()
  574. }, 200)
  575. }, 0)
  576. tooltipEl.dispatchEvent(createEvent('mouseover'))
  577. })
  578. it('should only trigger inserted event if a new tooltip element was created', done => {
  579. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  580. const tooltipEl = fixtureEl.querySelector('a')
  581. const tooltip = new Tooltip(tooltipEl)
  582. spyOn(window, 'getComputedStyle').and.returnValue({
  583. transitionDuration: '0.15s',
  584. transitionDelay: '0s'
  585. })
  586. const insertedFunc = jasmine.createSpy()
  587. tooltipEl.addEventListener('inserted.bs.tooltip', insertedFunc)
  588. setTimeout(() => {
  589. expect(insertedFunc).toHaveBeenCalledTimes(1)
  590. tooltip.hide()
  591. setTimeout(() => {
  592. tooltip.show()
  593. }, 100)
  594. setTimeout(() => {
  595. expect(insertedFunc).toHaveBeenCalledTimes(1)
  596. done()
  597. }, 200)
  598. }, 0)
  599. tooltip.show()
  600. })
  601. it('should show a tooltip with custom class provided in data attributes', done => {
  602. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-custom-class="custom-class">'
  603. const tooltipEl = fixtureEl.querySelector('a')
  604. const tooltip = new Tooltip(tooltipEl)
  605. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  606. const tip = document.querySelector('.tooltip')
  607. expect(tip).not.toBeNull()
  608. expect(tip.classList.contains('custom-class')).toBeTrue()
  609. done()
  610. })
  611. tooltip.show()
  612. })
  613. it('should show a tooltip with custom class provided as a string in config', done => {
  614. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  615. const tooltipEl = fixtureEl.querySelector('a')
  616. const tooltip = new Tooltip(tooltipEl, {
  617. customClass: 'custom-class custom-class-2'
  618. })
  619. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  620. const tip = document.querySelector('.tooltip')
  621. expect(tip).not.toBeNull()
  622. expect(tip.classList.contains('custom-class')).toBeTrue()
  623. expect(tip.classList.contains('custom-class-2')).toBeTrue()
  624. done()
  625. })
  626. tooltip.show()
  627. })
  628. it('should show a tooltip with custom class provided as a function in config', done => {
  629. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  630. const spy = jasmine.createSpy('customClass').and.returnValue('custom-class')
  631. const tooltipEl = fixtureEl.querySelector('a')
  632. const tooltip = new Tooltip(tooltipEl, {
  633. customClass: spy
  634. })
  635. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  636. const tip = document.querySelector('.tooltip')
  637. expect(tip).not.toBeNull()
  638. expect(spy).toHaveBeenCalled()
  639. expect(tip.classList.contains('custom-class')).toBeTrue()
  640. done()
  641. })
  642. tooltip.show()
  643. })
  644. })
  645. describe('hide', () => {
  646. it('should hide a tooltip', done => {
  647. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  648. const tooltipEl = fixtureEl.querySelector('a')
  649. const tooltip = new Tooltip(tooltipEl)
  650. tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
  651. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  652. expect(document.querySelector('.tooltip')).toBeNull()
  653. expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
  654. done()
  655. })
  656. tooltip.show()
  657. })
  658. it('should hide a tooltip on mobile', done => {
  659. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  660. const tooltipEl = fixtureEl.querySelector('a')
  661. const tooltip = new Tooltip(tooltipEl)
  662. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  663. document.documentElement.ontouchstart = noop
  664. spyOn(EventHandler, 'off')
  665. tooltip.hide()
  666. })
  667. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  668. expect(document.querySelector('.tooltip')).toBeNull()
  669. expect(EventHandler.off).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
  670. document.documentElement.ontouchstart = undefined
  671. done()
  672. })
  673. tooltip.show()
  674. })
  675. it('should hide a tooltip without animation', done => {
  676. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  677. const tooltipEl = fixtureEl.querySelector('a')
  678. const tooltip = new Tooltip(tooltipEl, {
  679. animation: false
  680. })
  681. tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
  682. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  683. expect(document.querySelector('.tooltip')).toBeNull()
  684. expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
  685. done()
  686. })
  687. tooltip.show()
  688. })
  689. it('should not hide a tooltip if hide event is prevented', done => {
  690. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  691. const assertDone = () => {
  692. setTimeout(() => {
  693. expect(document.querySelector('.tooltip')).not.toBeNull()
  694. done()
  695. }, 20)
  696. }
  697. const tooltipEl = fixtureEl.querySelector('a')
  698. const tooltip = new Tooltip(tooltipEl, {
  699. animation: false
  700. })
  701. tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
  702. tooltipEl.addEventListener('hide.bs.tooltip', event => {
  703. event.preventDefault()
  704. assertDone()
  705. })
  706. tooltipEl.addEventListener('hidden.bs.tooltip', () => {
  707. throw new Error('should not trigger hidden event')
  708. })
  709. tooltip.show()
  710. })
  711. it('should not throw error running hide if popper hasn\'t been shown', () => {
  712. fixtureEl.innerHTML = '<div></div>'
  713. const div = fixtureEl.querySelector('div')
  714. const tooltip = new Tooltip(div)
  715. try {
  716. tooltip.hide()
  717. expect().nothing()
  718. } catch {
  719. throw new Error('should not throw error')
  720. }
  721. })
  722. })
  723. describe('update', () => {
  724. it('should call popper update', done => {
  725. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  726. const tooltipEl = fixtureEl.querySelector('a')
  727. const tooltip = new Tooltip(tooltipEl)
  728. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  729. spyOn(tooltip._popper, 'update')
  730. tooltip.update()
  731. expect(tooltip._popper.update).toHaveBeenCalled()
  732. done()
  733. })
  734. tooltip.show()
  735. })
  736. it('should do nothing if the tooltip is not shown', () => {
  737. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  738. const tooltipEl = fixtureEl.querySelector('a')
  739. const tooltip = new Tooltip(tooltipEl)
  740. tooltip.update()
  741. expect().nothing()
  742. })
  743. })
  744. describe('isWithContent', () => {
  745. it('should return true if there is content', () => {
  746. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  747. const tooltipEl = fixtureEl.querySelector('a')
  748. const tooltip = new Tooltip(tooltipEl)
  749. expect(tooltip.isWithContent()).toEqual(true)
  750. })
  751. it('should return false if there is no content', () => {
  752. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="">'
  753. const tooltipEl = fixtureEl.querySelector('a')
  754. const tooltip = new Tooltip(tooltipEl)
  755. expect(tooltip.isWithContent()).toEqual(false)
  756. })
  757. })
  758. describe('getTipElement', () => {
  759. it('should create the tip element and return it', () => {
  760. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  761. const tooltipEl = fixtureEl.querySelector('a')
  762. const tooltip = new Tooltip(tooltipEl)
  763. spyOn(document, 'createElement').and.callThrough()
  764. expect(tooltip.getTipElement()).toBeDefined()
  765. expect(document.createElement).toHaveBeenCalled()
  766. })
  767. it('should return the created tip element', () => {
  768. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  769. const tooltipEl = fixtureEl.querySelector('a')
  770. const tooltip = new Tooltip(tooltipEl)
  771. const spy = spyOn(document, 'createElement').and.callThrough()
  772. expect(tooltip.getTipElement()).toBeDefined()
  773. expect(spy).toHaveBeenCalled()
  774. spy.calls.reset()
  775. expect(tooltip.getTipElement()).toBeDefined()
  776. expect(spy).not.toHaveBeenCalled()
  777. })
  778. })
  779. describe('setContent', () => {
  780. it('should set tip content', () => {
  781. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  782. const tooltipEl = fixtureEl.querySelector('a')
  783. const tooltip = new Tooltip(tooltipEl)
  784. tooltip.setContent()
  785. const tip = tooltip.getTipElement()
  786. expect(tip.classList.contains('show')).toEqual(false)
  787. expect(tip.classList.contains('fade')).toEqual(false)
  788. expect(tip.querySelector('.tooltip-inner').textContent).toEqual('Another tooltip')
  789. })
  790. })
  791. describe('updateAttachment', () => {
  792. it('should use end class name when right placement specified', done => {
  793. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  794. const tooltipEl = fixtureEl.querySelector('a')
  795. const tooltip = new Tooltip(tooltipEl, {
  796. placement: 'right'
  797. })
  798. tooltipEl.addEventListener('inserted.bs.tooltip', () => {
  799. expect(tooltip.getTipElement().classList.contains('bs-tooltip-end')).toEqual(true)
  800. done()
  801. })
  802. tooltip.show()
  803. })
  804. it('should use start class name when left placement specified', done => {
  805. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  806. const tooltipEl = fixtureEl.querySelector('a')
  807. const tooltip = new Tooltip(tooltipEl, {
  808. placement: 'left'
  809. })
  810. tooltipEl.addEventListener('inserted.bs.tooltip', () => {
  811. expect(tooltip.getTipElement().classList.contains('bs-tooltip-start')).toEqual(true)
  812. done()
  813. })
  814. tooltip.show()
  815. })
  816. })
  817. describe('setElementContent', () => {
  818. it('should do nothing if the element is null', () => {
  819. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  820. const tooltipEl = fixtureEl.querySelector('a')
  821. const tooltip = new Tooltip(tooltipEl)
  822. tooltip.setElementContent(null, null)
  823. expect().nothing()
  824. })
  825. it('should add the content as a child of the element', () => {
  826. fixtureEl.innerHTML = [
  827. '<a href="#" rel="tooltip" title="Another tooltip">',
  828. '<div id="childContent"></div>'
  829. ].join('')
  830. const tooltipEl = fixtureEl.querySelector('a')
  831. const childContent = fixtureEl.querySelector('div')
  832. const tooltip = new Tooltip(tooltipEl, {
  833. html: true
  834. })
  835. tooltip.setElementContent(tooltip.getTipElement(), childContent)
  836. expect(childContent.parentNode).toEqual(tooltip.getTipElement())
  837. })
  838. it('should do nothing if the content is a child of the element', () => {
  839. fixtureEl.innerHTML = [
  840. '<a href="#" rel="tooltip" title="Another tooltip">',
  841. '<div id="childContent"></div>'
  842. ].join('')
  843. const tooltipEl = fixtureEl.querySelector('a')
  844. const childContent = fixtureEl.querySelector('div')
  845. const tooltip = new Tooltip(tooltipEl, {
  846. html: true
  847. })
  848. tooltip.getTipElement().appendChild(childContent)
  849. tooltip.setElementContent(tooltip.getTipElement(), childContent)
  850. expect().nothing()
  851. })
  852. it('should add the content as a child of the element for jQuery elements', () => {
  853. fixtureEl.innerHTML = [
  854. '<a href="#" rel="tooltip" title="Another tooltip">',
  855. '<div id="childContent"></div>'
  856. ].join('')
  857. const tooltipEl = fixtureEl.querySelector('a')
  858. const childContent = fixtureEl.querySelector('div')
  859. const tooltip = new Tooltip(tooltipEl, {
  860. html: true
  861. })
  862. tooltip.setElementContent(tooltip.getTipElement(), { 0: childContent, jquery: 'jQuery' })
  863. expect(childContent.parentNode).toEqual(tooltip.getTipElement())
  864. })
  865. it('should add the child text content in the element', () => {
  866. fixtureEl.innerHTML = [
  867. '<a href="#" rel="tooltip" title="Another tooltip">',
  868. '<div id="childContent">Tooltip</div>'
  869. ].join('')
  870. const tooltipEl = fixtureEl.querySelector('a')
  871. const childContent = fixtureEl.querySelector('div')
  872. const tooltip = new Tooltip(tooltipEl)
  873. tooltip.setElementContent(tooltip.getTipElement(), childContent)
  874. expect(childContent.textContent).toEqual(tooltip.getTipElement().textContent)
  875. })
  876. it('should add html without sanitize it', () => {
  877. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  878. const tooltipEl = fixtureEl.querySelector('a')
  879. const tooltip = new Tooltip(tooltipEl, {
  880. sanitize: false,
  881. html: true
  882. })
  883. tooltip.setElementContent(tooltip.getTipElement(), '<div id="childContent">Tooltip</div>')
  884. expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent')
  885. })
  886. it('should add html sanitized', () => {
  887. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  888. const tooltipEl = fixtureEl.querySelector('a')
  889. const tooltip = new Tooltip(tooltipEl, {
  890. html: true
  891. })
  892. tooltip.setElementContent(tooltip.getTipElement(), [
  893. '<div id="childContent">',
  894. ' <button type="button">test btn</button>',
  895. '</div>'
  896. ].join(''))
  897. expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent')
  898. expect(tooltip.getTipElement().querySelector('button')).toEqual(null)
  899. })
  900. it('should add text content', () => {
  901. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  902. const tooltipEl = fixtureEl.querySelector('a')
  903. const tooltip = new Tooltip(tooltipEl)
  904. tooltip.setElementContent(tooltip.getTipElement(), 'test')
  905. expect(tooltip.getTipElement().textContent).toEqual('test')
  906. })
  907. })
  908. describe('getTitle', () => {
  909. it('should return the title', () => {
  910. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
  911. const tooltipEl = fixtureEl.querySelector('a')
  912. const tooltip = new Tooltip(tooltipEl)
  913. expect(tooltip.getTitle()).toEqual('Another tooltip')
  914. })
  915. it('should call title function', () => {
  916. fixtureEl.innerHTML = '<a href="#" rel="tooltip"></a>'
  917. const tooltipEl = fixtureEl.querySelector('a')
  918. const tooltip = new Tooltip(tooltipEl, {
  919. title: () => 'test'
  920. })
  921. expect(tooltip.getTitle()).toEqual('test')
  922. })
  923. })
  924. describe('getInstance', () => {
  925. it('should return tooltip instance', () => {
  926. fixtureEl.innerHTML = '<div></div>'
  927. const div = fixtureEl.querySelector('div')
  928. const alert = new Tooltip(div)
  929. expect(Tooltip.getInstance(div)).toEqual(alert)
  930. expect(Tooltip.getInstance(div)).toBeInstanceOf(Tooltip)
  931. })
  932. it('should return null when there is no tooltip instance', () => {
  933. fixtureEl.innerHTML = '<div></div>'
  934. const div = fixtureEl.querySelector('div')
  935. expect(Tooltip.getInstance(div)).toEqual(null)
  936. })
  937. })
  938. describe('aria-label', () => {
  939. it('should add the aria-label attribute for referencing original title', done => {
  940. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
  941. const tooltipEl = fixtureEl.querySelector('a')
  942. const tooltip = new Tooltip(tooltipEl)
  943. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  944. const tooltipShown = document.querySelector('.tooltip')
  945. expect(tooltipShown).not.toBeNull()
  946. expect(tooltipEl.getAttribute('aria-label')).toEqual('Another tooltip')
  947. done()
  948. })
  949. tooltip.show()
  950. })
  951. it('should not add the aria-label attribute if the attribute already exists', done => {
  952. fixtureEl.innerHTML = '<a href="#" rel="tooltip" aria-label="Different label" title="Another tooltip"></a>'
  953. const tooltipEl = fixtureEl.querySelector('a')
  954. const tooltip = new Tooltip(tooltipEl)
  955. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  956. const tooltipShown = document.querySelector('.tooltip')
  957. expect(tooltipShown).not.toBeNull()
  958. expect(tooltipEl.getAttribute('aria-label')).toEqual('Different label')
  959. done()
  960. })
  961. tooltip.show()
  962. })
  963. it('should not add the aria-label attribute if the element has text content', done => {
  964. fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">text content</a>'
  965. const tooltipEl = fixtureEl.querySelector('a')
  966. const tooltip = new Tooltip(tooltipEl)
  967. tooltipEl.addEventListener('shown.bs.tooltip', () => {
  968. const tooltipShown = document.querySelector('.tooltip')
  969. expect(tooltipShown).not.toBeNull()
  970. expect(tooltipEl.getAttribute('aria-label')).toBeNull()
  971. done()
  972. })
  973. tooltip.show()
  974. })
  975. })
  976. describe('getOrCreateInstance', () => {
  977. it('should return tooltip instance', () => {
  978. fixtureEl.innerHTML = '<div></div>'
  979. const div = fixtureEl.querySelector('div')
  980. const tooltip = new Tooltip(div)
  981. expect(Tooltip.getOrCreateInstance(div)).toEqual(tooltip)
  982. expect(Tooltip.getInstance(div)).toEqual(Tooltip.getOrCreateInstance(div, {}))
  983. expect(Tooltip.getOrCreateInstance(div)).toBeInstanceOf(Tooltip)
  984. })
  985. it('should return new instance when there is no tooltip instance', () => {
  986. fixtureEl.innerHTML = '<div></div>'
  987. const div = fixtureEl.querySelector('div')
  988. expect(Tooltip.getInstance(div)).toEqual(null)
  989. expect(Tooltip.getOrCreateInstance(div)).toBeInstanceOf(Tooltip)
  990. })
  991. it('should return new instance when there is no tooltip instance with given configuration', () => {
  992. fixtureEl.innerHTML = '<div></div>'
  993. const div = fixtureEl.querySelector('div')
  994. expect(Tooltip.getInstance(div)).toEqual(null)
  995. const tooltip = Tooltip.getOrCreateInstance(div, {
  996. title: () => 'test'
  997. })
  998. expect(tooltip).toBeInstanceOf(Tooltip)
  999. expect(tooltip.getTitle()).toEqual('test')
  1000. })
  1001. it('should return the instance when exists without given configuration', () => {
  1002. fixtureEl.innerHTML = '<div></div>'
  1003. const div = fixtureEl.querySelector('div')
  1004. const tooltip = new Tooltip(div, {
  1005. title: () => 'nothing'
  1006. })
  1007. expect(Tooltip.getInstance(div)).toEqual(tooltip)
  1008. const tooltip2 = Tooltip.getOrCreateInstance(div, {
  1009. title: () => 'test'
  1010. })
  1011. expect(tooltip).toBeInstanceOf(Tooltip)
  1012. expect(tooltip2).toEqual(tooltip)
  1013. expect(tooltip2.getTitle()).toEqual('nothing')
  1014. })
  1015. })
  1016. describe('jQueryInterface', () => {
  1017. it('should create a tooltip', () => {
  1018. fixtureEl.innerHTML = '<div></div>'
  1019. const div = fixtureEl.querySelector('div')
  1020. jQueryMock.fn.tooltip = Tooltip.jQueryInterface
  1021. jQueryMock.elements = [div]
  1022. jQueryMock.fn.tooltip.call(jQueryMock)
  1023. expect(Tooltip.getInstance(div)).not.toBeNull()
  1024. })
  1025. it('should not re create a tooltip', () => {
  1026. fixtureEl.innerHTML = '<div></div>'
  1027. const div = fixtureEl.querySelector('div')
  1028. const tooltip = new Tooltip(div)
  1029. jQueryMock.fn.tooltip = Tooltip.jQueryInterface
  1030. jQueryMock.elements = [div]
  1031. jQueryMock.fn.tooltip.call(jQueryMock)
  1032. expect(Tooltip.getInstance(div)).toEqual(tooltip)
  1033. })
  1034. it('should call a tooltip method', () => {
  1035. fixtureEl.innerHTML = '<div></div>'
  1036. const div = fixtureEl.querySelector('div')
  1037. const tooltip = new Tooltip(div)
  1038. spyOn(tooltip, 'show')
  1039. jQueryMock.fn.tooltip = Tooltip.jQueryInterface
  1040. jQueryMock.elements = [div]
  1041. jQueryMock.fn.tooltip.call(jQueryMock, 'show')
  1042. expect(Tooltip.getInstance(div)).toEqual(tooltip)
  1043. expect(tooltip.show).toHaveBeenCalled()
  1044. })
  1045. it('should throw error on undefined method', () => {
  1046. fixtureEl.innerHTML = '<div></div>'
  1047. const div = fixtureEl.querySelector('div')
  1048. const action = 'undefinedMethod'
  1049. jQueryMock.fn.tooltip = Tooltip.jQueryInterface
  1050. jQueryMock.elements = [div]
  1051. expect(() => {
  1052. jQueryMock.fn.tooltip.call(jQueryMock, action)
  1053. }).toThrowError(TypeError, `No method named "${action}"`)
  1054. })
  1055. })
  1056. })