UNPKG

4.6 kBJavaScriptView Raw
1var test = require('tape');
2var rifraf = require('../index');
3
4function get5() {
5 var i = 0;
6 var l = 5;
7 var set = [];
8 for (; i < l; i++) {
9 set.push({index: i});
10 }
11 return set;
12}
13
14function now() { return new Date().getTime(); }
15
16if (rifraf.isNative) {
17 test('request defers approximately 16ms by default', function (t) {
18 t.plan(7);
19 rifraf.request(function (start) {
20 var set = get5();
21 var i = 0;
22
23 function tick(dt) {
24 var item = set[i++];
25 item.time = dt;
26 t.ok((dt - start) >= 0, 'time has passed: ' + (dt - start));
27
28 if (set[i]) {
29 rifraf.request(tick);
30 } else {
31 t.ok((item.time - start) > 5 * 16, 'should take at least 5 (80ms) frames of clock time; actual: ' + (item.time - start));
32 t.ok(set.every(function (item, index) {
33 var next = set[index + 1];
34 if (next) {
35 return item.time < next.time;
36 }
37 return true;
38 }), 'callbacks executed in order');
39 t.end();
40 }
41 }
42 rifraf.request(tick);
43 });
44 });
45} else {
46
47 test('request defers approximately 8ms by default', function (t) {
48 t.plan(2);
49 var set = get5();
50 var i = 0;
51 var start = now();
52
53 function tick() {
54 var item = set[i++];
55 item.time = now();
56
57 if (item.index === 4) {
58 t.ok((item.time - start) > 5 * 8, 'total time > 5 * 8ms; actual(' + (item.time - start) + ')');
59 t.ok(set.every(function (item, index) {
60 var next = set[index + 1];
61 if (next) {
62 return item.time < next.time;
63 }
64 return true;
65 }), 'callbacks executed in order');
66 t.end();
67 }
68
69 if (set[i]) rifraf.request(tick);
70 }
71 rifraf.request(tick);
72 });
73
74 test('request defers approximately 16ms when sync60Hz is called', function (t) {
75 rifraf.sync60Hz();
76 t.plan(2);
77 var set = get5();
78 var i = 0;
79 var start = now();
80
81 function tick() {
82 var item = set[i++];
83 item.time = now();
84 if (item.index === 4) {
85 t.ok((item.time - start) > 5 * 16, 'total time > 5 * 16ms; actual(' + (item.time - start) + ')');
86 t.ok(set.every(function (item, index) {
87 var next = set[index + 1];
88 if (next) {
89 return item.time < next.time;
90 }
91 return true;
92 }), 'callbacks executed in order');
93 t.end();
94 }
95
96 if (set[i]) rifraf.request(tick);
97 }
98 rifraf.request(tick);
99 });
100
101 test('request defers approximately 33ms when sync30Hz is called', function (t) {
102 rifraf.sync30Hz();
103 t.plan(2);
104 var set = get5();
105 var i = 0;
106 var start = now();
107
108 function tick() {
109 var item = set[i++];
110 item.time = now();
111 if (item.index === 4) {
112 t.ok((item.time - start) > 5 * 33, 'total time > 5 * 33ms; actual(' + (item.time - start) + ')');
113 t.ok(set.every(function (item, index) {
114 var next = set[index + 1];
115 if (next) {
116 return item.time < next.time;
117 }
118 return true;
119 }), 'callbacks executed in order');
120 t.end();
121 }
122
123 if (set[i]) rifraf.request(tick);
124 }
125 rifraf.request(tick);
126 });
127}
128
129test('callbacks can be cancelled', function (t) {
130
131 function one() { one.called = true; }
132 function two() { two.called = true; }
133 function three() { three.called = true; }
134
135 one.handle = rifraf.request(one);
136 two.handle = rifraf.request(two);
137 three.handle = rifraf.request(three);
138
139 t.ok([one, two, three].every(function (fn) {
140 return fn.handle;
141 }), 'rifraf.request returns a handle');
142
143 rifraf.cancel(two.handle);
144
145 rifraf.request(function () {
146 t.ok(one.called, 'callback one was called');
147 t.notOk(two.called, 'callback two was not called');
148 t.ok(three.called, 'callback three was called');
149 t.end();
150 });
151});
\No newline at end of file