2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2022-09-08 22:03:58 +08:00
|
|
|
import type { CarouselRef } from '..';
|
2017-08-13 14:44:13 +08:00
|
|
|
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';
|
2023-06-07 11:54:50 +08:00
|
|
|
import { render, waitFakeTimer } 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(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2018-05-25 16:03:27 +08:00
|
|
|
});
|
|
|
|
|
2019-06-24 12:23:17 +08:00
|
|
|
afterEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2018-05-25 16:03:27 +08:00
|
|
|
});
|
|
|
|
|
2017-08-13 14:44:13 +08:00
|
|
|
it('should has innerSlider', () => {
|
2022-09-08 22:03:58 +08:00
|
|
|
const ref = React.createRef<CarouselRef>();
|
2022-07-02 16:31:13 +08:00
|
|
|
render(
|
2020-11-11 17:23:05 +08:00
|
|
|
<Carousel ref={ref}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<div />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2022-09-08 22:03:58 +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
|
|
|
|
2023-08-07 19:57:58 +08:00
|
|
|
it('should support id property', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Carousel id="my-carousel">
|
|
|
|
<div />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.ant-carousel')?.getAttribute('id')).toBe('my-carousel');
|
|
|
|
});
|
|
|
|
|
2022-07-02 16:31:13 +08:00
|
|
|
it('should has prev, next and go function', async () => {
|
2022-09-08 22:03:58 +08:00
|
|
|
const ref = React.createRef<CarouselRef>();
|
2022-07-02 16:31:13 +08:00
|
|
|
render(
|
2020-11-11 17:23:05 +08:00
|
|
|
<Carousel ref={ref}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2022-09-08 22:03:58 +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');
|
2022-09-08 22:03:58 +08:00
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(0);
|
|
|
|
ref.current?.goTo(2);
|
2022-10-21 15:09:48 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 22:03:58 +08:00
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(2);
|
2022-07-02 16:31:13 +08:00
|
|
|
// wait for animation to be finished
|
2022-10-21 15:09:48 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 22:03:58 +08:00
|
|
|
ref.current?.prev();
|
2022-10-21 15:09:48 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 22:03:58 +08:00
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(1);
|
2022-10-21 15:09:48 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 22:03:58 +08:00
|
|
|
ref.current?.next();
|
2022-10-21 15:09:48 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 22:03:58 +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 () => {
|
2022-09-08 22:03:58 +08:00
|
|
|
const ref = React.createRef<CarouselRef>();
|
2022-07-02 16:31:13 +08:00
|
|
|
render(
|
2020-11-11 17:23:05 +08:00
|
|
|
<Carousel autoplay ref={ref}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2023-06-07 21:59:21 +08:00
|
|
|
const spy = jest.spyOn(ref.current?.innerSlider, 'autoPlay');
|
2022-07-02 16:31:13 +08:00
|
|
|
window.resizeTo(1000, window.outerHeight);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2022-10-21 15:09:48 +08:00
|
|
|
await waitFakeTimer();
|
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 () => {
|
2022-07-02 16:31:13 +08:00
|
|
|
const { unmount } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Carousel autoplay>
|
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2023-06-07 21:59:21 +08:00
|
|
|
const spy = jest.spyOn(window, 'removeEventListener');
|
2022-07-02 16:31:13 +08:00
|
|
|
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', () => {
|
2022-11-19 13:47:33 +08:00
|
|
|
(['left', 'right', 'top', 'bottom'] as const).forEach((dotPosition) => {
|
2023-06-07 21:59:21 +08:00
|
|
|
// eslint-disable-next-line jest/valid-title
|
2019-05-06 12:04:39 +08:00
|
|
|
it(dotPosition, () => {
|
2022-07-02 16:31:13 +08:00
|
|
|
const { container } = render(
|
2019-05-06 12:04:39 +08:00
|
|
|
<Carousel dotPosition={dotPosition}>
|
|
|
|
<div />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2022-07-02 16:31:13 +08:00
|
|
|
container.normalize();
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-23 23:08:03 +08:00
|
|
|
describe('should active when children change', () => {
|
|
|
|
it('should active', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { rerender, container } = render(<Carousel />);
|
|
|
|
expect(container.querySelector('.slick-active')).toBeFalsy();
|
|
|
|
|
|
|
|
// Update children
|
|
|
|
rerender(
|
|
|
|
<Carousel>
|
|
|
|
<div />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.slick-active')).toBeTruthy();
|
2019-05-23 23:08:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep initialSlide', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { rerender, container } = render(<Carousel initialSlide={1} />);
|
|
|
|
rerender(
|
|
|
|
<Carousel initialSlide={1}>
|
|
|
|
<div key="1" />
|
|
|
|
<div key="2" />
|
|
|
|
<div key="3" />
|
|
|
|
</Carousel>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.slick-dots li')[1]).toHaveClass('slick-active');
|
2019-05-15 00:39:09 +08:00
|
|
|
});
|
|
|
|
});
|
2020-03-04 14:13:52 +08:00
|
|
|
|
|
|
|
describe('dots precise control by plain object', () => {
|
2023-02-23 21:56:43 +08:00
|
|
|
it('use dots to provide dotsClass', () => {
|
2022-07-02 16:31:13 +08:00
|
|
|
const { container } = render(
|
2020-03-04 14:13:52 +08:00
|
|
|
<Carousel dots={{ className: 'customDots' }}>
|
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
2022-07-02 16:31:13 +08:00
|
|
|
expect(container.querySelector('.slick-dots')).toHaveClass('customDots');
|
2020-03-04 14:13:52 +08:00
|
|
|
});
|
|
|
|
});
|
2023-04-25 10:08:35 +08:00
|
|
|
|
|
|
|
it('should not wait for the animation', async () => {
|
|
|
|
const ref = React.createRef<CarouselRef>();
|
|
|
|
render(
|
|
|
|
<Carousel ref={ref}>
|
|
|
|
<div>1</div>
|
|
|
|
<div>2</div>
|
|
|
|
<div>3</div>
|
|
|
|
</Carousel>,
|
|
|
|
);
|
|
|
|
const { prev, next, goTo } = ref.current || {};
|
|
|
|
expect(typeof prev).toBe('function');
|
|
|
|
expect(typeof next).toBe('function');
|
|
|
|
expect(typeof goTo).toBe('function');
|
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(0);
|
|
|
|
ref.current?.goTo(1);
|
|
|
|
ref.current?.goTo(2);
|
|
|
|
ref.current?.goTo(1);
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(1);
|
|
|
|
ref.current?.prev();
|
|
|
|
ref.current?.next();
|
|
|
|
ref.current?.next();
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(2);
|
|
|
|
ref.current?.prev();
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(ref.current?.innerSlider.state.currentSlide).toBe(1);
|
|
|
|
});
|
2017-08-13 14:44:13 +08:00
|
|
|
});
|