-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
92 lines (65 loc) · 1.64 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import ava from 'ava';
import timeSpan from 'time-span';
import inRange from 'in-range';
import Fn from './';
const test = ava.serial;
function fixture() {
return Promise.resolve(1337);
}
function nonPromiseFixture() {
return 1337;
}
function argumentAcceptingFixture(previousValue) {
return previousValue * 2;
}
test('not delaying first function', async t => {
const queue = new Fn();
const end = timeSpan();
await queue.up(fixture);
t.true(inRange(end(), 10));
});
test('delaying second function', async t => {
const queue = new Fn(100);
const end = timeSpan();
queue.up(fixture);
await queue.up(fixture);
t.true(inRange(end(), 90, 110));
});
test('delaying third function', async t => {
const queue = new Fn(100);
const end = timeSpan();
queue.up(fixture);
queue.up(fixture);
await queue.up(fixture);
t.true(inRange(end(), 190, 210));
});
test(`keeping resolved value`, async t => {
const queue = new Fn(100);
const res = await queue.up(fixture);
t.is(res, 1337);
});
test('resolving non-promises', async t => {
const queue = new Fn(100);
const res = await queue.up(nonPromiseFixture);
t.is(res, 1337);
});
test('queue from array', async t => {
const queue = new Fn(100);
const end = timeSpan();
const res = await queue.all([
fixture,
fixture,
nonPromiseFixture,
nonPromiseFixture
]);
t.same(res, [1337, 1337, 1337, 1337]);
t.true(inRange(end(), 290, 310));
});
test('passing initialValue', async t => {
const queue = new Fn(100, 1337);
const end = timeSpan();
queue.up(argumentAcceptingFixture);
const res = await queue.up(argumentAcceptingFixture);
t.is(res, 5348);
t.true(inRange(end(), 90, 110));
});