avocado/packages/core/test/ticking-promise.js
2022-03-14 15:07:47 -05:00

56 lines
1.3 KiB
JavaScript

import {assert} from 'chai';
import TickingPromise from '../src/ticking-promise';
const createTickingPromise = (duration) => new TickingPromise(
() => {},
(elapsed, resolve) => {
// eslint-disable-next-line no-param-reassign
duration -= elapsed;
if (duration <= 0) {
resolve();
}
},
);
describe('TickingPromise', () => {
it('can behave as a promise', async () => {
try {
await new TickingPromise((resolve) => {
resolve();
});
}
catch (error) {
assert.fail('TickingPromise threw');
}
try {
await new TickingPromise((resolve, reject) => {
reject(new Error());
});
}
catch (error) {
return;
}
assert.fail("TickingPromise didn't throw");
});
it('accepts a ticker', async () => {
const promise = createTickingPromise(1);
for (let i = 0; i < 4; ++i) {
promise.tick(0.3);
}
await promise;
});
it('implements a compatible .all', async () => {
await TickingPromise.all([
new Promise((resolve) => {
resolve();
}),
]);
const promise = createTickingPromise(1);
const allPromise = TickingPromise.all([promise]);
for (let i = 0; i < 4; ++i) {
allPromise.tick(0.3);
}
await allPromise;
});
});