2016-10-21 06:42:17 +00:00
|
|
|
import test from 'ava';
|
|
|
|
import delay from 'delay';
|
2017-11-19 09:33:44 +00:00
|
|
|
import PCancelable from 'p-cancelable';
|
2020-12-26 10:42:06 +00:00
|
|
|
import inRange from 'in-range';
|
|
|
|
import timeSpan from 'time-span';
|
2019-03-12 08:17:11 +00:00
|
|
|
import pTimeout from '.';
|
2016-10-21 06:42:17 +00:00
|
|
|
|
|
|
|
const fixture = Symbol('fixture');
|
2019-03-12 08:21:47 +00:00
|
|
|
const fixtureError = new Error('fixture');
|
2016-10-21 06:42:17 +00:00
|
|
|
|
|
|
|
test('resolves before timeout', async t => {
|
2019-03-12 08:17:11 +00:00
|
|
|
t.is(await pTimeout(delay(50).then(() => fixture), 200), fixture);
|
2016-10-21 06:42:17 +00:00
|
|
|
});
|
|
|
|
|
2019-03-12 08:21:47 +00:00
|
|
|
test('throws when milliseconds is not number', async t => {
|
2019-03-12 08:17:11 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(50), '200'), TypeError);
|
2017-11-19 09:27:16 +00:00
|
|
|
});
|
|
|
|
|
2019-03-12 08:21:47 +00:00
|
|
|
test('throws when milliseconds is negative number', async t => {
|
2019-03-12 08:17:11 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(50), -1), TypeError);
|
2017-11-19 09:27:16 +00:00
|
|
|
});
|
|
|
|
|
2019-09-17 15:09:28 +00:00
|
|
|
test('handles milliseconds being `Infinity`', async t => {
|
|
|
|
t.is(
|
|
|
|
await pTimeout(delay(50, {value: fixture}), Infinity),
|
|
|
|
fixture
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-10-21 06:42:17 +00:00
|
|
|
test('rejects after timeout', async t => {
|
2019-03-12 08:17:11 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(200), 50), pTimeout.TimeoutError);
|
2016-10-21 06:42:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('rejects before timeout if specified promise rejects', async t => {
|
2019-03-12 08:21:47 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(50).then(() => Promise.reject(fixtureError)), 200), fixtureError.message);
|
2016-10-21 06:42:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('fallback argument', async t => {
|
2019-03-12 08:17:11 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(200), 50, 'rainbow'), 'rainbow');
|
|
|
|
await t.throwsAsync(pTimeout(delay(200), 50, new RangeError('cake')), RangeError);
|
2019-03-12 08:21:47 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(200), 50, () => Promise.reject(fixtureError)), fixtureError.message);
|
2019-03-12 08:17:11 +00:00
|
|
|
await t.throwsAsync(pTimeout(delay(200), 50, () => {
|
2017-11-28 20:05:54 +00:00
|
|
|
throw new RangeError('cake');
|
|
|
|
}), RangeError);
|
2016-10-21 06:42:17 +00:00
|
|
|
});
|
2017-11-19 09:33:44 +00:00
|
|
|
|
|
|
|
test('calls `.cancel()` on promise when it exists', async t => {
|
2019-03-12 08:17:11 +00:00
|
|
|
const promise = new PCancelable(async (resolve, reject, onCancel) => {
|
2017-11-19 09:33:44 +00:00
|
|
|
onCancel(() => {
|
|
|
|
t.pass();
|
|
|
|
});
|
|
|
|
|
2019-03-12 08:17:11 +00:00
|
|
|
await delay(200);
|
|
|
|
resolve();
|
2017-11-19 09:33:44 +00:00
|
|
|
});
|
|
|
|
|
2019-03-12 08:17:11 +00:00
|
|
|
await t.throwsAsync(pTimeout(promise, 50), pTimeout.TimeoutError);
|
|
|
|
t.true(promise.isCanceled);
|
2017-11-19 09:33:44 +00:00
|
|
|
});
|
2020-12-01 15:40:53 +00:00
|
|
|
|
|
|
|
test('accepts `customTimers` option', async t => {
|
|
|
|
t.plan(2);
|
|
|
|
|
|
|
|
await pTimeout(delay(50), 123, undefined, {
|
|
|
|
customTimers: {
|
|
|
|
setTimeout(fn, milliseconds) {
|
|
|
|
t.is(milliseconds, 123);
|
|
|
|
return setTimeout(fn, milliseconds);
|
|
|
|
},
|
|
|
|
clearTimeout(timeoutId) {
|
|
|
|
t.pass();
|
|
|
|
return clearTimeout(timeoutId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-12-26 10:42:06 +00:00
|
|
|
|
|
|
|
test('`.clear()` method', async t => {
|
|
|
|
const end = timeSpan();
|
|
|
|
const promise = pTimeout(delay(300), 200);
|
|
|
|
|
|
|
|
promise.clear();
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
t.true(inRange(end(), {start: 0, end: 350}));
|
|
|
|
});
|