2022-03-17 12:21:06 +08:00
|
|
|
import { nextTick, reactive } from 'vue'
|
2021-09-17 15:27:31 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-03-17 12:21:06 +08:00
|
|
|
import Carousel from '../src/carousel.vue'
|
|
|
|
import CarouselItem from '../src/carousel-item.vue'
|
2022-03-17 22:20:25 +08:00
|
|
|
|
|
|
|
import type { VueWrapper } from '@vue/test-utils'
|
2022-03-17 12:21:06 +08:00
|
|
|
import type { CarouselInstance } from '../src/carousel'
|
2020-09-15 10:50:32 +08:00
|
|
|
|
|
|
|
const wait = (ms = 100) =>
|
2021-09-04 19:29:28 +08:00
|
|
|
new Promise((resolve) => setTimeout(() => resolve(0), ms))
|
2020-09-15 10:50:32 +08:00
|
|
|
|
2022-03-17 22:20:25 +08:00
|
|
|
const generateCarouselItems = (count = 3, hasLabel = false) => {
|
2022-03-17 12:21:06 +08:00
|
|
|
const list = Array.from({ length: count }, (_, index) => index + 1)
|
2022-03-17 22:20:25 +08:00
|
|
|
return list.map((i) =>
|
|
|
|
hasLabel ? <CarouselItem key={i} label={i} /> : <CarouselItem key={i} />
|
|
|
|
)
|
2022-03-17 12:21:06 +08:00
|
|
|
}
|
2020-09-15 10:50:32 +08:00
|
|
|
|
|
|
|
describe('Carousel', () => {
|
2022-03-17 22:20:25 +08:00
|
|
|
let wrapper: VueWrapper<any>
|
|
|
|
|
|
|
|
const createComponent = (
|
|
|
|
props: any = {},
|
|
|
|
count?: number,
|
|
|
|
hasLabel?: boolean
|
|
|
|
) => {
|
|
|
|
return mount({
|
2022-03-17 12:21:06 +08:00
|
|
|
setup() {
|
|
|
|
return () => (
|
|
|
|
<div>
|
2022-03-17 22:20:25 +08:00
|
|
|
<Carousel {...props}>
|
|
|
|
{generateCarouselItems(count, hasLabel)}
|
|
|
|
</Carousel>
|
2022-03-17 12:21:06 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
})
|
2022-03-17 22:20:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.unmount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('create', () => {
|
|
|
|
wrapper = createComponent({
|
|
|
|
ref: 'carousel',
|
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
|
2022-03-17 12:21:06 +08:00
|
|
|
const carousel = wrapper.findComponent({ ref: 'carousel' })
|
|
|
|
.vm as CarouselInstance
|
|
|
|
expect(carousel.direction).toBe('horizontal')
|
2020-09-15 10:50:32 +08:00
|
|
|
expect(wrapper.findAll('.el-carousel__item').length).toEqual(3)
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('auto play', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
interval: 50,
|
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
await wait(10)
|
|
|
|
const items = wrapper.vm.$el.querySelectorAll('.el-carousel__item')
|
|
|
|
expect(items[0].classList.contains('is-active')).toBeTruthy()
|
|
|
|
await wait(60)
|
|
|
|
expect(items[1].classList.contains('is-active')).toBeTruthy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('initial index', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
autoplay: false,
|
|
|
|
'initial-index': 1,
|
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
await wait(10)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
wrapper.vm.$el
|
|
|
|
.querySelectorAll('.el-carousel__item')[1]
|
2021-09-04 19:29:28 +08:00
|
|
|
.classList.contains('is-active')
|
2020-09-15 10:50:32 +08:00
|
|
|
).toBeTruthy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('reset timer', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
interval: 500,
|
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
await nextTick()
|
|
|
|
const items = wrapper.vm.$el.querySelectorAll('.el-carousel__item')
|
|
|
|
await wrapper.trigger('mouseenter')
|
|
|
|
await nextTick()
|
|
|
|
expect(items[0].classList.contains('is-active')).toBeTruthy()
|
|
|
|
await wrapper.trigger('mouseleave')
|
|
|
|
await nextTick()
|
|
|
|
await wait(700)
|
|
|
|
expect(items[1].classList.contains('is-active')).toBeTruthy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('change', async (done) => {
|
2022-03-17 12:21:06 +08:00
|
|
|
const state = reactive({
|
|
|
|
val: -1,
|
|
|
|
oldVal: -1,
|
|
|
|
})
|
|
|
|
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
onChange(val: number, prevVal: number) {
|
|
|
|
state.val = val
|
|
|
|
state.oldVal = prevVal
|
2020-09-15 10:50:32 +08:00
|
|
|
},
|
2022-03-17 22:20:25 +08:00
|
|
|
interval: 50,
|
2022-03-17 12:21:06 +08:00
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
await wait(50)
|
2022-03-17 12:21:06 +08:00
|
|
|
expect(state.val).toBe(1)
|
|
|
|
expect(state.oldVal).toBe(0)
|
2020-09-15 10:50:32 +08:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('label', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent(undefined, 3, true)
|
2020-09-15 10:50:32 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.find('.el-carousel__button span').text()).toBe('1')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('manual control', () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
it('hover', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
autoplay: false,
|
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
await wait()
|
|
|
|
await wrapper.findAll('.el-carousel__indicator')[1].trigger('mouseenter')
|
|
|
|
await nextTick()
|
|
|
|
await wait()
|
|
|
|
expect(
|
|
|
|
wrapper.vm.$el
|
|
|
|
.querySelectorAll('.el-carousel__item')[1]
|
2021-09-04 19:29:28 +08:00
|
|
|
.classList.contains('is-active')
|
2020-09-15 10:50:32 +08:00
|
|
|
).toBeTruthy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('card', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent(
|
|
|
|
{
|
|
|
|
autoplay: false,
|
|
|
|
type: 'card',
|
|
|
|
},
|
|
|
|
7
|
|
|
|
)
|
|
|
|
|
2020-09-15 10:50:32 +08:00
|
|
|
await nextTick()
|
|
|
|
await wait()
|
|
|
|
const items = wrapper.vm.$el.querySelectorAll('.el-carousel__item')
|
|
|
|
expect(items[0].classList.contains('is-active')).toBeTruthy()
|
|
|
|
expect(items[1].classList.contains('is-in-stage')).toBeTruthy()
|
|
|
|
expect(items[6].classList.contains('is-in-stage')).toBeTruthy()
|
|
|
|
await items[1].click()
|
|
|
|
await wait()
|
|
|
|
expect(items[1].classList.contains('is-active')).toBeTruthy()
|
|
|
|
await wrapper.vm.$el.querySelector('.el-carousel__arrow--left').click()
|
|
|
|
await wait()
|
|
|
|
expect(items[0].classList.contains('is-active')).toBeTruthy()
|
|
|
|
await items[6].click()
|
|
|
|
await wait()
|
|
|
|
expect(items[6].classList.contains('is-active')).toBeTruthy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('vertical direction', () => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
ref: 'carousel',
|
|
|
|
autoplay: false,
|
|
|
|
direction: 'vertical',
|
|
|
|
height: '100px',
|
2022-03-17 12:21:06 +08:00
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
const items = wrapper.vm.$el.querySelectorAll('.el-carousel__item')
|
2022-03-17 12:21:06 +08:00
|
|
|
const carousel = wrapper.findComponent({ ref: 'carousel' })
|
|
|
|
.vm as CarouselInstance
|
|
|
|
expect(carousel.direction).toBe('vertical')
|
2022-03-08 14:03:32 +08:00
|
|
|
expect(items[0].style.transform.includes('translateY')).toBeTruthy()
|
2020-09-15 10:50:32 +08:00
|
|
|
})
|
2021-02-08 16:23:48 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('pause auto play on hover', async (done) => {
|
2022-03-17 22:20:25 +08:00
|
|
|
wrapper = createComponent({
|
|
|
|
interval: 50,
|
|
|
|
'pause-on-hover': false,
|
|
|
|
})
|
2021-02-08 16:23:48 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
await wrapper.find('.el-carousel').trigger('mouseenter')
|
|
|
|
const items = wrapper.vm.$el.querySelectorAll('.el-carousel__item')
|
2021-02-27 16:03:06 +08:00
|
|
|
await nextTick()
|
2021-02-08 16:23:48 +08:00
|
|
|
await wait(60)
|
|
|
|
expect(items[1].classList.contains('is-active')).toBeTruthy()
|
|
|
|
done()
|
|
|
|
})
|
2020-09-15 10:50:32 +08:00
|
|
|
})
|