Skip to content

Commit d62ba93

Browse files
authored
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
1 parent 9485172 commit d62ba93

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

js/src/carousel.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,14 @@ class Carousel extends BaseComponent {
498498
static carouselInterface(element, config) {
499499
const data = Carousel.getOrCreateInstance(element, config)
500500

501-
const { _config } = data
501+
let { _config } = data
502+
if (typeof config === 'object') {
503+
_config = {
504+
..._config,
505+
...config
506+
}
507+
}
508+
502509
const action = typeof config === 'string' ? config : _config.slide
503510

504511
if (typeof config === 'number') {

js/tests/unit/carousel.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,34 @@ describe('Carousel', () => {
707707

708708
carousel.next()
709709
})
710+
711+
it('should call next()/prev() instance methods when clicking the respective direction buttons', () => {
712+
fixtureEl.innerHTML = [
713+
'<div id="carousel" class="carousel slide">',
714+
' <div class="carousel-inner">',
715+
' <div class="carousel-item active">item 1</div>',
716+
' <div class="carousel-item">item 2</div>',
717+
' <div class="carousel-item">item 3</div>',
718+
' </div>',
719+
' <button class="carousel-control-prev" type="button" data-bs-target="#carousel" data-bs-slide="prev"></button>',
720+
' <button class="carousel-control-next" type="button" data-bs-target="#carousel" data-bs-slide="next"></button>',
721+
'</div>'
722+
].join('')
723+
724+
const carouselEl = fixtureEl.querySelector('#carousel')
725+
const prevBtnEl = fixtureEl.querySelector('.carousel-control-prev')
726+
const nextBtnEl = fixtureEl.querySelector('.carousel-control-next')
727+
728+
const carousel = new Carousel(carouselEl)
729+
const nextSpy = spyOn(carousel, 'next')
730+
const prevSpy = spyOn(carousel, 'prev')
731+
732+
nextBtnEl.click()
733+
prevBtnEl.click()
734+
735+
expect(nextSpy).toHaveBeenCalled()
736+
expect(prevSpy).toHaveBeenCalled()
737+
})
710738
})
711739

712740
describe('nextWhenVisible', () => {

0 commit comments

Comments
 (0)