-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarousel.js
263 lines (234 loc) · 8.84 KB
/
carousel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { mount } from '@vue/test-utils'
import Carousel from '@/components/Carousel.vue'
import { setTimeout } from 'core-js'
describe('Carousel.vue', () => {
it('renders props.listData when passed', done => {
const listData = ['a', 'b']
const wrapper = mount(Carousel, {
propsData: { listData },
scopedSlots: {
default: `
<template slot-scope="{ record, index }">
<div :id="'slot-' + index">
{{ record }}
</div>
</template>
`
}
})
listData.forEach((value, index) => {
expect(wrapper.find('#slot-' + index).text()).toMatch(value)
})
listData[2] = 'c'
wrapper.setProps({ listData: [ ...listData ] })
// wrapper.vm.listData = [ ...listData ]
wrapper.vm.$nextTick(() => {
expect(wrapper.find('#slot-2').text()).toMatch(listData[2])
wrapper.destroy()
done()
})
})
it('renders props.showIndicator when passed', () => {
const wrapper = mount(Carousel, {
propsData: { showIndicator: false }
})
expect(wrapper.find('.d-carousel-indicators').exists()).toBe(false)
})
it('renders props.autoPlay when passed', () => {
const wrapper = mount(Carousel, {
propsData: { autoPlay: false }
})
expect(wrapper.props().autoPlay).toBe(false)
})
it('renders props.autoPlayTime when passed', () => {
const wrapper = mount(Carousel, {
propsData: { autoPlayTime: 1500 }
})
expect(wrapper.props().autoPlayTime).toBe(1500)
})
it('renders props.inAnimate when passed', () => {
const wrapper = mount(Carousel, {
propsData: { inAnimate: 'fadeInRight' }
})
expect(wrapper.props().inAnimate).toBe('fadeInRight')
})
it('renders props.inAnimateOtherSide when passed', () => {
const wrapper = mount(Carousel, {
propsData: { inAnimateOtherSide: 'fadeInRight' }
})
expect(wrapper.props().inAnimateOtherSide).toBe('fadeInRight')
})
it('renders props.outAnimate when passed', () => {
const wrapper = mount(Carousel, {
propsData: { outAnimate: 'fadeInRight' }
})
expect(wrapper.props().outAnimate).toBe('fadeInRight')
})
it('renders props.outAnimateOtherSide when passed', () => {
const wrapper = mount(Carousel, {
propsData: { outAnimateOtherSide: 'fadeInRight' }
})
expect(wrapper.props().outAnimateOtherSide).toBe('fadeInRight')
})
it('renders props.speed when passed', done => {
const wrapper = mount(Carousel, {
propsData: { speed: 'faster', listData: [1, 2], autoPlay: false }
})
expect(wrapper.props().speed).toBe('faster')
wrapper.vm.goIndex(1)
wrapper.vm.$nextTick(() => {
expect(wrapper.findAll('.animated').at(1).classes('faster')).toBe(true)
done()
})
})
it('can play automatically', done => {
const wrapper = mount(Carousel, {
propsData: {
listData: [1, 2],
autoPlayTime: 100 // 设置低值,加快测试
}
})
const item0 = wrapper.findAll('.animated').at(0)
const item1 = wrapper.findAll('.animated').at(1)
expect(item0.classes('hide')).toBe(false)
expect(item1.classes('hide')).toBe(true)
setTimeout(() => {
// 自动播放
expect(item0.classes('fadeOut')).toBe(true)
expect(item1.classes('fadeIn')).toBe(true)
setTimeout(() => {
// 自动播放
expect(item0.classes('fadeIn')).toBe(true)
expect(item1.classes('fadeOut')).toBe(true)
// 取消自动播放
wrapper.setProps({ autoPlay: false })
setTimeout(() => {
// 播放已停止
expect(item0.classes('fadeIn')).toBe(true)
expect(item1.classes('fadeOut')).toBe(true)
// 恢复自动播放
wrapper.setProps({ autoPlay: true })
setTimeout(() => {
// 自动播放已恢复
expect(item0.classes('fadeOut')).toBe(true)
expect(item1.classes('fadeIn')).toBe(true)
done()
}, 610)
}, 610)
}, 610)
}, 610) // 500(动画时间) + 100(autoPlayTime)
})
it('mouseenter and mouseleave', done => {
const wrapper = mount(Carousel, {
propsData: { listData: [1, 2], autoPlayTime: 100, waitingTime: 0 }
})
const item0 = wrapper.findAll('.animated').at(0)
const item1 = wrapper.findAll('.animated').at(1)
expect(item0.classes('hide')).toBe(false)
expect(item1.classes('hide')).toBe(true)
// 鼠标移动进来,会触发停止自动播放
wrapper.trigger('mouseenter')
setTimeout(() => {
// 播放已停止
expect(item0.classes('fadeOut')).toBe(false)
expect(item1.classes('fadeIn')).toBe(false)
// 鼠标移动出去,会触发恢复自动播放
wrapper.trigger('mouseleave')
setTimeout(() => {
// 自动播放已恢复
expect(item0.classes('fadeOut')).toBe(true)
expect(item1.classes('fadeIn')).toBe(true)
done()
}, 101)
}, 101)
})
it('touch', done => {
const inAnimate = 'fadeInRight'
const outAnimate = 'fadeOutRight'
const wrapper = mount(Carousel, {
propsData: { listData: [1, 2, 3], autoPlayTime: 100, inAnimate, outAnimate, waitingTime: 1 }
})
const item0 = wrapper.findAll('.animated').at(0)
const item1 = wrapper.findAll('.animated').at(1)
const item2 = wrapper.findAll('.animated').at(2)
expect(item0.classes('hide')).toBe(false)
expect(item1.classes('hide')).toBe(true)
expect(item2.classes('hide')).toBe(true)
// 触摸移动开始,会触发停止自动播放
wrapper.trigger('touchstart', { changedTouches: [{ pageX: 100, pageY: 100 }] })
setTimeout(() => {
// 播放已停止
expect(item0.classes(outAnimate)).toBe(false)
expect(item1.classes(inAnimate)).toBe(false)
expect(item2.classes('hide')).toBe(true)
// 触摸移动结束,向左运动
wrapper.trigger('touchend', { changedTouches: [{ pageX: 1, pageY: 100 }] })
wrapper.vm.$nextTick(() => {
// 向左运动
expect(item0.classes(outAnimate)).toBe(true)
expect(item1.classes(inAnimate)).toBe(true)
expect(item2.classes('hide')).toBe(true)
setTimeout(() => {
// 触摸移动,向左(上)运动
wrapper.trigger('touchstart', { changedTouches: [{ pageX: 100, pageY: 100 }] })
wrapper.trigger('touchend', { changedTouches: [{ pageX: 100, pageY: 1 }] })
wrapper.vm.$nextTick(() => {
expect(item0.classes('hide')).toBe(true)
expect(item1.classes(outAnimate)).toBe(true)
expect(item2.classes(inAnimate)).toBe(true)
setTimeout(() => {
// 触摸移动,向右运动
wrapper.trigger('touchstart', { changedTouches: [{ pageX: 1, pageY: 100 }] })
wrapper.trigger('touchend', { changedTouches: [{ pageX: 100, pageY: 100 }] })
wrapper.vm.$nextTick(() => {
expect(item0.classes('hide')).toBe(true)
expect(item1.classes('fadeIn')).toBe(true)
expect(item2.classes('fadeOut')).toBe(true)
setTimeout(() => {
// 触摸移动,向右(下)运动
wrapper.trigger('touchstart', { changedTouches: [{ pageX: 100, pageY: 1 }] })
wrapper.trigger('touchend', { changedTouches: [{ pageX: 100, pageY: 100 }] })
wrapper.vm.$nextTick(() => {
expect(item0.classes('fadeIn')).toBe(true)
expect(item1.classes('fadeOut')).toBe(true)
expect(item2.classes('hide')).toBe(true)
setTimeout(() => {
// 自动播放已恢复
expect(item0.classes(outAnimate)).toBe(true)
expect(item1.classes(inAnimate)).toBe(true)
expect(item2.classes('hide')).toBe(true)
done()
}, 110)
})
}, 10)
})
}, 10)
})
}, 10)
})
}, 10)
})
it('goIndex', done => {
const wrapper = mount(Carousel, { propsData: { autoPlayTime: 100, waitingTime: 0 }})
wrapper.vm.goIndex(-1)
wrapper.vm.$nextTick(() => {
wrapper.setProps({ listData: [1,1,1] })
wrapper.vm.goIndex(0)
wrapper.vm.$nextTick(() => {
const item0 = wrapper.findAll('.animated').at(0)
const item1 = wrapper.findAll('.animated').at(1)
const item2 = wrapper.findAll('.animated').at(2)
expect(item0.classes('fadeIn')).toBe(true)
expect(item1.classes('hide')).toBe(true)
expect(item2.classes('hide')).toBe(true)
wrapper.vm.goIndex(2)
wrapper.vm.$nextTick(() => {
expect(item0.classes('fadeOut')).toBe(true)
expect(item1.classes('hide')).toBe(true)
expect(item2.classes('fadeIn')).toBe(true)
done()
})
})
})
})
})