fix: carousel slick ref not work

pull/2682/head
tangjinzhou 4 years ago
parent 3ca47f078c
commit 45c899dab1

@ -1 +1 @@
Subproject commit fedb9ecff3abc0ef5d7f57cf5cee1f8352dd9d85 Subproject commit 8b691ca22c5a535a911f9bde1d149f1e7a9da652

@ -4,10 +4,10 @@ exports[`Carousel should works for dotPosition bottom 1`] = `
<div class="ant-carousel"> <div class="ant-carousel">
<div class="slick-slider slick-initialized"> <div class="slick-slider slick-initialized">
<div class="slick-list"> <div class="slick-list">
<div class="slick-track" style="width: 100%; left: 0%;"> <div class="slick-track" style="opacity: 1; transform: translate3d(0px, 0px, 0px);">
<div tabindex="-1" data-index="0" class="slick-slide slick-active slick-current" style="outline: none; width: 100%;"> <div tabindex="-1" data-index="0" aria-hidden="false" class="slick-slide slick-active slick-current" style="outline: none; width: 0px;">
<div> <div>
<div tabindex="-1" class="" style="width: 100%; display: inline-block;"></div> <div tabindex="-1" style="width: 100%; display: inline-block;"></div>
</div> </div>
</div> </div>
</div> </div>
@ -20,10 +20,10 @@ exports[`Carousel should works for dotPosition left 1`] = `
<div class="ant-carousel ant-carousel-vertical"> <div class="ant-carousel ant-carousel-vertical">
<div class="slick-slider slick-vertical slick-initialized"> <div class="slick-slider slick-vertical slick-initialized">
<div class="slick-list"> <div class="slick-list">
<div class="slick-track" style="width: 100%; left: 0%;"> <div class="slick-track" style="opacity: 1; transform: translate3d(0px, 0px, 0px);">
<div tabindex="-1" data-index="0" class="slick-slide slick-active slick-current" style="outline: none; width: 100%;"> <div tabindex="-1" data-index="0" aria-hidden="false" class="slick-slide slick-active slick-current" style="outline: none; width: 0px;">
<div> <div>
<div tabindex="-1" class="" style="width: 100%; display: inline-block;"></div> <div tabindex="-1" style="width: 100%; display: inline-block;"></div>
</div> </div>
</div> </div>
</div> </div>
@ -36,10 +36,10 @@ exports[`Carousel should works for dotPosition right 1`] = `
<div class="ant-carousel ant-carousel-vertical"> <div class="ant-carousel ant-carousel-vertical">
<div class="slick-slider slick-vertical slick-initialized"> <div class="slick-slider slick-vertical slick-initialized">
<div class="slick-list"> <div class="slick-list">
<div class="slick-track" style="width: 100%; left: 0%;"> <div class="slick-track" style="opacity: 1; transform: translate3d(0px, 0px, 0px);">
<div tabindex="-1" data-index="0" class="slick-slide slick-active slick-current" style="outline: none; width: 100%;"> <div tabindex="-1" data-index="0" aria-hidden="false" class="slick-slide slick-active slick-current" style="outline: none; width: 0px;">
<div> <div>
<div tabindex="-1" class="" style="width: 100%; display: inline-block;"></div> <div tabindex="-1" style="width: 100%; display: inline-block;"></div>
</div> </div>
</div> </div>
</div> </div>
@ -52,10 +52,10 @@ exports[`Carousel should works for dotPosition top 1`] = `
<div class="ant-carousel"> <div class="ant-carousel">
<div class="slick-slider slick-initialized"> <div class="slick-slider slick-initialized">
<div class="slick-list"> <div class="slick-list">
<div class="slick-track" style="width: 100%; left: 0%;"> <div class="slick-track" style="opacity: 1; transform: translate3d(0px, 0px, 0px);">
<div tabindex="-1" data-index="0" class="slick-slide slick-active slick-current" style="outline: none; width: 100%;"> <div tabindex="-1" data-index="0" aria-hidden="false" class="slick-slide slick-active slick-current" style="outline: none; width: 0px;">
<div> <div>
<div tabindex="-1" class="" style="width: 100%; display: inline-block;"></div> <div tabindex="-1" style="width: 100%; display: inline-block;"></div>
</div> </div>
</div> </div>
</div> </div>

