]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Clear timeout before showing the toast (#31155)
authorRohit Sharma <rohit2sharma95@gmail.com>
Sat, 11 Jul 2020 18:51:04 +0000 (00:21 +0530)
committerGitHub <noreply@github.com>
Sat, 11 Jul 2020 18:51:04 +0000 (11:51 -0700)
* 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>
js/src/toast.js
js/tests/unit/toast.spec.js

index 623e8977147db063153930b0462a3007b02f3a21..cca6d553b07e87865e51696556c1383353a6069e 100644 (file)
@@ -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,11 @@ class Toast {
     )
   }
 
+  _clearTimeout() {
+    clearTimeout(this._timeout)
+    this._timeout = null
+  }
+
   // Static
 
   static jQueryInterface(config) {
index ee623c8ccc5dc137bacf8ac34d627de80e890ef3..031c841afa0824df9f09f1ca8b816f3622d96312 100644 (file)
@@ -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', () => {