From 1a8edb3bc44715ed28c6559db5df05026d1a931c Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Tue, 23 Jun 2020 23:32:15 +0530 Subject: [PATCH 1/4] clear timeout before showing the toast --- js/src/toast.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/js/src/toast.js b/js/src/toast.js index 623e8977147d..4aa767828c90 100644 --- a/js/src/toast.js +++ b/js/src/toast.js @@ -91,6 +91,8 @@ class Toast { return } + this._clearTimeout() + if (this._config.animation) { this._element.classList.add(CLASS_NAME_FADE) } @@ -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) @@ -186,6 +187,13 @@ class Toast { ) } + _clearTimeout() { + if (this._timeout) { + clearTimeout(this._timeout) + this._timeout = null + } + } + // Static static jQueryInterface(config) { From a4489f40f2d4c66527fd124677f8b1f7fd026b29 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Wed, 24 Jun 2020 00:43:56 +0530 Subject: [PATCH 2/4] Add unit test --- js/tests/unit/toast.spec.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/js/tests/unit/toast.spec.js b/js/tests/unit/toast.spec.js index ee623c8ccc5d..5470d52e6fad 100644 --- a/js/tests/unit/toast.spec.js +++ b/js/tests/unit/toast.spec.js @@ -170,6 +170,30 @@ describe('Toast', () => { toast.show() }) + + it('should clear timeout if toast is shown again before it is hidden', done => { + fixtureEl.innerHTML = [ + '
', + '
', + ' a simple toast', + '
', + '
' + ].join('') + + const toastEl = fixtureEl.querySelector('.toast') + const toast = new Toast(toastEl) + + setTimeout(() => { + toast._config.autohide = false + toastEl.addEventListener('shown.bs.toast', () => { + expect(toast._timeout).toBeNull() + done() + }) + toast.show() + }, toast._config.delay / 2) + + toast.show() + }) }) describe('hide', () => { From a48507bed901d0231262b42b4c67c25fc78edf30 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Tue, 7 Jul 2020 17:05:51 +0530 Subject: [PATCH 3/4] Remove the check for timeout --- js/src/toast.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/js/src/toast.js b/js/src/toast.js index 4aa767828c90..cca6d553b07e 100644 --- a/js/src/toast.js +++ b/js/src/toast.js @@ -188,10 +188,8 @@ class Toast { } _clearTimeout() { - if (this._timeout) { - clearTimeout(this._timeout) - this._timeout = null - } + clearTimeout(this._timeout) + this._timeout = null } // Static From 616dc15eb6c9df1f3c7b325190727b83e8a95c69 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Tue, 7 Jul 2020 17:11:12 +0530 Subject: [PATCH 4/4] Check for clearTimeout to have been called --- js/tests/unit/toast.spec.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js/tests/unit/toast.spec.js b/js/tests/unit/toast.spec.js index 5470d52e6fad..031c841afa08 100644 --- a/js/tests/unit/toast.spec.js +++ b/js/tests/unit/toast.spec.js @@ -186,12 +186,15 @@ describe('Toast', () => { 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() }) })