Skip to content

Commit

Permalink
fix: carousel should stop immediately after interval is set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusJam committed Jan 26, 2024
1 parent 6085ac0 commit 4e6e447
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ class Carousel extends React.Component {
}

componentDidUpdate(prevProps, prevState) {
if (prevState.activeIndex === this.state.activeIndex) return;
if (
prevState.activeIndex === this.state.activeIndex &&
(prevProps.interval === this.props.interval ||
this.props.interval !== false)
)
return;
this.setInterval();
}

Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/Carousel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CarouselIndicators from '../CarouselIndicators';
import CarouselControl from '../CarouselControl';
import CarouselCaption from '../CarouselCaption';
import { CarouselContext } from '../CarouselContext';
import { shallow } from 'enzyme';

const DEFAULT_TIMER_TIME = 600;

Expand Down Expand Up @@ -765,5 +766,23 @@ describe('Carousel', () => {
jest.advanceTimersByTime(1000);
expect(next).toHaveBeenCalled();
});

it('it should stop immediately when interval is set to false', () => {
const next = jest.fn();
const wrapper = shallow(
<Carousel
next={next}
previous={() => {}}
interval="1000"
activeIndex={0}
ride="carousel"
>
{slides}
</Carousel>,
);
wrapper.setProps({ interval: false });
jest.advanceTimersByTime(1000);
expect(next).not.toHaveBeenCalled();
});
});
});

0 comments on commit 4e6e447

Please sign in to comment.