]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Carousel: move repeated code to a method
authorGeoSot <geo.sotis@gmail.com>
Thu, 9 Sep 2021 23:17:28 +0000 (02:17 +0300)
committerXhmikosR <xhmikosr@gmail.com>
Tue, 21 Dec 2021 15:37:24 +0000 (17:37 +0200)
js/src/carousel.js
js/tests/unit/carousel.spec.js

index e91ba376c1854c90a497df6018f68ca1f924dfa2..51c5dded8e135a083b14228bf945f45a46439ea7 100644 (file)
@@ -148,8 +148,7 @@ class Carousel extends BaseComponent {
       this.cycle(true)
     }
 
-    clearInterval(this._interval)
-    this._interval = null
+    this._clearInterval()
   }
 
   cycle(event) {
@@ -157,11 +156,7 @@ class Carousel extends BaseComponent {
       this._isPaused = false
     }
 
-    if (this._interval) {
-      clearInterval(this._interval)
-      this._interval = null
-    }
-
+    this._clearInterval()
     if (this._config.interval && !this._isPaused) {
       this._updateInterval()
 
@@ -412,6 +407,13 @@ class Carousel extends BaseComponent {
     return SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
   }
 
+  _clearInterval() {
+    if (this._interval) {
+      clearInterval(this._interval)
+      this._interval = null
+    }
+  }
+
   _directionToOrder(direction) {
     if (![DIRECTION_RIGHT, DIRECTION_LEFT].includes(direction)) {
       return direction
index ce9cd0fbcd4b4364dfa4d50d6a5bd10bb51abfb6..7b58b9de9eac6000387115057545955a94776dbe 100644 (file)
@@ -851,12 +851,12 @@ describe('Carousel', () => {
       const carousel = new Carousel(carouselEl)
 
       spyOn(carousel, 'cycle')
-      spyOn(window, 'clearInterval')
+      spyOn(carousel, '_clearInterval')
 
       carousel.pause()
 
       expect(carousel.cycle).toHaveBeenCalledWith(true)
-      expect(window.clearInterval).toHaveBeenCalled()
+      expect(carousel._clearInterval).toHaveBeenCalled()
       expect(carousel._isPaused).toBeTrue()
     })
 
@@ -877,12 +877,12 @@ describe('Carousel', () => {
       const carousel = new Carousel(carouselEl)
 
       spyOn(carousel, 'cycle')
-      spyOn(window, 'clearInterval')
+      spyOn(carousel, '_clearInterval')
 
       carousel.pause()
 
       expect(carousel.cycle).not.toHaveBeenCalled()
-      expect(window.clearInterval).toHaveBeenCalled()
+      expect(carousel._clearInterval).toHaveBeenCalled()
       expect(carousel._isPaused).toBeTrue()
     })
 
@@ -903,11 +903,11 @@ describe('Carousel', () => {
       const carousel = new Carousel(carouselEl)
       const event = createEvent('mouseenter')
 
-      spyOn(window, 'clearInterval')
+      spyOn(carousel, '_clearInterval')
 
       carousel.pause(event)
 
-      expect(window.clearInterval).toHaveBeenCalled()
+      expect(carousel._clearInterval).toHaveBeenCalled()
       expect(carousel._isPaused).toBeFalse()
     })
   })