1 | var test = require('tape')
|
---|
2 | , raf = require('./index.js')
|
---|
3 |
|
---|
4 | test('continues to emit events', function(t) {
|
---|
5 | t.plan(11)
|
---|
6 |
|
---|
7 | var start = new Date().getTime()
|
---|
8 | , times = 0
|
---|
9 |
|
---|
10 | raf(function tick(dt) {
|
---|
11 | t.ok(dt >= 0, 'time has passed: ' + dt)
|
---|
12 | if(++times == 10) {
|
---|
13 | var elapsed = (new Date().getTime() - start)
|
---|
14 | t.ok(elapsed >= 150, 'should take at least 9 frames worth of wall time: ' + elapsed)
|
---|
15 | t.end()
|
---|
16 | } else {
|
---|
17 | raf(tick)
|
---|
18 | }
|
---|
19 | })
|
---|
20 | })
|
---|
21 |
|
---|
22 | test('cancel removes callbacks from queue', function(t) {
|
---|
23 | t.plan(6)
|
---|
24 |
|
---|
25 | function cb1() { cb1.called = true }
|
---|
26 | function cb2() { cb2.called = true }
|
---|
27 | function cb3() { cb3.called = true }
|
---|
28 |
|
---|
29 | var handle1 = raf(cb1)
|
---|
30 | t.ok(handle1, 'returns a handle')
|
---|
31 | var handle2 = raf(cb2)
|
---|
32 | t.ok(handle2, 'returns a handle')
|
---|
33 | var handle3 = raf(cb3)
|
---|
34 | t.ok(handle3, 'returns a handle')
|
---|
35 |
|
---|
36 | raf.cancel(handle2)
|
---|
37 |
|
---|
38 | raf(function() {
|
---|
39 | t.ok(cb1.called, 'callback was invoked')
|
---|
40 | t.notOk(cb2.called, 'callback was cancelled')
|
---|
41 | t.ok(cb3.called, 'callback was invoked')
|
---|
42 | t.end()
|
---|
43 | })
|
---|
44 | })
|
---|
45 |
|
---|
46 | test('raf should throw on errors', function(t) {
|
---|
47 | t.plan(1)
|
---|
48 |
|
---|
49 | var onError = function() {
|
---|
50 | t.pass('error bubbled up to event loop')
|
---|
51 | }
|
---|
52 |
|
---|
53 | if(typeof window !== 'undefined') {
|
---|
54 | window.onerror = onError
|
---|
55 | } else if(typeof process !== 'undefined') {
|
---|
56 | process.on('uncaughtException', onError)
|
---|
57 | }
|
---|
58 |
|
---|
59 | raf(function() {
|
---|
60 | throw new Error('foo')
|
---|
61 | })
|
---|
62 | })
|
---|