-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
test.js
171 lines (145 loc) · 4.19 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {serial as test} from 'ava';
import timeSpan from 'time-span';
import inRange from 'in-range';
import currentlyUnhandled from 'currently-unhandled';
import {AbortController} from 'abort-controller';
import delay from '.';
const getCurrentlyUnhandled = currentlyUnhandled();
test('returns a resolved promise', async t => {
const end = timeSpan();
await delay(50);
t.true(inRange(end(), 30, 70), 'is delayed');
});
test('returns a rejected promise', async t => {
const end = timeSpan();
await t.throwsAsync(
delay.reject(50, {value: new Error('foo')}),
'foo'
);
t.true(inRange(end(), 30, 70), 'is delayed');
});
test('able to resolve a falsy value', async t => {
t.is(
await delay(50, {value: 0}),
0
);
});
test('able to reject a falsy value', async t => {
t.plan(1);
try {
await delay.reject(50, {value: false});
} catch (error) {
t.is(error, false);
}
});
test('delay defaults to 0 ms', async t => {
const end = timeSpan();
await delay();
t.true(end() < 30);
});
test('reject will cause an unhandledRejection if not caught', async t => {
const reason = new Error('foo');
const promise = delay.reject(0, {value: reason});
await delay(10);
t.deepEqual(getCurrentlyUnhandled(), [{
reason,
promise
}], 'Promise should be unhandled');
promise.catch(() => {});
await delay(10);
t.deepEqual(getCurrentlyUnhandled(), [], 'no unhandled rejections now');
});
test('can clear a delayed resolution', async t => {
const end = timeSpan();
const delayPromise = delay(1000, {value: 'success!'});
delayPromise.clear();
const success = await delayPromise;
t.true(end() < 30);
t.is(success, 'success!');
});
test('can clear a delayed rejection', async t => {
const end = timeSpan();
const delayPromise = delay.reject(1000, {value: new Error('error!')});
delayPromise.clear();
await t.throwsAsync(delayPromise, /error!/);
t.true(end() < 30);
});
test('resolution can be aborted with an AbortSignal', async t => {
const end = timeSpan();
const abortController = new AbortController();
setTimeout(() => abortController.abort(), 1);
await t.throwsAsync(
delay(1000, {signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
});
test('resolution can be aborted with an AbortSignal if a value is passed', async t => {
const end = timeSpan();
const abortController = new AbortController();
setTimeout(() => abortController.abort(), 1);
await t.throwsAsync(
delay(1000, {value: 123, signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
});
test('rejection can be aborted with an AbortSignal if a value is passed', async t => {
const end = timeSpan();
const abortController = new AbortController();
setTimeout(() => abortController.abort(), 1);
await t.throwsAsync(
delay.reject(1000, {value: new Error(), signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
});
test('rejects with AbortError if AbortSignal is already aborted', async t => {
const end = timeSpan();
const abortController = new AbortController();
abortController.abort();
await t.throwsAsync(
delay(1000, {signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
});
test('can create a new instance with fixed timeout methods', async t => {
const cleared = [];
const callbacks = [];
const custom = delay.createWithTimers({
clearTimeout(handle) {
cleared.push(handle);
},
setTimeout(callback, ms) {
const handle = Symbol('handle');
callbacks.push({callback, handle, ms});
return handle;
}
});
const first = custom(50, {value: 'first'});
t.is(callbacks.length, 1);
t.is(callbacks[0].ms, 50);
callbacks[0].callback();
t.is(await first, 'first');
const second = custom.reject(40, {value: 'second'});
t.is(callbacks.length, 2);
t.is(callbacks[1].ms, 40);
callbacks[1].callback();
try {
await second;
} catch (error) {
t.is(error, 'second');
}
const third = custom(60);
t.is(callbacks.length, 3);
t.is(callbacks[2].ms, 60);
third.clear();
t.is(cleared.length, 1);
t.is(cleared[0], callbacks[2].handle);
});
test('returns a promise that is resolved in a random range of time', async t => {
const end = timeSpan();
await delay.range(50, 150);
t.true(inRange(end(), 30, 170), 'is delayed');
});