scroll.js
3.35 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
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
import BScroll from 'scroll/index'
import { dispatchTouchStart, dispatchTouchMove, dispatchSwipe } from '../../utils/event'
describe('BScroll - core scroll', () => {
let scroll
let scroller
let scrollOptions = {
bindToWrapper: true,
probeType: 3,
deceleration: 0.001
}
beforeEach(() => {
const wrapper = document.createElement('div')
const list = document.createElement('ul')
scroller = document.createElement('div')
scroller.appendChild(list)
wrapper.appendChild(scroller)
document.body.appendChild(wrapper)
wrapper.style.height = '500px'
wrapper.style.overflow = 'hidden'
let listHTML = ''
list.style.margin = '0'
for (let i = 0; i < 100; i++) {
listHTML += `<li>${i}</li>`
}
list.innerHTML = listHTML
scroll = new BScroll(wrapper, scrollOptions)
})
afterEach(() => {
if (scroll) {
document.body.removeChild(scroll.wrapper)
scroll.destroy()
}
})
it('start', () => {
const wrapper = scroll.wrapper
const startHandler = sinon.spy()
scroll.on('beforeScrollStart', startHandler)
dispatchTouchStart(wrapper, {
pageX: 100,
pageY: 50
})
expect(startHandler)
.to.be.calledOnce
expect(scroll.pointX)
.to.equal(100)
expect(scroll.pointY)
.to.equal(50)
})
it('move', () => {
const wrapper = scroll.wrapper
const startHandler = sinon.spy()
const scrollHandler = sinon.spy()
scroll.on('scrollStart', startHandler)
scroll.on('scroll', scrollHandler)
dispatchTouchStart(wrapper, {
pageX: 100,
pageY: 100
})
dispatchTouchMove(wrapper, {
pageX: 100,
pageY: 50
})
expect(startHandler)
.to.be.calledOnce
expect(scrollHandler)
.to.be.calledOnce
})
it('end', (done) => {
const wrapper = scroll.wrapper
const endHandler = sinon.spy()
scroll.on('touchEnd', endHandler)
scroll.on('scrollEnd', () => {
done()
})
dispatchSwipe(wrapper, [
{
pageX: 100,
pageY: 100
},
{
pageX: 100,
pageY: 80
}
], 100, () => {
expect(endHandler)
.to.be.calledOnce
})
expect(scroll.x)
.to.equal(0)
expect(scroll.y)
.to.equal(0)
})
it('scrollStop', (done) => {
const endHandler = sinon.spy()
scroll.on('scrollEnd', endHandler)
scroll.scrollToElement(scroller, 100000, 0, 500)
setTimeout(() => {
scroll.stop()
expect(endHandler)
.to.be.calledOnce
done()
}, 5000)
})
it('scrollToElement', () => {
scroll.scrollToElement(scroller, 1000, 0, 500)
expect(scroll.y)
.to.equal(-500)
scroll.scrollToElement(scroller, 1000, 0, 400)
expect(scroll.y)
.to.equal(-400)
})
it('scrollBy', () => {
scroll.scrollBy(0, -300, 1000)
expect(scroll.y)
.to.equal(-300)
scroll.scrollBy(0, -200, 1000)
expect(scroll.y)
.to.equal(-500)
})
it('resize', (done) => {
const refreshHandle = sinon.spy()
scroll.on('refresh', refreshHandle)
if (document.createEvent) {
var event = document.createEvent('HTMLEvents')
event.initEvent('resize', true, true)
window.dispatchEvent(event)
} else if (document.createEventObject) {
window.fireEvent('onresize')
}
setTimeout(() => {
expect(refreshHandle)
.to.be.called
done()
}, 5000)
})
})