diff options
author | GeoSot <geo.sotis@gmail.com> | 2021-09-10 02:17:28 +0300 |
---|---|---|
committer | XhmikosR <xhmikosr@gmail.com> | 2021-12-21 18:37:24 +0300 |
commit | 0d4213bde39bb4f2e2bc5a0df699dad82780efa3 (patch) | |
tree | 2b76fde2fd30ef635c4d1985dbdaa3a17f9c0aea /js | |
parent | b8ee68cfa0f3516dc55aec5da6d7e43e2705f402 (diff) |
Carousel: move repeated code to a method
Diffstat (limited to 'js')
-rw-r--r-- | js/src/carousel.js | 16 | ||||
-rw-r--r-- | js/tests/unit/carousel.spec.js | 12 |
2 files changed, 15 insertions, 13 deletions
diff --git a/js/src/carousel.js b/js/src/carousel.js index e91ba376c1..51c5dded8e 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -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 diff --git a/js/tests/unit/carousel.spec.js b/js/tests/unit/carousel.spec.js index ce9cd0fbcd..7b58b9de9e 100644 --- a/js/tests/unit/carousel.spec.js +++ b/js/tests/unit/carousel.spec.js @@ -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() }) }) |