2023-07-13 07:49:37 +00:00
|
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
2017-01-16 13:54:08 +00:00
|
|
|
|
2023-04-05 14:29:29 +00:00
|
|
|
import * as smartdelay from '../ts/index.js';
|
2017-01-16 13:54:08 +00:00
|
|
|
|
2021-06-04 12:11:27 +00:00
|
|
|
tap.test('.delayFor should delay async', async (tools) => {
|
2018-07-06 12:20:56 +00:00
|
|
|
tools.timeout(5000);
|
|
|
|
let timePassed = false;
|
2017-06-04 22:32:01 +00:00
|
|
|
setTimeout(() => {
|
2018-07-06 12:20:56 +00:00
|
|
|
timePassed = true;
|
|
|
|
}, 2000);
|
2017-06-04 22:32:01 +00:00
|
|
|
await smartdelay.delayFor(3000).then(async () => {
|
|
|
|
// tslint:disable-next-line:no-unused-expression
|
2023-04-05 14:29:29 +00:00
|
|
|
expect(timePassed).toBeTrue();
|
2018-07-06 12:20:56 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-04 22:32:01 +00:00
|
|
|
|
2021-06-04 12:11:27 +00:00
|
|
|
tap.test('.delayForRandom should delay async for a random time period', async (tools) => {
|
2019-11-03 16:11:24 +00:00
|
|
|
let timePassedBefore = false;
|
|
|
|
let timePassedAfter = false;
|
2017-10-09 09:33:59 +00:00
|
|
|
setTimeout(() => {
|
2019-11-03 16:11:24 +00:00
|
|
|
timePassedBefore = true;
|
2018-07-06 12:20:56 +00:00
|
|
|
}, 3000);
|
2019-11-03 16:11:24 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
timePassedAfter = true;
|
|
|
|
}, 5000);
|
|
|
|
await smartdelay.delayForRandom(3000, 4900);
|
2023-04-05 14:29:29 +00:00
|
|
|
expect(timePassedBefore).toBeTrue();
|
|
|
|
expect(timePassedAfter).toBeFalse();
|
2018-07-06 12:20:56 +00:00
|
|
|
});
|
2017-10-09 09:33:59 +00:00
|
|
|
|
2021-06-04 12:11:27 +00:00
|
|
|
tap.test('.delayFor should pass on a type', async (tools) => {
|
2018-07-06 12:20:56 +00:00
|
|
|
tools.timeout(5000);
|
|
|
|
let timePassed = false;
|
2017-06-04 22:32:01 +00:00
|
|
|
setTimeout(() => {
|
2018-07-06 12:20:56 +00:00
|
|
|
timePassed = true;
|
|
|
|
}, 2000);
|
|
|
|
let hey = 'heyThere';
|
2021-06-04 12:11:27 +00:00
|
|
|
await smartdelay.delayFor<string>(3000, hey).then(async (stringArg) => {
|
2023-04-05 14:29:29 +00:00
|
|
|
expect(stringArg).toEqual('heyThere');
|
2018-07-06 12:20:56 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-04 22:32:01 +00:00
|
|
|
|
|
|
|
tap.test('smartdelay.Timeout', async () => {
|
2018-07-06 12:20:56 +00:00
|
|
|
let timeout = new smartdelay.Timeout(2000);
|
|
|
|
await timeout.promise;
|
|
|
|
});
|
2017-06-04 22:32:01 +00:00
|
|
|
|
2021-06-04 12:11:27 +00:00
|
|
|
tap.test('smartdelay.Timeout should cancel', async (tools) => {
|
2018-07-06 12:20:56 +00:00
|
|
|
let timeout = new smartdelay.Timeout(60000);
|
|
|
|
timeout.cancel();
|
|
|
|
});
|
2017-06-04 22:32:01 +00:00
|
|
|
|
2018-07-06 12:20:56 +00:00
|
|
|
tap.start();
|