123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422 |
- import Tooltip from '../../src/tooltip'
- import EventHandler from '../../src/dom/event-handler'
- import { noop } from '../../src/util/index'
- import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
- describe('Tooltip', () => {
- let fixtureEl
- beforeAll(() => {
- fixtureEl = getFixture()
- })
- afterEach(() => {
- clearFixture()
- document.querySelectorAll('.tooltip').forEach(tooltipEl => {
- document.body.removeChild(tooltipEl)
- })
- })
- describe('VERSION', () => {
- it('should return plugin version', () => {
- expect(Tooltip.VERSION).toEqual(jasmine.any(String))
- })
- })
- describe('Default', () => {
- it('should return plugin default config', () => {
- expect(Tooltip.Default).toEqual(jasmine.any(Object))
- })
- })
- describe('NAME', () => {
- it('should return plugin name', () => {
- expect(Tooltip.NAME).toEqual(jasmine.any(String))
- })
- })
- describe('DATA_KEY', () => {
- it('should return plugin data key', () => {
- expect(Tooltip.DATA_KEY).toEqual('bs.tooltip')
- })
- })
- describe('Event', () => {
- it('should return plugin events', () => {
- expect(Tooltip.Event).toEqual(jasmine.any(Object))
- })
- })
- describe('EVENT_KEY', () => {
- it('should return plugin event key', () => {
- expect(Tooltip.EVENT_KEY).toEqual('.bs.tooltip')
- })
- })
- describe('DefaultType', () => {
- it('should return plugin default type', () => {
- expect(Tooltip.DefaultType).toEqual(jasmine.any(Object))
- })
- })
- describe('constructor', () => {
- it('should take care of element either passed as a CSS selector or DOM element', () => {
- fixtureEl.innerHTML = '<a href="#" id="tooltipEl" rel="tooltip" title="Nice and short title">'
- const tooltipEl = fixtureEl.querySelector('#tooltipEl')
- const tooltipBySelector = new Tooltip('#tooltipEl')
- const tooltipByElement = new Tooltip(tooltipEl)
- expect(tooltipBySelector._element).toEqual(tooltipEl)
- expect(tooltipByElement._element).toEqual(tooltipEl)
- })
- it('should not take care of disallowed data attributes', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-bs-sanitize="false" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- expect(tooltip._config.sanitize).toEqual(true)
- })
- it('should convert title and content to string if numbers', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- title: 1,
- content: 7
- })
- expect(tooltip._config.title).toEqual('1')
- expect(tooltip._config.content).toEqual('7')
- })
- it('should enable selector delegation', done => {
- fixtureEl.innerHTML = '<div></div>'
- const containerEl = fixtureEl.querySelector('div')
- const tooltipContainer = new Tooltip(containerEl, {
- selector: 'a[rel="tooltip"]',
- trigger: 'click'
- })
- containerEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipInContainerEl = containerEl.querySelector('a')
- tooltipInContainerEl.addEventListener('shown.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- tooltipContainer.dispose()
- done()
- })
- tooltipInContainerEl.click()
- })
- it('should create offset modifier when offset is passed as a function', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Offset from function">'
- const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- offset: getOffset,
- popperConfig: {
- onFirstUpdate: state => {
- expect(getOffset).toHaveBeenCalledWith({
- popper: state.rects.popper,
- reference: state.rects.reference,
- placement: state.placement
- }, tooltipEl)
- done()
- }
- }
- })
- const offset = tooltip._getOffset()
- expect(typeof offset).toEqual('function')
- tooltip.show()
- })
- it('should create offset modifier when offset option is passed in data attribute', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-bs-offset="10,20" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- expect(tooltip._getOffset()).toEqual([10, 20])
- })
- it('should allow to pass config to Popper with `popperConfig`', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- popperConfig: {
- placement: 'left'
- }
- })
- const popperConfig = tooltip._getPopperConfig('top')
- expect(popperConfig.placement).toEqual('left')
- })
- it('should allow to pass config to Popper with `popperConfig` as a function', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const getPopperConfig = jasmine.createSpy('getPopperConfig').and.returnValue({ placement: 'left' })
- const tooltip = new Tooltip(tooltipEl, {
- popperConfig: getPopperConfig
- })
- const popperConfig = tooltip._getPopperConfig('top')
- expect(getPopperConfig).toHaveBeenCalled()
- expect(popperConfig.placement).toEqual('left')
- })
- })
- describe('enable', () => {
- it('should enable a tooltip', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.enable()
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- done()
- })
- tooltip.show()
- })
- })
- describe('disable', () => {
- it('should disable tooltip', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.disable()
- tooltipEl.addEventListener('show.bs.tooltip', () => {
- throw new Error('should not show a disabled tooltip')
- })
- tooltip.show()
- setTimeout(() => {
- expect().nothing()
- done()
- }, 10)
- })
- })
- describe('toggleEnabled', () => {
- it('should toggle enabled', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- expect(tooltip._isEnabled).toEqual(true)
- tooltip.toggleEnabled()
- expect(tooltip._isEnabled).toEqual(false)
- })
- })
- describe('toggle', () => {
- it('should do nothing if disabled', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.disable()
- tooltipEl.addEventListener('show.bs.tooltip', () => {
- throw new Error('should not show a disabled tooltip')
- })
- tooltip.toggle()
- setTimeout(() => {
- expect().nothing()
- done()
- }, 10)
- })
- it('should show a tooltip', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- done()
- })
- tooltip.toggle()
- })
- it('should call toggle and show the tooltip when trigger is "click"', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- trigger: 'click'
- })
- spyOn(tooltip, 'toggle').and.callThrough()
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(tooltip.toggle).toHaveBeenCalled()
- done()
- })
- tooltipEl.click()
- })
- it('should hide a tooltip', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- tooltip.toggle()
- })
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).toBeNull()
- done()
- })
- tooltip.toggle()
- })
- it('should call toggle and hide the tooltip when trigger is "click"', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- trigger: 'click'
- })
- spyOn(tooltip, 'toggle').and.callThrough()
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- tooltipEl.click()
- })
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- expect(tooltip.toggle).toHaveBeenCalled()
- done()
- })
- tooltipEl.click()
- })
- })
- describe('dispose', () => {
- it('should destroy a tooltip', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const addEventSpy = spyOn(tooltipEl, 'addEventListener').and.callThrough()
- const removeEventSpy = spyOn(tooltipEl, 'removeEventListener').and.callThrough()
- const tooltip = new Tooltip(tooltipEl)
- expect(Tooltip.getInstance(tooltipEl)).toEqual(tooltip)
- const expectedArgs = [
- ['mouseover', jasmine.any(Function), jasmine.any(Boolean)],
- ['mouseout', jasmine.any(Function), jasmine.any(Boolean)],
- ['focusin', jasmine.any(Function), jasmine.any(Boolean)],
- ['focusout', jasmine.any(Function), jasmine.any(Boolean)]
- ]
- expect(addEventSpy.calls.allArgs()).toEqual(expectedArgs)
- tooltip.dispose()
- expect(Tooltip.getInstance(tooltipEl)).toEqual(null)
- expect(removeEventSpy.calls.allArgs()).toEqual(expectedArgs)
- })
- it('should destroy a tooltip after it is shown and hidden', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- tooltip.hide()
- })
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- tooltip.dispose()
- expect(tooltip.tip).toEqual(null)
- expect(Tooltip.getInstance(tooltipEl)).toEqual(null)
- done()
- })
- tooltip.show()
- })
- it('should destroy a tooltip and remove it from the dom', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- tooltip.dispose()
- expect(document.querySelector('.tooltip')).toBeNull()
- done()
- })
- tooltip.show()
- })
- })
- describe('show', () => {
- it('should show a tooltip', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tooltipShown = document.querySelector('.tooltip')
- expect(tooltipShown).not.toBeNull()
- expect(tooltipEl.getAttribute('aria-describedby')).toEqual(tooltipShown.getAttribute('id'))
- expect(tooltipShown.getAttribute('id')).toContain('tooltip')
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip when hovering a children element', done => {
- fixtureEl.innerHTML =
- '<a href="#" rel="tooltip" title="Another tooltip">' +
- '<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">' +
- '<rect width="100%" fill="#563d7c"/>' +
- '<circle cx="50" cy="50" r="30" fill="#fff"/>' +
- '</svg>' +
- '</a>'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- spyOn(tooltip, 'show')
- tooltipEl.querySelector('rect').dispatchEvent(createEvent('mouseover', { bubbles: true }))
- setTimeout(() => {
- expect(tooltip.show).toHaveBeenCalled()
- done()
- }, 0)
- })
- it('should show a tooltip on mobile', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- document.documentElement.ontouchstart = noop
- spyOn(EventHandler, 'on').and.callThrough()
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- expect(EventHandler.on).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
- document.documentElement.ontouchstart = undefined
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip relative to placement option', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- placement: 'bottom'
- })
- tooltipEl.addEventListener('inserted.bs.tooltip', () => {
- expect(tooltip.getTipElement().classList.contains('bs-tooltip-bottom')).toEqual(true)
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tooltipShown = document.querySelector('.tooltip')
- expect(tooltipShown.classList.contains('bs-tooltip-bottom')).toEqual(true)
- done()
- })
- tooltip.show()
- })
- it('should not error when trying to show a tooltip that has been removed from the dom', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- const firstCallback = () => {
- tooltipEl.removeEventListener('shown.bs.tooltip', firstCallback)
- let tooltipShown = document.querySelector('.tooltip')
- tooltipShown.parentNode.removeChild(tooltipShown)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- tooltipShown = document.querySelector('.tooltip')
- expect(tooltipShown).not.toBeNull()
- done()
- })
- tooltip.show()
- }
- tooltipEl.addEventListener('shown.bs.tooltip', firstCallback)
- tooltip.show()
- })
- it('should show a tooltip with a dom element container', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- container: fixtureEl
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip with a jquery element container', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- container: {
- 0: fixtureEl,
- jquery: 'jQuery'
- }
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip with a selector in container', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- container: '#fixture'
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip with placement as a function', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const spy = jasmine.createSpy('placement').and.returnValue('top')
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- placement: spy
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- expect(spy).toHaveBeenCalled()
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip without the animation', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- animation: false
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tip = document.querySelector('.tooltip')
- expect(tip).not.toBeNull()
- expect(tip.classList.contains('fade')).toEqual(false)
- done()
- })
- tooltip.show()
- })
- it('should throw an error the element is not visible', () => {
- fixtureEl.innerHTML = '<a href="#" style="display: none" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- try {
- tooltip.show()
- } catch (error) {
- expect(error.message).toEqual('Please use show on visible elements')
- }
- })
- it('should not show a tooltip if show.bs.tooltip is prevented', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- const expectedDone = () => {
- setTimeout(() => {
- expect(document.querySelector('.tooltip')).toBeNull()
- done()
- }, 10)
- }
- tooltipEl.addEventListener('show.bs.tooltip', ev => {
- ev.preventDefault()
- expectedDone()
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- throw new Error('Tooltip should not be shown')
- })
- tooltip.show()
- })
- it('should show tooltip if leave event hasn\'t occurred before delay expires', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- delay: 150
- })
- spyOn(tooltip, 'show')
- setTimeout(() => {
- expect(tooltip.show).not.toHaveBeenCalled()
- }, 100)
- setTimeout(() => {
- expect(tooltip.show).toHaveBeenCalled()
- done()
- }, 200)
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- })
- it('should not show tooltip if leave event occurs before delay expires', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- delay: 150
- })
- spyOn(tooltip, 'show')
- setTimeout(() => {
- expect(tooltip.show).not.toHaveBeenCalled()
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- }, 100)
- setTimeout(() => {
- expect(tooltip.show).toHaveBeenCalled()
- expect(document.querySelectorAll('.tooltip').length).toEqual(0)
- done()
- }, 200)
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- })
- it('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- delay: {
- show: 0,
- hide: 150
- }
- })
- setTimeout(() => {
- expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
- tooltipEl.dispatchEvent(createEvent('mouseout'))
- setTimeout(() => {
- expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- }, 100)
- setTimeout(() => {
- expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
- done()
- }, 200)
- }, 0)
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- })
- it('should not hide tooltip if leave event occurs and interaction remains inside trigger', done => {
- fixtureEl.innerHTML = [
- '<a href="#" rel="tooltip" title="Another tooltip">',
- '<b>Trigger</b>',
- 'the tooltip',
- '</a>'
- ]
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- const triggerChild = tooltipEl.querySelector('b')
- spyOn(tooltip, 'hide').and.callThrough()
- tooltipEl.addEventListener('mouseover', () => {
- const moveMouseToChildEvent = createEvent('mouseout')
- Object.defineProperty(moveMouseToChildEvent, 'relatedTarget', {
- value: triggerChild
- })
- tooltipEl.dispatchEvent(moveMouseToChildEvent)
- })
- tooltipEl.addEventListener('mouseout', () => {
- expect(tooltip.hide).not.toHaveBeenCalled()
- done()
- })
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- })
- it('should properly maintain tooltip state if leave event occurs and enter event occurs during hide transition', done => {
-
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-placement="top" style="position:fixed;left:50%;top:50%;">Trigger</a>'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- spyOn(window, 'getComputedStyle').and.returnValue({
- transitionDuration: '0.15s',
- transitionDelay: '0s'
- })
- setTimeout(() => {
- expect(tooltip._popper).not.toBeNull()
- expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toBe('top')
- tooltipEl.dispatchEvent(createEvent('mouseout'))
- setTimeout(() => {
- expect(tooltip.getTipElement().classList.contains('show')).toEqual(false)
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- }, 100)
- setTimeout(() => {
- expect(tooltip._popper).not.toBeNull()
- expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toBe('top')
- done()
- }, 200)
- }, 0)
- tooltipEl.dispatchEvent(createEvent('mouseover'))
- })
- it('should only trigger inserted event if a new tooltip element was created', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- spyOn(window, 'getComputedStyle').and.returnValue({
- transitionDuration: '0.15s',
- transitionDelay: '0s'
- })
- const insertedFunc = jasmine.createSpy()
- tooltipEl.addEventListener('inserted.bs.tooltip', insertedFunc)
- setTimeout(() => {
- expect(insertedFunc).toHaveBeenCalledTimes(1)
- tooltip.hide()
- setTimeout(() => {
- tooltip.show()
- }, 100)
- setTimeout(() => {
- expect(insertedFunc).toHaveBeenCalledTimes(1)
- done()
- }, 200)
- }, 0)
- tooltip.show()
- })
- it('should show a tooltip with custom class provided in data attributes', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-custom-class="custom-class">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tip = document.querySelector('.tooltip')
- expect(tip).not.toBeNull()
- expect(tip.classList.contains('custom-class')).toBeTrue()
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip with custom class provided as a string in config', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- customClass: 'custom-class custom-class-2'
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tip = document.querySelector('.tooltip')
- expect(tip).not.toBeNull()
- expect(tip.classList.contains('custom-class')).toBeTrue()
- expect(tip.classList.contains('custom-class-2')).toBeTrue()
- done()
- })
- tooltip.show()
- })
- it('should show a tooltip with custom class provided as a function in config', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const spy = jasmine.createSpy('customClass').and.returnValue('custom-class')
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- customClass: spy
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tip = document.querySelector('.tooltip')
- expect(tip).not.toBeNull()
- expect(spy).toHaveBeenCalled()
- expect(tip.classList.contains('custom-class')).toBeTrue()
- done()
- })
- tooltip.show()
- })
- })
- describe('hide', () => {
- it('should hide a tooltip', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).toBeNull()
- expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
- done()
- })
- tooltip.show()
- })
- it('should hide a tooltip on mobile', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- document.documentElement.ontouchstart = noop
- spyOn(EventHandler, 'off')
- tooltip.hide()
- })
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).toBeNull()
- expect(EventHandler.off).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
- document.documentElement.ontouchstart = undefined
- done()
- })
- tooltip.show()
- })
- it('should hide a tooltip without animation', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- animation: false
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- expect(document.querySelector('.tooltip')).toBeNull()
- expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
- done()
- })
- tooltip.show()
- })
- it('should not hide a tooltip if hide event is prevented', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const assertDone = () => {
- setTimeout(() => {
- expect(document.querySelector('.tooltip')).not.toBeNull()
- done()
- }, 20)
- }
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- animation: false
- })
- tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
- tooltipEl.addEventListener('hide.bs.tooltip', event => {
- event.preventDefault()
- assertDone()
- })
- tooltipEl.addEventListener('hidden.bs.tooltip', () => {
- throw new Error('should not trigger hidden event')
- })
- tooltip.show()
- })
- it('should not throw error running hide if popper hasn\'t been shown', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(div)
- try {
- tooltip.hide()
- expect().nothing()
- } catch {
- throw new Error('should not throw error')
- }
- })
- })
- describe('update', () => {
- it('should call popper update', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- spyOn(tooltip._popper, 'update')
- tooltip.update()
- expect(tooltip._popper.update).toHaveBeenCalled()
- done()
- })
- tooltip.show()
- })
- it('should do nothing if the tooltip is not shown', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.update()
- expect().nothing()
- })
- })
- describe('isWithContent', () => {
- it('should return true if there is content', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- expect(tooltip.isWithContent()).toEqual(true)
- })
- it('should return false if there is no content', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- expect(tooltip.isWithContent()).toEqual(false)
- })
- })
- describe('getTipElement', () => {
- it('should create the tip element and return it', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- spyOn(document, 'createElement').and.callThrough()
- expect(tooltip.getTipElement()).toBeDefined()
- expect(document.createElement).toHaveBeenCalled()
- })
- it('should return the created tip element', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- const spy = spyOn(document, 'createElement').and.callThrough()
- expect(tooltip.getTipElement()).toBeDefined()
- expect(spy).toHaveBeenCalled()
- spy.calls.reset()
- expect(tooltip.getTipElement()).toBeDefined()
- expect(spy).not.toHaveBeenCalled()
- })
- })
- describe('setContent', () => {
- it('should set tip content', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.setContent()
- const tip = tooltip.getTipElement()
- expect(tip.classList.contains('show')).toEqual(false)
- expect(tip.classList.contains('fade')).toEqual(false)
- expect(tip.querySelector('.tooltip-inner').textContent).toEqual('Another tooltip')
- })
- })
- describe('updateAttachment', () => {
- it('should use end class name when right placement specified', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- placement: 'right'
- })
- tooltipEl.addEventListener('inserted.bs.tooltip', () => {
- expect(tooltip.getTipElement().classList.contains('bs-tooltip-end')).toEqual(true)
- done()
- })
- tooltip.show()
- })
- it('should use start class name when left placement specified', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- placement: 'left'
- })
- tooltipEl.addEventListener('inserted.bs.tooltip', () => {
- expect(tooltip.getTipElement().classList.contains('bs-tooltip-start')).toEqual(true)
- done()
- })
- tooltip.show()
- })
- })
- describe('setElementContent', () => {
- it('should do nothing if the element is null', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.setElementContent(null, null)
- expect().nothing()
- })
- it('should add the content as a child of the element', () => {
- fixtureEl.innerHTML = [
- '<a href="#" rel="tooltip" title="Another tooltip">',
- '<div id="childContent"></div>'
- ].join('')
- const tooltipEl = fixtureEl.querySelector('a')
- const childContent = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(tooltipEl, {
- html: true
- })
- tooltip.setElementContent(tooltip.getTipElement(), childContent)
- expect(childContent.parentNode).toEqual(tooltip.getTipElement())
- })
- it('should do nothing if the content is a child of the element', () => {
- fixtureEl.innerHTML = [
- '<a href="#" rel="tooltip" title="Another tooltip">',
- '<div id="childContent"></div>'
- ].join('')
- const tooltipEl = fixtureEl.querySelector('a')
- const childContent = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(tooltipEl, {
- html: true
- })
- tooltip.getTipElement().appendChild(childContent)
- tooltip.setElementContent(tooltip.getTipElement(), childContent)
- expect().nothing()
- })
- it('should add the content as a child of the element for jQuery elements', () => {
- fixtureEl.innerHTML = [
- '<a href="#" rel="tooltip" title="Another tooltip">',
- '<div id="childContent"></div>'
- ].join('')
- const tooltipEl = fixtureEl.querySelector('a')
- const childContent = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(tooltipEl, {
- html: true
- })
- tooltip.setElementContent(tooltip.getTipElement(), { 0: childContent, jquery: 'jQuery' })
- expect(childContent.parentNode).toEqual(tooltip.getTipElement())
- })
- it('should add the child text content in the element', () => {
- fixtureEl.innerHTML = [
- '<a href="#" rel="tooltip" title="Another tooltip">',
- '<div id="childContent">Tooltip</div>'
- ].join('')
- const tooltipEl = fixtureEl.querySelector('a')
- const childContent = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.setElementContent(tooltip.getTipElement(), childContent)
- expect(childContent.textContent).toEqual(tooltip.getTipElement().textContent)
- })
- it('should add html without sanitize it', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- sanitize: false,
- html: true
- })
- tooltip.setElementContent(tooltip.getTipElement(), '<div id="childContent">Tooltip</div>')
- expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent')
- })
- it('should add html sanitized', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- html: true
- })
- tooltip.setElementContent(tooltip.getTipElement(), [
- '<div id="childContent">',
- ' <button type="button">test btn</button>',
- '</div>'
- ].join(''))
- expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent')
- expect(tooltip.getTipElement().querySelector('button')).toEqual(null)
- })
- it('should add text content', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltip.setElementContent(tooltip.getTipElement(), 'test')
- expect(tooltip.getTipElement().textContent).toEqual('test')
- })
- })
- describe('getTitle', () => {
- it('should return the title', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- expect(tooltip.getTitle()).toEqual('Another tooltip')
- })
- it('should call title function', () => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip"></a>'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl, {
- title: () => 'test'
- })
- expect(tooltip.getTitle()).toEqual('test')
- })
- })
- describe('getInstance', () => {
- it('should return tooltip instance', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const alert = new Tooltip(div)
- expect(Tooltip.getInstance(div)).toEqual(alert)
- expect(Tooltip.getInstance(div)).toBeInstanceOf(Tooltip)
- })
- it('should return null when there is no tooltip instance', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- expect(Tooltip.getInstance(div)).toEqual(null)
- })
- })
- describe('aria-label', () => {
- it('should add the aria-label attribute for referencing original title', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tooltipShown = document.querySelector('.tooltip')
- expect(tooltipShown).not.toBeNull()
- expect(tooltipEl.getAttribute('aria-label')).toEqual('Another tooltip')
- done()
- })
- tooltip.show()
- })
- it('should not add the aria-label attribute if the attribute already exists', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" aria-label="Different label" title="Another tooltip"></a>'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tooltipShown = document.querySelector('.tooltip')
- expect(tooltipShown).not.toBeNull()
- expect(tooltipEl.getAttribute('aria-label')).toEqual('Different label')
- done()
- })
- tooltip.show()
- })
- it('should not add the aria-label attribute if the element has text content', done => {
- fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">text content</a>'
- const tooltipEl = fixtureEl.querySelector('a')
- const tooltip = new Tooltip(tooltipEl)
- tooltipEl.addEventListener('shown.bs.tooltip', () => {
- const tooltipShown = document.querySelector('.tooltip')
- expect(tooltipShown).not.toBeNull()
- expect(tooltipEl.getAttribute('aria-label')).toBeNull()
- done()
- })
- tooltip.show()
- })
- })
- describe('getOrCreateInstance', () => {
- it('should return tooltip instance', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(div)
- expect(Tooltip.getOrCreateInstance(div)).toEqual(tooltip)
- expect(Tooltip.getInstance(div)).toEqual(Tooltip.getOrCreateInstance(div, {}))
- expect(Tooltip.getOrCreateInstance(div)).toBeInstanceOf(Tooltip)
- })
- it('should return new instance when there is no tooltip instance', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- expect(Tooltip.getInstance(div)).toEqual(null)
- expect(Tooltip.getOrCreateInstance(div)).toBeInstanceOf(Tooltip)
- })
- it('should return new instance when there is no tooltip instance with given configuration', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- expect(Tooltip.getInstance(div)).toEqual(null)
- const tooltip = Tooltip.getOrCreateInstance(div, {
- title: () => 'test'
- })
- expect(tooltip).toBeInstanceOf(Tooltip)
- expect(tooltip.getTitle()).toEqual('test')
- })
- it('should return the instance when exists without given configuration', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(div, {
- title: () => 'nothing'
- })
- expect(Tooltip.getInstance(div)).toEqual(tooltip)
- const tooltip2 = Tooltip.getOrCreateInstance(div, {
- title: () => 'test'
- })
- expect(tooltip).toBeInstanceOf(Tooltip)
- expect(tooltip2).toEqual(tooltip)
- expect(tooltip2.getTitle()).toEqual('nothing')
- })
- })
- describe('jQueryInterface', () => {
- it('should create a tooltip', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- jQueryMock.fn.tooltip = Tooltip.jQueryInterface
- jQueryMock.elements = [div]
- jQueryMock.fn.tooltip.call(jQueryMock)
- expect(Tooltip.getInstance(div)).not.toBeNull()
- })
- it('should not re create a tooltip', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(div)
- jQueryMock.fn.tooltip = Tooltip.jQueryInterface
- jQueryMock.elements = [div]
- jQueryMock.fn.tooltip.call(jQueryMock)
- expect(Tooltip.getInstance(div)).toEqual(tooltip)
- })
- it('should call a tooltip method', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const tooltip = new Tooltip(div)
- spyOn(tooltip, 'show')
- jQueryMock.fn.tooltip = Tooltip.jQueryInterface
- jQueryMock.elements = [div]
- jQueryMock.fn.tooltip.call(jQueryMock, 'show')
- expect(Tooltip.getInstance(div)).toEqual(tooltip)
- expect(tooltip.show).toHaveBeenCalled()
- })
- it('should throw error on undefined method', () => {
- fixtureEl.innerHTML = '<div></div>'
- const div = fixtureEl.querySelector('div')
- const action = 'undefinedMethod'
- jQueryMock.fn.tooltip = Tooltip.jQueryInterface
- jQueryMock.elements = [div]
- expect(() => {
- jQueryMock.fn.tooltip.call(jQueryMock, action)
- }).toThrowError(TypeError, `No method named "${action}"`)
- })
- })
- })
|