Skip to content

Commit

Permalink
fix: autoplay interval bug (#253)
Browse files Browse the repository at this point in the history
* Pause playing when certain control HOCs are manually

* Tests

* Fix trailing comma lint error
  • Loading branch information
ZLevine authored Mar 17, 2020
1 parent f14544f commit 0e57068
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ButtonBack/ButtonBack.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class ButtonBack extends React.Component {
carouselStore.setStoreState(
{
currentSlide: newCurrentSlide,
isPlaying: false,
},
onClick !== null && onClick.call(this, ev),
);
Expand Down
16 changes: 16 additions & 0 deletions src/ButtonBack/__tests__/ButtonBack.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,20 @@ describe('<ButtonBack />', () => {
);
expect(wrapper.find('button').prop('foo')).toEqual('bar');
});
it('should pause autoplay when clicked', () => {
const wrapper = mount(
<CarouselProvider
naturalSlideWidth={100}
naturalSlideHeight={125}
totalSlides={3}
currentSlide={1}
step={3}
isPlaying
>
<ButtonBackWithStore>Hello</ButtonBackWithStore>
</CarouselProvider>,
);
wrapper.find('button').simulate('click');
expect(wrapper.instance().getStore().state.isPlaying).toBe(false);
});
});
9 changes: 8 additions & 1 deletion src/ButtonFirst/ButtonFirst.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ const ButtonFirst = class ButtonFirst extends React.Component {
const { carouselStore, onClick } = this.props;
carouselStore.setStoreState({
currentSlide: 0,
isPlaying: false,
}, onClick !== null && onClick.call(this, ev));
}

render() {
const {
carouselStore, className, currentSlide, disabled, onClick, totalSlides, ...props
carouselStore,
className,
currentSlide,
disabled,
onClick,
totalSlides,
...props
} = this.props;

const newClassName = cn([
Expand Down
5 changes: 5 additions & 0 deletions src/ButtonFirst/__tests__/ButtonFirst.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ describe('<ButtonFirst />', () => {
wrapper.find('button').simulate('click');
expect(onClick.mock.calls.length).toBe(1);
});
it('should pause autoplay when clicked', () => {
const wrapper = mount(<ButtonFirst {...props} />);
wrapper.find('button').simulate('click');
expect(props.carouselStore.getStoreState().isPlaying).toBe(false);
});
});
1 change: 1 addition & 0 deletions src/ButtonLast/ButtonLast.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const ButtonLast = class ButtonLast extends React.Component {
carouselStore.setStoreState(
{
currentSlide: totalSlides - visibleSlides,
isPlaying: false,
},
onClick !== null && onClick.call(this, ev),
);
Expand Down
10 changes: 10 additions & 0 deletions src/ButtonLast/__tests__/ButtonLast.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ describe('<ButtonLast />', () => {
const wrapper = shallow(<ButtonLast {...newProps} />);
expect(wrapper.prop('disabled')).toBe(false);
});
it('should pause autoplay when clicked', () => {
const newProps = Object.assign({}, props, {
carouselStore: new Store({
isPlaying: true,
}),
});
const wrapper = mount(<ButtonLast {...newProps} />);
wrapper.find('button').simulate('click');
expect(newProps.carouselStore.getStoreState().isPlaying).toBe(false);
});
});
1 change: 1 addition & 0 deletions src/ButtonNext/ButtonNext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const ButtonNext = class ButtonNext extends React.PureComponent {
carouselStore.setStoreState(
{
currentSlide: newCurrentSlide,
isPlaying: false,
},
onClick !== null && onClick.call(this, ev),
);
Expand Down
16 changes: 16 additions & 0 deletions src/ButtonNext/__tests__/ButtonNext.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,20 @@ describe('<ButtonNext />', () => {
expect(instance.carouselStore.state.currentSlide).toBe(newProps.totalSlides - newProps.visibleSlides);
expect(wrapper.find('button').prop('disabled')).toBe(true);
});
it('should pause autoplay when clicked', () => {
const wrapper = mount(
<CarouselProvider
naturalSlideWidth={100}
naturalSlideHeight={125}
totalSlides={3}
currentSlide={1}
step={3}
isPlaying
>
<ButtonNextWithStore>Hello</ButtonNextWithStore>
</CarouselProvider>,
);
wrapper.find('button').simulate('click');
expect(wrapper.instance().getStore().state.isPlaying).toBe(false);
});
});
1 change: 1 addition & 0 deletions src/Dot/Dot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Dot = class Dot extends React.Component {

carouselStore.setStoreState({
currentSlide: newSlide,
isPlaying: false,
}, onClick !== null && onClick.call(this, ev));
}

Expand Down
5 changes: 5 additions & 0 deletions src/Dot/__tests__/Dot.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ describe('<Dot />', () => {
const wrapper = mount(<Dot {...props} slide={0} disabled />);
expect(wrapper.find('button').prop('disabled')).toBe(true);
});
it('should pause autoplay when clicked', () => {
const wrapper = mount(<Dot {...props} slide={10} />);
wrapper.find('button').simulate('click');
expect(props.carouselStore.getStoreState().isPlaying).toBe(false);
});
});

0 comments on commit 0e57068

Please sign in to comment.