2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import Carousel from '..';
|
2020-03-07 11:45:13 +00:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2018-07-20 08:13:21 +00:00
|
|
|
|
|
|
|
describe('Carousel', () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
mountTest(Carousel);
|
|
|
|
// beforeEach(() => {
|
|
|
|
// jest.useFakeTimers();
|
|
|
|
// });
|
|
|
|
|
|
|
|
// afterEach(() => {
|
|
|
|
// jest.useRealTimers();
|
|
|
|
// });
|
2018-07-20 08:13:21 +00:00
|
|
|
it('should has innerSlider', () => {
|
|
|
|
const props = {
|
|
|
|
slots: {
|
|
|
|
default: '<div />',
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Carousel, props);
|
|
|
|
const { innerSlider, $refs } = wrapper.vm;
|
|
|
|
const innerSliderFromRefs = $refs.slick.innerSlider;
|
|
|
|
expect(innerSlider).toBe(innerSliderFromRefs);
|
|
|
|
expect(typeof innerSlider.slickNext).toBe('function');
|
|
|
|
});
|
2018-07-20 08:13:21 +00:00
|
|
|
|
|
|
|
it('should has prev, next and go function', async () => {
|
|
|
|
const props = {
|
|
|
|
slots: {
|
|
|
|
default: '<div>1</div><div>2</div><div>3</div>',
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Carousel, props);
|
|
|
|
const { prev, next, goTo } = wrapper.vm;
|
|
|
|
expect(typeof prev).toBe('function');
|
|
|
|
expect(typeof next).toBe('function');
|
|
|
|
expect(typeof goTo).toBe('function');
|
|
|
|
const slick = wrapper.vm.$refs.slick;
|
2018-07-20 08:13:21 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(slick.innerSlider.currentSlide).toBe(0);
|
|
|
|
wrapper.vm.goTo(2);
|
2018-07-20 08:13:21 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(slick.innerSlider.currentSlide).toBe(2);
|
|
|
|
}, 1000);
|
|
|
|
prev();
|
2018-07-20 08:13:21 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(slick.innerSlider.currentSlide).toBe(1);
|
|
|
|
}, 1000);
|
2018-07-20 08:13:21 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
next();
|
2018-07-20 08:13:21 +00:00
|
|
|
|
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(slick.innerSlider.currentSlide).toBe(2);
|
|
|
|
}, 1000);
|
|
|
|
});
|
2018-07-20 08:13:21 +00:00
|
|
|
|
|
|
|
it('should trigger autoPlay after window resize', async () => {
|
|
|
|
const props = {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2018-07-20 08:13:21 +00:00
|
|
|
autoplay: true,
|
|
|
|
},
|
|
|
|
slots: {
|
|
|
|
default: '<div>1</div><div>2</div><div>3</div>',
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Carousel, props);
|
|
|
|
const spy = jest.spyOn(wrapper.vm.$refs.slick.innerSlider, 'handleAutoPlay');
|
|
|
|
window.resizeTo(1000);
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2019-01-12 03:33:27 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(spy).toHaveBeenCalled();
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-07-20 08:13:21 +00:00
|
|
|
|
|
|
|
it('cancel resize listener when unmount', async () => {
|
|
|
|
const props = {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2018-07-20 08:13:21 +00:00
|
|
|
autoplay: true,
|
|
|
|
},
|
|
|
|
slots: {
|
|
|
|
default: '<div>1</div><div>2</div><div>3</div>',
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Carousel, props);
|
|
|
|
const { onWindowResized } = wrapper.vm;
|
|
|
|
const spy = jest.spyOn(wrapper.vm.onWindowResized, 'cancel');
|
|
|
|
const spy2 = jest.spyOn(window, 'removeEventListener');
|
2020-07-25 07:46:49 +00:00
|
|
|
wrapper.unmount();
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
expect(spy2).toHaveBeenCalledWith('resize', onWindowResized);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should works for dotPosition', () => {
|
|
|
|
['left', 'right', 'top', 'bottom'].forEach(dotPosition => {
|
|
|
|
it(dotPosition, () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Carousel dotPosition={dotPosition}>
|
|
|
|
<div />
|
|
|
|
</Carousel>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('warning', () => {
|
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
mount({
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Carousel vertical>
|
|
|
|
<div />
|
|
|
|
</Carousel>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(warnSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antdv: Carousel] `vertical` is deprecated, please use `dotPosition` instead.',
|
|
|
|
);
|
|
|
|
warnSpy.mockRestore();
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|