wheel.js
2.55 KB
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
import BScroll from 'scroll/index'
describe('BScroll - wheel', () => {
let wheels = []
let wrapper = null
beforeEach(() => {
let style = document.createElement('style')
style.type = 'text/css'
const styleSheet = `
ul {
width: 100px;
list-style: none;
display: inline-block;
margin: 0;
}
.wheel-wrapper {
display: flex;
overflow: hidden;
}
.wheel {
flex: 1;
height: 173px;
}
.wheel-scroll {
padding: 0;
margin-top: 68px;
line-height: 36px;
list-style: none;
}
`
style.appendChild(document.createTextNode(styleSheet))
document.head.appendChild(style)
wrapper = document.createElement('div')
wrapper.className = 'wheel-wrapper'
const listArr = []
document.body.appendChild(wrapper)
for (let i = 0; i < 3; i++) {
let ulHTML = ''
const ul = document.createElement('ul')
ul.className = 'wheel-scroll'
const scroller = document.createElement('div')
scroller.className = 'wheel'
for (let i = 0; i < 100; i++) {
ulHTML += `<li class="wheel-item">${i}</li>`
}
ul.innerHTML = ulHTML
listArr.push(ul)
scroller.appendChild(ul)
wrapper.appendChild(scroller)
wheels[i] = new BScroll(wrapper.children[i], {
wheel: {
selectedIndex: 2
},
probeType: 3
})
}
})
afterEach(() => {
wheels = []
wrapper = null
document.body.removeChild(document.querySelector('.wheel-wrapper'))
})
it('the wheel wrapper class name should be use default value', () => {
const wheel = wheels[0]
expect(wheel.options.wheel.wheelWrapperClass).to.equal('wheel-scroll')
expect(wheel.options.wheel.wheelItemClass).to.equal('wheel-item')
})
it('wheelTo', () => {
const wheel = wheels[0]
wheel.wheelTo(4)
expect(wheel.getSelectedIndex())
.to.equal(4)
expect(wheel.y)
.to.equal(-1 * wheel.itemHeight * 4)
})
it('getSelectedIndex', () => {
const [firstWheel, secondWheel, thirdWheel] = [...wheels]
expect(firstWheel.getSelectedIndex())
.to.equal(2)
expect(secondWheel.getSelectedIndex())
.to.equal(2)
expect(thirdWheel.getSelectedIndex())
.to.equal(2)
})
it('it will report warning when wheel.getSelectedIndex is undefined', () => {
const firstWheel = new BScroll(wrapper.children[0], {
wheel: {
selectedIndex: undefined
},
probeType: 3
})
expect(firstWheel.options.startY).to.equal(0)
})
})