Skip to content

Commit

Permalink
only trigger tooltip inserted event on true dom insert
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanBerliner authored and XhmikosR committed Mar 16, 2021
1 parent 34620ce commit 0bd32a3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
3 changes: 1 addition & 2 deletions js/src/tooltip.js
Expand Up @@ -279,10 +279,9 @@ class Tooltip extends BaseComponent {

if (!this._element.ownerDocument.documentElement.contains(this.tip)) {
container.appendChild(tip)
EventHandler.trigger(this._element, this.constructor.Event.INSERTED)
}

EventHandler.trigger(this._element, this.constructor.Event.INSERTED)

if (this._popper) {
this._popper.update()
} else {
Expand Down
62 changes: 62 additions & 0 deletions js/tests/unit/tooltip.spec.js
Expand Up @@ -708,6 +708,37 @@ describe('Tooltip', () => {
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 => {
// Style this tooltip to give it plenty of room for popper to do what it wants
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-placement="top" style="position:fixed;left:50%;top:50%;">Trigger</a>'
Expand Down Expand Up @@ -740,6 +771,37 @@ describe('Tooltip', () => {
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">'

Expand Down

0 comments on commit 0bd32a3

Please sign in to comment.