forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelay.js
More file actions
67 lines (52 loc) · 1.41 KB
/
delay.js
File metadata and controls
67 lines (52 loc) · 1.41 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
import assert from 'assert';
import { slice } from './utils.js';
import delay from '../delay.js';
describe('delay', function() {
it('should delay `func` execution', function(done) {
var pass = false;
delay(function() { pass = true; }, 32);
setTimeout(function() {
assert.ok(!pass);
}, 1);
setTimeout(function() {
assert.ok(pass);
done();
}, 64);
});
it('should provide additional arguments to `func`', function(done) {
var args;
delay(function() {
args = slice.call(arguments);
}, 32, 1, 2);
setTimeout(function() {
assert.deepStrictEqual(args, [1, 2]);
done();
}, 64);
});
it('should use a default `wait` of `0`', function(done) {
var pass = false;
delay(function() { pass = true; });
assert.ok(!pass);
setTimeout(function() {
assert.ok(pass);
done();
}, 0);
});
it('should be cancelable', function(done) {
var pass = true,
timerId = delay(function() { pass = false; }, 32);
clearTimeout(timerId);
setTimeout(function() {
assert.ok(pass);
done();
}, 64);
});
it('should work with mocked `setTimeout`', function() {
var pass = false,
setTimeout = root.setTimeout;
setProperty(root, 'setTimeout', function(func) { func(); });
delay(function() { pass = true; }, 32);
setProperty(root, 'setTimeout', setTimeout);
assert.ok(pass);
});
});