Skip to content

Commit 0e57068

Browse files
authored
fix: autoplay interval bug (#253)
* Pause playing when certain control HOCs are manually * Tests * Fix trailing comma lint error
1 parent f14544f commit 0e57068

File tree

10 files changed

+64
-1
lines changed

10 files changed

+64
-1
lines changed

src/ButtonBack/ButtonBack.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class ButtonBack extends React.Component {
5555
carouselStore.setStoreState(
5656
{
5757
currentSlide: newCurrentSlide,
58+
isPlaying: false,
5859
},
5960
onClick !== null && onClick.call(this, ev),
6061
);

src/ButtonBack/__tests__/ButtonBack.test.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,20 @@ describe('<ButtonBack />', () => {
191191
);
192192
expect(wrapper.find('button').prop('foo')).toEqual('bar');
193193
});
194+
it('should pause autoplay when clicked', () => {
195+
const wrapper = mount(
196+
<CarouselProvider
197+
naturalSlideWidth={100}
198+
naturalSlideHeight={125}
199+
totalSlides={3}
200+
currentSlide={1}
201+
step={3}
202+
isPlaying
203+
>
204+
<ButtonBackWithStore>Hello</ButtonBackWithStore>
205+
</CarouselProvider>,
206+
);
207+
wrapper.find('button').simulate('click');
208+
expect(wrapper.instance().getStore().state.isPlaying).toBe(false);
209+
});
194210
});

src/ButtonFirst/ButtonFirst.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ const ButtonFirst = class ButtonFirst extends React.Component {
2929
const { carouselStore, onClick } = this.props;
3030
carouselStore.setStoreState({
3131
currentSlide: 0,
32+
isPlaying: false,
3233
}, onClick !== null && onClick.call(this, ev));
3334
}
3435

3536
render() {
3637
const {
37-
carouselStore, className, currentSlide, disabled, onClick, totalSlides, ...props
38+
carouselStore,
39+
className,
40+
currentSlide,
41+
disabled,
42+
onClick,
43+
totalSlides,
44+
...props
3845
} = this.props;
3946

4047
const newClassName = cn([

src/ButtonFirst/__tests__/ButtonFirst.test.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ describe('<ButtonFirst />', () => {
4040
wrapper.find('button').simulate('click');
4141
expect(onClick.mock.calls.length).toBe(1);
4242
});
43+
it('should pause autoplay when clicked', () => {
44+
const wrapper = mount(<ButtonFirst {...props} />);
45+
wrapper.find('button').simulate('click');
46+
expect(props.carouselStore.getStoreState().isPlaying).toBe(false);
47+
});
4348
});

src/ButtonLast/ButtonLast.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const ButtonLast = class ButtonLast extends React.Component {
3333
carouselStore.setStoreState(
3434
{
3535
currentSlide: totalSlides - visibleSlides,
36+
isPlaying: false,
3637
},
3738
onClick !== null && onClick.call(this, ev),
3839
);

src/ButtonLast/__tests__/ButtonLast.test.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,14 @@ describe('<ButtonLast />', () => {
7070
const wrapper = shallow(<ButtonLast {...newProps} />);
7171
expect(wrapper.prop('disabled')).toBe(false);
7272
});
73+
it('should pause autoplay when clicked', () => {
74+
const newProps = Object.assign({}, props, {
75+
carouselStore: new Store({
76+
isPlaying: true,
77+
}),
78+
});
79+
const wrapper = mount(<ButtonLast {...newProps} />);
80+
wrapper.find('button').simulate('click');
81+
expect(newProps.carouselStore.getStoreState().isPlaying).toBe(false);
82+
});
7383
});

src/ButtonNext/ButtonNext.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const ButtonNext = class ButtonNext extends React.PureComponent {
5656
carouselStore.setStoreState(
5757
{
5858
currentSlide: newCurrentSlide,
59+
isPlaying: false,
5960
},
6061
onClick !== null && onClick.call(this, ev),
6162
);

src/ButtonNext/__tests__/ButtonNext.test.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,20 @@ describe('<ButtonNext />', () => {
151151
expect(instance.carouselStore.state.currentSlide).toBe(newProps.totalSlides - newProps.visibleSlides);
152152
expect(wrapper.find('button').prop('disabled')).toBe(true);
153153
});
154+
it('should pause autoplay when clicked', () => {
155+
const wrapper = mount(
156+
<CarouselProvider
157+
naturalSlideWidth={100}
158+
naturalSlideHeight={125}
159+
totalSlides={3}
160+
currentSlide={1}
161+
step={3}
162+
isPlaying
163+
>
164+
<ButtonNextWithStore>Hello</ButtonNextWithStore>
165+
</CarouselProvider>,
166+
);
167+
wrapper.find('button').simulate('click');
168+
expect(wrapper.instance().getStore().state.isPlaying).toBe(false);
169+
});
154170
});

src/Dot/Dot.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const Dot = class Dot extends React.Component {
3737

3838
carouselStore.setStoreState({
3939
currentSlide: newSlide,
40+
isPlaying: false,
4041
}, onClick !== null && onClick.call(this, ev));
4142
}
4243

src/Dot/__tests__/Dot.test.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ describe('<Dot />', () => {
5959
const wrapper = mount(<Dot {...props} slide={0} disabled />);
6060
expect(wrapper.find('button').prop('disabled')).toBe(true);
6161
});
62+
it('should pause autoplay when clicked', () => {
63+
const wrapper = mount(<Dot {...props} slide={10} />);
64+
wrapper.find('button').simulate('click');
65+
expect(props.carouselStore.getStoreState().isPlaying).toBe(false);
66+
});
6267
});

0 commit comments

Comments
 (0)