Skip to content

Commit 9c32030

Browse files
Merge pull request #68 from zediah/fix-incorrect-index-to-before-change
Fix incorrect index index passed to beforeChange with infinite scrolling
2 parents e835b63 + 9bb49d8 commit 9c32030

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export default class Carousel extends Component {
330330

331331
this._animating = true;
332332

333-
beforeChange && beforeChange(index, currentSlide, direction);
333+
beforeChange && beforeChange(newIndex, currentSlide, direction);
334334
this.setState({
335335
transitionDuration
336336
}, () => {

test/unit/carousel.tests.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ describe('Carousel', () => {
109109

110110
it('should wrap around from the last to first slide if infinite is true and next is clicked', done => {
111111
const onSlideTransitionedStub = sinon.stub();
112+
const beforeChangeStub = sinon.stub();
112113

113114
renderToJsdom(
114115
<Carousel initialSlide={ 2 }
115116
slideWidth='300px'
116117
viewportWidth='300px'
117118
infinite={ true }
118-
onSlideTransitioned={ onSlideTransitionedStub }>
119+
onSlideTransitioned={ onSlideTransitionedStub }
120+
beforeChange={ beforeChangeStub }>
119121
<div id='slide1'/>
120122
<div id='slide2'/>
121123
<div id='slide3'/>
@@ -136,13 +138,16 @@ describe('Carousel', () => {
136138
index: 0,
137139
direction: 'right'
138140
});
141+
expect(beforeChangeStub).to.have.been.calledWith(0, 2, 'right');
139142
done();
140143
});
141144
});
142145

143146
it('should wrap around from the first to last slide if infinite is true and prev is clicked', done => {
147+
const beforeChangeStub = sinon.stub();
148+
144149
renderToJsdom(
145-
<Carousel initialSlide={ 2 } slideWidth='300px' viewportWidth='300px' infinite={ true }>
150+
<Carousel initialSlide={ 0 } slideWidth='300px' viewportWidth='300px' infinite={ true } beforeChange={ beforeChangeStub }>
146151
<div id='slide1'/>
147152
<div id='slide2'/>
148153
<div id='slide3'/>
@@ -152,12 +157,13 @@ describe('Carousel', () => {
152157
setImmediate(() => {
153158
let dots = tree.find('.carousel-dot');
154159
expect(dots.length).to.equal(3);
155-
expect(dots.at(2).prop('className')).to.contain('selected');
156-
const nextButton = tree.find('.carousel-right-arrow');
157-
nextButton.simulate('click');
158-
dots = tree.find('.carousel-dot');
159-
expect(dots.at(2).prop('className')).to.not.contain('selected');
160160
expect(dots.at(0).prop('className')).to.contain('selected');
161+
const prevButton = tree.find('.carousel-left-arrow');
162+
prevButton.simulate('click');
163+
dots = tree.find('.carousel-dot');
164+
expect(dots.at(0).prop('className')).to.not.contain('selected');
165+
expect(dots.at(2).prop('className')).to.contain('selected');
166+
expect(beforeChangeStub).to.have.been.calledWith(2, 0, 'left');
161167
done();
162168
});
163169
});

0 commit comments

Comments
 (0)