Skip to content
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

fix: carousel should stop immediately after interval is set to false #2791

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Carousel.js
Expand Up @@ -112,6 +112,9 @@ class Carousel extends React.Component {
}

componentDidUpdate(prevProps, prevState) {
if (prevProps.interval !== this.props.interval && (prevProps.interval === false || this.props.interval === false)) {
this.setInterval();
}
if (prevState.activeIndex === this.state.activeIndex) return;
this.setInterval();
}
Expand Down
59 changes: 59 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,63 @@ describe('Carousel', () => {
jest.advanceTimersByTime(1000);
expect(next).toHaveBeenCalled();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if you can rewrite the test to use react testing library

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with react testing library but I've pushed some changes. Please let me know if it's fine now.


it('it should stop immediately when interval is set to false', () => {
const next = jest.fn();
const { rerender } = render(
<Carousel
next={next}
previous={() => {}}
interval="1000"
activeIndex={0}
ride="carousel"
>
{slides}
</Carousel>,
);

rerender(
<Carousel
next={next}
previous={() => {}}
interval={false}
activeIndex={0}
ride="carousel"
>
{slides}
</Carousel>,
);
jest.advanceTimersByTime(1000);
expect(next).not.toHaveBeenCalled();
});

it('it should restart when interval is set again', () => {
const next = jest.fn();
const { rerender } = render(
<Carousel
next={next}
previous={() => {}}
interval={false}
activeIndex={0}
ride="carousel"
>
{slides}
</Carousel>,
);

rerender(
<Carousel
next={next}
previous={() => {}}
interval="1000"
activeIndex={0}
ride="carousel"
>
{slides}
</Carousel>,
);
jest.advanceTimersByTime(1000);
expect(next).toHaveBeenCalled();
});
});
});