@ -1,5 +1,6 @@
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import { asyncExpect } from '@/tests/utils'; import { h, createVNode } from 'vue';
import { asyncExpect, sleep } from '@/tests/utils';
import Carousel from '..'; import Carousel from '..';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
@ -15,13 +16,13 @@ describe('Carousel', () => {
it('should has innerSlider', () => { it('should has innerSlider', () => {
const props = { const props = {
slots: { slots: {
default: '<div />', default: () => h('div'),
}, },
sync: false, sync: false,
}; };
const wrapper = mount(Carousel, props); const wrapper = mount(Carousel, props);
const { innerSlider, $refs } = wrapper.vm; const { innerSlider, slick } = wrapper.vm;
const innerSliderFromRefs = $refs.slick.innerSlider; const innerSliderFromRefs = slick.innerSlider;
expect(innerSlider).toBe(innerSliderFromRefs); expect(innerSlider).toBe(innerSliderFromRefs);
expect(typeof innerSlider.slickNext).toBe('function'); expect(typeof innerSlider.slickNext).toBe('function');
}); });
@ -29,7 +30,11 @@ describe('Carousel', () => {
it('should has prev, next and go function', async () => { it('should has prev, next and go function', async () => {
const props = { const props = {
slots: { slots: {
default: '<div>1</div><div>2</div><div>3</div>', default: () => [
createVNode('div', null, '1'),
createVNode('div', null, '2'),
createVNode('div', null, '3'),
],
}, },
sync: false, sync: false,
}; };
@ -38,7 +43,7 @@ describe('Carousel', () => {
expect(typeof prev).toBe('function'); expect(typeof prev).toBe('function');
expect(typeof next).toBe('function'); expect(typeof next).toBe('function');
expect(typeof goTo).toBe('function'); expect(typeof goTo).toBe('function');
const slick = wrapper.vm.$refs.slick; const slick = wrapper.vm.slick;
expect(slick.innerSlider.currentSlide).toBe(0); expect(slick.innerSlider.currentSlide).toBe(0);
wrapper.vm.goTo(2); wrapper.vm.goTo(2);
@ -56,24 +61,29 @@ describe('Carousel', () => {
expect(slick.innerSlider.currentSlide).toBe(2); expect(slick.innerSlider.currentSlide).toBe(2);
}, 1000); }, 1000);
}); });
// TODO
// it('should trigger autoPlay after window resize', async () => {
// const props = {
// props: {
// autoplay: true,
// },
// slots: {
// default: () => [
// createVNode('div', null, '1'),
// createVNode('div', null, '2'),
// createVNode('div', null, '3'),
// ],
// },
// sync: false,
// };
// const wrapper = mount(Carousel, props);
it('should trigger autoPlay after window resize', async () => { // const spy = jest.spyOn(wrapper.vm.slick.innerSlider, 'handleAutoPlay');
const props = { // window.resizeTo(1000);
props: { // expect(spy).not.toHaveBeenCalled();
autoplay: true, // await new Promise(resolve => setTimeout(resolve, 1000));
}, // expect(spy).toHaveBeenCalled();
slots: { // });
default: '<div>1</div><div>2</div><div>3</div>',
},
sync: false,
};
const wrapper = mount(Carousel, props);
const spy = jest.spyOn(wrapper.vm.$refs.slick.innerSlider, 'handleAutoPlay');
window.resizeTo(1000);
expect(spy).not.toHaveBeenCalled();
await new Promise(resolve => setTimeout(resolve, 1000));
expect(spy).toHaveBeenCalled();
});
it('cancel resize listener when unmount', async () => { it('cancel resize listener when unmount', async () => {
const props = { const props = {
@ -81,7 +91,11 @@ describe('Carousel', () => {
autoplay: true, autoplay: true,
}, },
slots: { slots: {
default: '<div>1</div><div>2</div><div>3</div>', default: () => [
createVNode('div', null, '1'),
createVNode('div', null, '2'),
createVNode('div', null, '3'),
],
}, },
sync: false, sync: false,
}; };
@ -96,16 +110,20 @@ describe('Carousel', () => {
describe('should works for dotPosition', () => { describe('should works for dotPosition', () => {
['left', 'right', 'top', 'bottom'].forEach(dotPosition => { ['left', 'right', 'top', 'bottom'].forEach(dotPosition => {
it(dotPosition, () => { it(dotPosition, async () => {
const wrapper = mount({ const wrapper = mount(
render() { {
return ( render() {
<Carousel dotPosition={dotPosition}> return (
<div /> <Carousel dotPosition={dotPosition}>
</Carousel> <div />
); </Carousel>
);
},
}, },
}); { sync: false, attachTo: 'body' },
);
await sleep(100);
expect(wrapper.html()).toMatchSnapshot(); expect(wrapper.html()).toMatchSnapshot();
}); });
}); });

@ -173,7 +173,7 @@ const Carousel = {
}; };
return ( return (
<div class={className}> <div class={className}>
<SlickCarousel ref="slick" {...SlickCarouselProps} vSlots={$slots}></SlickCarousel> <SlickCarousel ref={this.saveSlick} {...SlickCarouselProps} vSlots={$slots}></SlickCarousel>
</div> </div>
); );
}, },

@ -4,7 +4,7 @@
</div> </div>
</template> </template>
<script> <script>
import demo from '../antdv-demo/docs/calendar/demo/index'; import demo from '../antdv-demo/docs/carousel/demo/position';
export default { export default {
components: { components: {

Loading…
Cancel
Save