mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 11:27:35 +00:00
LibJS/Tests: Add some basic tests for Array.fromAsync
This commit is contained in:
parent
fe4f2923f8
commit
ba758e9f4d
1 changed files with 60 additions and 0 deletions
|
@ -0,0 +1,60 @@
|
||||||
|
test("length is 1", () => {
|
||||||
|
expect(Array.fromAsync).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("normal behavior", () => {
|
||||||
|
function checkResult(promise) {
|
||||||
|
expect(promise).toBeInstanceOf(Promise);
|
||||||
|
let error = null;
|
||||||
|
let passed = false;
|
||||||
|
promise
|
||||||
|
.then(value => {
|
||||||
|
expect(value instanceof Array).toBeTrue();
|
||||||
|
expect(value[0]).toBe(0);
|
||||||
|
expect(value[1]).toBe(2);
|
||||||
|
expect(value[2]).toBe(4);
|
||||||
|
passed = true;
|
||||||
|
})
|
||||||
|
.catch(value => {
|
||||||
|
error = value;
|
||||||
|
});
|
||||||
|
runQueuedPromiseJobs();
|
||||||
|
expect(error).toBeNull();
|
||||||
|
expect(passed).toBeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
test("async from sync iterable no mapfn", () => {
|
||||||
|
const input = [0, Promise.resolve(2), Promise.resolve(4)].values();
|
||||||
|
const promise = Array.fromAsync(input);
|
||||||
|
checkResult(promise);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("from object of promises no mapfn", () => {
|
||||||
|
let promise = Array.fromAsync({
|
||||||
|
length: 3,
|
||||||
|
0: Promise.resolve(0),
|
||||||
|
1: Promise.resolve(2),
|
||||||
|
2: Promise.resolve(4),
|
||||||
|
});
|
||||||
|
checkResult(promise);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("async from sync iterable with mapfn", () => {
|
||||||
|
const input = [Promise.resolve(0), 1, Promise.resolve(2)].values();
|
||||||
|
const promise = Array.fromAsync(input, async element => element * 2);
|
||||||
|
checkResult(promise);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("from object of promises with mapfn", () => {
|
||||||
|
let promise = Array.fromAsync(
|
||||||
|
{
|
||||||
|
length: 3,
|
||||||
|
0: Promise.resolve(0),
|
||||||
|
1: Promise.resolve(1),
|
||||||
|
2: Promise.resolve(2),
|
||||||
|
},
|
||||||
|
async element => element * 2
|
||||||
|
);
|
||||||
|
checkResult(promise);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue