Skip to content

fix: prevent currentSlide from going out of bounds of totalSlides #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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