-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
106 lines (97 loc) · 2.55 KB
/
test.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
/* eslint-disable no-shadow */
const assert = require('assert')
const onresize = require('./index')
let w, h, detached = {}, frame = 0, init = 0
function length(n) {
return n == null ? ++init : n
}
function width(n) {
return w = length(n)
}
function height(n) {
return h = length(n)
}
function success(msg) {
console.log('Test Passed! \t' + msg)
}
// test 1
function mockElement() {
frame = 0
return {
get offsetWidth() { return width() },
get offsetHeight() { return height() }
}
}
mockElement.message = 'basic test, callback should called in the exact frame.'
// test2
function mockElement2(f) {
frame = 0
let w = 0, h = 0, t = ~~(Math.random() * (f - 1))
return {
get offsetWidth() {
// size only changed before penultimate frame.
if (frame % f === t) {
w = width()
h = height()
} else {
width()
height()
}
return w
},
get offsetHeight() { return h }
}
}
mockElement2.message = 'even size only changed before the penultimate in frame cycle, '
+ 'callback should be also called at the last frame(size equals to penultimate frame) in frame cycle.'
// @ts-ignore
global.window = {
requestAnimationFrame(cb) {
const id = length()
setTimeout(() => {
if (detached[id]) return
delete detached[id]
frame++
// @ts-ignore
cb()
}, 10)
return id
},
cancelAnimationFrame(id) {
detached[id] = true
}
}
function flow(...calls) {
if (!calls.length) return
const cancel = calls.shift()()
setTimeout(() => {
cancel()
success(calls.shift())
flow(...calls)
}, 2000)
}
flow(
// @ts-ignore
() => onresize(mockElement(), (size) => {
assert.equal(w, size.width, 'expect callback size param to be the last size.')
assert.equal(h, size.height, 'expect callback size param to be the last size.')
assert.equal(frame % 30, 0, 'called in unexpected frame: ' + frame)
}),
mockElement.message,
// @ts-ignore
() => onresize(mockElement2(20), { frame: 20 }, (size) => {
assert.notEqual(w, size.width, 'unexpected size')
assert.notEqual(h, size.height, 'unexpected size')
assert.equal(frame % 20, 0, 'called in unexpected frame: ' + frame)
}),
mockElement2.message,
// @ts-ignore
() => onresize(mockElement(), {
getSize: (ele) => ele.offsetWidth + ele.offsetHeight,
equal: (a, b) => a === b
}, (size) => {
assert.equal(w + h, size, 'expect callback size param to be the last size.')
assert.equal(frame % 30, 0, 'called in unexpected frame: ' + frame)
}),
'custom `getSize` and `equal`.'
)