Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
152 lines (112 loc) · 3.72 KB
/
Copy pathtest.js
File metadata and controls
152 lines (112 loc) · 3.72 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { test } = require('bare-tap')
const { Window, View } = require('bare-native')
const AbortController = require('bare-abort-controller')
const AnimationFrameProvider = require('.')
const { requestAnimationFrame, cancelAnimationFrame, afterAnimationFrame } = AnimationFrameProvider
// Frames are only drawn for a window on screen, and on Linux the default
// provider follows the active window.
const window = new Window(300, 200).content(new View()).show()
test('calls the callback with the time of the frame', async (t) => {
const time = await new Promise((resolve) => requestAnimationFrame(resolve))
t.equal(typeof time, 'number')
t.ok(time > 0)
})
test('calls the callbacks of a frame in order with the same time', async (t) => {
const calls = []
requestAnimationFrame((time) => calls.push(['a', time]))
requestAnimationFrame((time) => calls.push(['b', time]))
await afterAnimationFrame()
t.deepStrictEqual(
calls.map(([name]) => name),
['a', 'b']
)
t.equal(calls[0][1], calls[1][1])
})
test('defers callbacks requested within a frame to the next one', async (t) => {
const times = await new Promise((resolve) => {
requestAnimationFrame((first) => {
requestAnimationFrame((second) => resolve([first, second]))
})
})
t.ok(times[1] > times[0])
})
test('skips a callback cancelled earlier in the same frame', async (t) => {
let called = false
let request
requestAnimationFrame(() => request.cancel())
request = requestAnimationFrame(() => {
called = true
})
await afterAnimationFrame()
t.notOk(called)
})
test('takes back a request', async (t) => {
let calls = 0
requestAnimationFrame(() => calls++).cancel()
cancelAnimationFrame(requestAnimationFrame(() => calls++))
{
using request = requestAnimationFrame(() => calls++)
}
await afterAnimationFrame()
t.equal(calls, 0)
})
test('takes back a request once its signal is aborted', async (t) => {
const controller = new AbortController()
let called = false
requestAnimationFrame(
() => {
called = true
},
{ signal: controller.signal }
)
controller.abort()
await afterAnimationFrame()
t.notOk(called)
})
test('resolves without a callback', async (t) => {
const time = await requestAnimationFrame()
t.equal(typeof time, 'number')
})
test('rejects with the reason of an aborted signal', async (t) => {
const reason = new Error('Aborted')
const controller = new AbortController()
const pending = requestAnimationFrame({ signal: controller.signal })
controller.abort(reason)
const aborted = new AbortController()
aborted.abort(reason)
for (const promise of [pending, requestAnimationFrame({ signal: aborted.signal })]) {
try {
await promise
t.fail('Should have rejected')
} catch (err) {
t.equal(err, reason)
}
}
})
test('reports an error thrown by a callback after the rest have run', async (t) => {
const error = new Error('Thrown')
let called = false
const reported = new Promise((resolve) => Bare.once('uncaughtException', resolve))
requestAnimationFrame(() => {
throw error
})
requestAnimationFrame(() => {
called = true
})
t.equal(await reported, error)
t.ok(called)
})
test('rejects a callback that is not a function', (t) => {
t.throws(() => requestAnimationFrame(null), TypeError)
t.throws(() => afterAnimationFrame(1), TypeError)
})
test('waits for the frame after the next one', async (t) => {
const next = requestAnimationFrame()
const after = afterAnimationFrame()
t.ok((await after) > (await next))
})
test('follows a target', async (t) => {
const { requestAnimationFrame } = new AnimationFrameProvider(window.native)
const time = await requestAnimationFrame()
t.equal(typeof time, 'number')
})