Skip to content

Commit

Permalink
fix: prevent currentSlide from going out of bounds of totalSlides (
Browse files Browse the repository at this point in the history
…#240)

* prevent currentSlide from going out of bounds

* Update CarouselProvider.test.jsx
  • Loading branch information
seleb authored Feb 11, 2020
1 parent 56ca553 commit 30e3110
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/CarouselProvider/CarouselProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ const CarouselProvider = class CarouselProvider extends React.Component {
newStoreState.slideTraySize = slideTraySize(this.props.totalSlides, this.props.visibleSlides);
}

if (this.carouselStore.state.currentSlide >= this.props.totalSlides) {
newStoreState.currentSlide = this.props.totalSlides - 1;
}

if (Object.keys(newStoreState).length > 0) {
this.carouselStore.setStoreState(newStoreState);
}
Expand Down
8 changes: 7 additions & 1 deletion src/CarouselProvider/__tests__/CarouselProvider.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('<CarouselProvider />', () => {
expect(instance.carouselStore.state.slideSize).toBe(25);
expect(instance.carouselStore.state.slideTraySize).toBe(200);
});
it('should keep currentSlide within bounds of totalSlides', () => {
const wrapper = shallow(<CarouselProvider {...props} currentSlide={3} totalSlides={4} />);
const instance = wrapper.instance();
wrapper.setProps({ totalSlides: 3 });
expect(instance.carouselStore.state.currentSlide).toEqual(2);
});
it('should not update the carouselStore if some prop we do not track changes', () => {
const wrapper = shallow(<CarouselProvider {...props} data-foo={1} />);
const instance = wrapper.instance();
Expand All @@ -58,7 +64,7 @@ describe('<CarouselProvider />', () => {
expect(start).toEqual(end);
});
it('should not reset the currentSlide or disableAnimation values when unrelated props change', () => {
const wrapper = shallow(<CarouselProvider {...props} data-foo={1} />);
const wrapper = shallow(<CarouselProvider {...props} totalSlides={4} data-foo={1} />);
const instance = wrapper.instance();
instance.carouselStore.setStoreState({ currentSlide: 2 });
const start = clone(instance.carouselStore.state);
Expand Down

0 comments on commit 30e3110

Please sign in to comment.