-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
55 lines (49 loc) · 1.34 KB
/
test.js
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
'use strict';
require('mocha');
var assert = require('assert');
var alarm = require('./');
describe('alarm', function() {
it('should export a function', function() {
assert.equal(typeof alarm, 'function');
});
it('should throw an error when invalid args are passed', function(cb) {
try {
alarm();
cb(new Error('expected an error'));
} catch (err) {
assert(err);
assert.equal(err.message, 'expected second argument to be a function');
cb();
}
});
it('should execute the function after 1 second', function(cb) {
var now = new Date();
var date = new Date(+now + 1000);
alarm(date, function() {
assert.equal((new Date()).toString(), date.toString());
cb();
});
});
it('should cancel the alarm', function(cb) {
var now = new Date();
var date = new Date(+now + 1000);
var cancel = alarm(date, function() {
cb(new Error('expected the alarm to be cancelled'));
});
cancel();
cb();
});
it('should execute the function every second, 5 times', function(cb) {
this.timeout(6000);
var count = 0;
var cancel = alarm.recurring(1000, function() {
count++;
if (count >= 5) {
assert.equal(count, 5);
cancel();
return cb();
}
assert(count < 5, 'expected alarm to execute 5 times');
});
});
});