diff options
| author | alpadev <[email protected]> | 2021-06-16 06:48:23 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-16 07:48:23 +0300 |
| commit | d62ba935ef2e1ee97a57b1b75090e50e86e0d140 (patch) | |
| tree | 1232c08d70ad843bdf48d80fba4435d0bd1fddad | |
| parent | 9485172017868952047da5f188bc13a92ef0435d (diff) | |
| download | bootstrap-d62ba935ef2e1ee97a57b1b75090e50e86e0d140.tar.xz bootstrap-d62ba935ef2e1ee97a57b1b75090e50e86e0d140.zip | |
Fix carousel buttons (#34266)
* test(carousel): add test to check if next/prev button work as intended
* fix(carousel): merge passed config with instance config in carouselInterface
| -rw-r--r-- | js/src/carousel.js | 9 | ||||
| -rw-r--r-- | js/tests/unit/carousel.spec.js | 28 |
2 files changed, 36 insertions, 1 deletions
diff --git a/js/src/carousel.js b/js/src/carousel.js index a956ebc8b..fa401535a 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -498,7 +498,14 @@ class Carousel extends BaseComponent { static carouselInterface(element, config) { const data = Carousel.getOrCreateInstance(element, config) - const { _config } = data + let { _config } = data + if (typeof config === 'object') { + _config = { + ..._config, + ...config + } + } + const action = typeof config === 'string' ? config : _config.slide if (typeof config === 'number') { diff --git a/js/tests/unit/carousel.spec.js b/js/tests/unit/carousel.spec.js index 5120cc601..74f82ce1f 100644 --- a/js/tests/unit/carousel.spec.js +++ b/js/tests/unit/carousel.spec.js @@ -707,6 +707,34 @@ describe('Carousel', () => { carousel.next() }) + + it('should call next()/prev() instance methods when clicking the respective direction buttons', () => { + fixtureEl.innerHTML = [ + '<div id="carousel" class="carousel slide">', + ' <div class="carousel-inner">', + ' <div class="carousel-item active">item 1</div>', + ' <div class="carousel-item">item 2</div>', + ' <div class="carousel-item">item 3</div>', + ' </div>', + ' <button class="carousel-control-prev" type="button" data-bs-target="#carousel" data-bs-slide="prev"></button>', + ' <button class="carousel-control-next" type="button" data-bs-target="#carousel" data-bs-slide="next"></button>', + '</div>' + ].join('') + + const carouselEl = fixtureEl.querySelector('#carousel') + const prevBtnEl = fixtureEl.querySelector('.carousel-control-prev') + const nextBtnEl = fixtureEl.querySelector('.carousel-control-next') + + const carousel = new Carousel(carouselEl) + const nextSpy = spyOn(carousel, 'next') + const prevSpy = spyOn(carousel, 'prev') + + nextBtnEl.click() + prevBtnEl.click() + + expect(nextSpy).toHaveBeenCalled() + expect(prevSpy).toHaveBeenCalled() + }) }) describe('nextWhenVisible', () => { |
