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 bf99409
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
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
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import user from '@testing-library/user-event';
import { shallow } from 'enzyme';
import { Carousel } from '..';
import CarouselItem from '../CarouselItem';
import CarouselIndicators from '../CarouselIndicators';
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 bf99409

Please sign in to comment.