File: timeout-test.js

package info (click to toggle)
node-d3-timer 1.0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 224 kB
  • sloc: makefile: 13; sh: 2
file content (55 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (3)
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
var tape = require("tape"),
    timer = require("../"),
    end = require("./end");

require("./inRange");

tape("timeout(callback) invokes the callback once", function(test) {
  var count = 0;
  timer.timeout(function() {
    test.equal(++count, 1);
    end(test);
  });
});

tape("timeout(callback, delay) invokes the callback once after the specified delay", function(test) {
  var then = timer.now(), delay = 50;
  timer.timeout(function(elapsed) {
    test.inRange(timer.now() - then, delay - 10, delay + 1000);
    end(test);
  }, delay);
});

tape("timeout(callback, delay, time) invokes the callback once after the specified delay relative to the given time", function(test) {
  var then = timer.now() + 50, delay = 50;
  timer.timeout(function(elapsed) {
    test.inRange(timer.now() - then, delay - 10, delay + 1000);
    end(test);
  }, delay, then);
});

tape("timeout(callback) uses the global context for the callback", function(test) {
  timer.timeout(function() {
    test.equal(this, global);
    end(test);
  });
});

tape("timeout(callback) passes the callback the elapsed time", function(test) {
  var then = timer.now(), count = 0;
  timer.timeout(function(elapsed) {
    test.equal(elapsed, timer.now() - then);
    end(test);
  });
});

tape("timeout(callback) returns a timer", function(test) {
  var count = 0;
  var t = timer.timeout(function() { ++count; });
  test.equal(t instanceof timer.timer, true);
  t.stop();
  setTimeout(function() {
    test.equal(count, 0);
    end(test);
  }, 100);
});