Skip to content

Commit

Permalink
Clear timeout before showing the toast (#31155)
Browse files Browse the repository at this point in the history
* clear timeout before showing the toast

* Add unit test

* Remove the check for timeout

* Check for clearTimeout to have been called

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
  • Loading branch information
rohit2sharma95 and XhmikosR committed Jul 11, 2020
1 parent 6acdfdb commit f6348f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions js/src/toast.js
Expand Up @@ -91,6 +91,8 @@ class Toast {
return
}

this._clearTimeout()

if (this._config.animation) {
this._element.classList.add(CLASS_NAME_FADE)
}
Expand Down Expand Up @@ -149,8 +151,7 @@ class Toast {
}

dispose() {
clearTimeout(this._timeout)
this._timeout = null
this._clearTimeout()

if (this._element.classList.contains(CLASS_NAME_SHOW)) {
this._element.classList.remove(CLASS_NAME_SHOW)
Expand Down Expand Up @@ -186,6 +187,11 @@ class Toast {
)
}

_clearTimeout() {
clearTimeout(this._timeout)
this._timeout = null
}

// Static

static jQueryInterface(config) {
Expand Down
27 changes: 27 additions & 0 deletions js/tests/unit/toast.spec.js
Expand Up @@ -170,6 +170,33 @@ describe('Toast', () => {

toast.show()
})

it('should clear timeout if toast is shown again before it is hidden', done => {
fixtureEl.innerHTML = [
'<div class="toast">',
' <div class="toast-body">',
' a simple toast',
' </div>',
'</div>'
].join('')

const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)

setTimeout(() => {
toast._config.autohide = false
toastEl.addEventListener('shown.bs.toast', () => {
expect(toast._clearTimeout).toHaveBeenCalled()
expect(toast._timeout).toBeNull()
done()
})
toast.show()
}, toast._config.delay / 2)

spyOn(toast, '_clearTimeout').and.callThrough()

toast.show()
})
})

describe('hide', () => {
Expand Down

0 comments on commit f6348f6

Please sign in to comment.