|
1 require("../env"); |
|
2 require("../../d3"); |
|
3 |
|
4 var vows = require("vows"), |
|
5 assert = require("assert"); |
|
6 |
|
7 var suite = vows.describe("d3.timer"); |
|
8 |
|
9 suite.addBatch({ |
|
10 "timer": { |
|
11 topic: function() { |
|
12 return d3.timer; |
|
13 }, |
|
14 "with no delay": { |
|
15 topic: timer(), |
|
16 "first calls after 17 ms or less": function(info) { |
|
17 assert.inDelta(info.start - info.scheduled, 17, 20); |
|
18 }, |
|
19 "calls until the function returns true": function(info) { |
|
20 assert.equal(info.count, 4); |
|
21 }, |
|
22 "calls every 17 ms": function(info) { |
|
23 assert.inDelta(info.stop - info.start, 17 * 3, 20); |
|
24 } |
|
25 }, |
|
26 "with a specified delay": { |
|
27 topic: timer(250), |
|
28 "first calls after the delay": function(info) { |
|
29 assert.inDelta(info.start - info.scheduled, 250, 20); |
|
30 }, |
|
31 "calls until the function returns true": function(info) { |
|
32 assert.equal(info.count, 4); |
|
33 }, |
|
34 "calls every 17 ms": function(info) { |
|
35 assert.inDelta(info.stop - info.start, 17 * 3, 20); |
|
36 } |
|
37 } |
|
38 } |
|
39 }); |
|
40 |
|
41 function timer(delay) { |
|
42 var args = Array.prototype.slice.call(arguments); |
|
43 return function() { |
|
44 var cb = this.callback, |
|
45 info = {scheduled: Date.now(), count: 0}; |
|
46 |
|
47 args.unshift(function() { |
|
48 var count = ++info.count; |
|
49 if (count === 1) { |
|
50 info.start = Date.now(); |
|
51 } else if (count === 4) { |
|
52 info.stop = Date.now(); |
|
53 cb(null, info); |
|
54 return true; |
|
55 } |
|
56 }); |
|
57 |
|
58 d3.timer.apply(this, args); |
|
59 }; |
|
60 } |
|
61 |
|
62 suite.export(module); |