2017-08-13 14:44:13 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Carousel from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2021-11-26 12:18:21 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2017-08-13 14:44:13 +08:00
|
|
|
|
|
|
|
describe('Carousel', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Carousel);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Carousel);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2019-06-24 12:23:17 +08:00
|
|
|
beforeEach(() => {
|
2018-05-25 16:03:27 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2019-06-24 12:23:17 +08:00
|
|
|
afterEach(() => {
|
2018-05-25 16:03:27 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2017-08-13 14:44:13 +08:00
|
|
|
it('should has innerSlider', () => {
|
2020-11-11 17:23:05 +08:00
|
|
|
const ref = React.createRef();
|
|
|
|
mount(
|
|
|
|
<Carousel ref={ref}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<div />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2020-11-11 17:23:05 +08:00
|
|
|
const { innerSlider } = ref.current;
|
2017-08-13 14:44:13 +08:00
|
|
|
expect(typeof innerSlider.slickNext).toBe('function');
|
|
|
|
});
|
2018-05-25 16:03:27 +08:00
|
|
|
|
|
|
|
it('should has prev, next and go function', () => {
|
2020-11-11 17:23:05 +08:00
|
|
|
const ref = React.createRef();
|
|
|
|
mount(
|
|
|
|
<Carousel ref={ref}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2020-11-11 17:23:05 +08:00
|
|
|
const { prev, next, goTo } = ref.current;
|
2018-05-25 16:03:27 +08:00
|
|
|
expect(typeof prev).toBe('function');
|
|
|
|
expect(typeof next).toBe('function');
|
|
|
|
expect(typeof goTo).toBe('function');
|
2020-11-11 17:23:05 +08:00
|
|
|
expect(ref.current.innerSlider.state.currentSlide).toBe(0);
|
|
|
|
ref.current.goTo(2);
|
2018-05-25 16:03:27 +08:00
|
|
|
jest.runAllTimers();
|
2020-11-11 17:23:05 +08:00
|
|
|
expect(ref.current.innerSlider.state.currentSlide).toBe(2);
|
|
|
|
ref.current.prev();
|
2018-05-25 16:03:27 +08:00
|
|
|
jest.runAllTimers();
|
2020-11-11 17:23:05 +08:00
|
|
|
expect(ref.current.innerSlider.state.currentSlide).toBe(1);
|
|
|
|
ref.current.next();
|
2018-05-25 16:03:27 +08:00
|
|
|
jest.runAllTimers();
|
2020-11-11 17:23:05 +08:00
|
|
|
expect(ref.current.innerSlider.state.currentSlide).toBe(2);
|
2018-05-25 16:03:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger autoPlay after window resize', async () => {
|
|
|
|
jest.useRealTimers();
|
2020-11-11 17:23:05 +08:00
|
|
|
const ref = React.createRef();
|
|
|
|
mount(
|
|
|
|
<Carousel autoplay ref={ref}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2020-11-11 17:23:05 +08:00
|
|
|
const spy = jest.spyOn(ref.current.innerSlider, 'autoPlay');
|
2018-05-25 16:03:27 +08:00
|
|
|
window.resizeTo(1000);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2021-11-26 12:18:21 +08:00
|
|
|
await sleep(500);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(spy).toHaveBeenCalled();
|
2018-05-25 16:03:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('cancel resize listener when unmount', async () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Carousel autoplay>
|
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2020-11-11 17:23:05 +08:00
|
|
|
const spy = jest.spyOn(window, 'removeEventListener');
|
2018-05-25 16:03:27 +08:00
|
|
|
wrapper.unmount();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(spy).toHaveBeenCalled();
|
2018-05-25 16:03:27 +08:00
|
|
|
});
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
describe('should works for dotPosition', () => {
|
|
|
|
['left', 'right', 'top', 'bottom'].forEach(dotPosition => {
|
2020-09-15 11:54:47 +08:00
|
|
|
// eslint-disable-next-line jest/valid-title
|
2019-05-06 12:04:39 +08:00
|
|
|
it(dotPosition, () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Carousel dotPosition={dotPosition}>
|
|
|
|
<div />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-23 23:08:03 +08:00
|
|
|
describe('should active when children change', () => {
|
|
|
|
it('should active', () => {
|
|
|
|
const wrapper = mount(<Carousel />);
|
|
|
|
wrapper.setProps({
|
|
|
|
children: <div />,
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.slick-active').length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep initialSlide', () => {
|
|
|
|
const wrapper = mount(<Carousel initialSlide={1} />);
|
|
|
|
wrapper.setProps({
|
|
|
|
children: [<div key="1" />, <div key="2" />, <div key="3" />],
|
|
|
|
});
|
|
|
|
wrapper.update();
|
2020-09-15 11:54:47 +08:00
|
|
|
expect(wrapper.find('.slick-dots li').at(1).hasClass('slick-active')).toBeTruthy();
|
2019-05-15 00:39:09 +08:00
|
|
|
});
|
|
|
|
});
|
2020-03-04 14:13:52 +08:00
|
|
|
|
|
|
|
describe('dots precise control by plain object', () => {
|
|
|
|
it('use dots to provide dotsClasse', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Carousel dots={{ className: 'customDots' }}>
|
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.slick-dots').hasClass('customDots')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2017-08-13 14:44:13 +08:00
|
|
|
});
|