Unverified Commit f6348f6c authored by Rohit Sharma's avatar Rohit Sharma Committed by GitHub
Browse files

Clear timeout before showing the toast (#31155)


* clear timeout before showing the toast

* Add unit test

* Remove the check for timeout

* Check for clearTimeout to have been called

Co-authored-by: default avatarXhmikosR <xhmikosr@gmail.com>
parent 6acdfdbf
Showing with 35 additions and 2 deletions
+35 -2
...@@ -91,6 +91,8 @@ class Toast { ...@@ -91,6 +91,8 @@ class Toast {
return return
} }
this._clearTimeout()
if (this._config.animation) { if (this._config.animation) {
this._element.classList.add(CLASS_NAME_FADE) this._element.classList.add(CLASS_NAME_FADE)
} }
...@@ -149,8 +151,7 @@ class Toast { ...@@ -149,8 +151,7 @@ class Toast {
} }
dispose() { dispose() {
clearTimeout(this._timeout) this._clearTimeout()
this._timeout = null
if (this._element.classList.contains(CLASS_NAME_SHOW)) { if (this._element.classList.contains(CLASS_NAME_SHOW)) {
this._element.classList.remove(CLASS_NAME_SHOW) this._element.classList.remove(CLASS_NAME_SHOW)
...@@ -186,6 +187,11 @@ class Toast { ...@@ -186,6 +187,11 @@ class Toast {
) )
} }
_clearTimeout() {
clearTimeout(this._timeout)
this._timeout = null
}
// Static // Static
static jQueryInterface(config) { static jQueryInterface(config) {
......
...@@ -170,6 +170,33 @@ describe('Toast', () => { ...@@ -170,6 +170,33 @@ describe('Toast', () => {
toast.show() 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', () => { describe('hide', () => {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment