-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettle.test.ts
66 lines (56 loc) · 1.89 KB
/
settle.test.ts
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
import test from "node:test";
import { ok, equal, deepEqual } from "node:assert/strict";
import Future from "./index.js";
test("settle should return the result of all fullfilled Futures", async () => {
const a = Future.settle(
Future.from<string, never>((ok) => setTimeout(() => ok("foo"), 10)),
Future.from<boolean, never>((ok) => setTimeout(() => ok(false), 20)),
Future.fail(3),
);
return a.then((r) =>
deepEqual(r, [{ ok: "foo" }, { ok: false }, { err: 3 }]),
);
});
test("a single array argument should be treated as a list of futures for the settle function", async () => {
const a = Future.settle([
Future.from<string, never>((ok) => setTimeout(() => ok("foo"), 10)),
Future.from<boolean, never>((ok) => setTimeout(() => ok(false), 20)),
Future.fail(3),
]);
return a.then((r) =>
deepEqual(r, [{ ok: "foo" }, { ok: false }, { err: 3 }]),
);
});
test("a single iterable argument should be treated as a list of futures for the settle function", async () => {
const foo = Object.assign(() => {}, {
*[Symbol.iterator]() {
yield* [
Future.from<string, never>((ok) => setTimeout(() => ok("foo"), 10)),
Future.from<boolean, never>((ok) => setTimeout(() => ok(false), 20)),
Future.fail(3),
];
},
});
const a = Future.settle(foo);
return a.then((r) =>
deepEqual(r, [{ ok: "foo" }, { ok: false }, { err: 3 }]),
);
});
test("should treat an arrayLike as non-iterable value", async () => {
const a = Future.settle({
0: Future.of(""),
1: Future.of(2),
2: Future.of([true]),
length: 3,
});
return a.then((result) => {
ok(Array.isArray(result));
equal(result.length, 1);
equal(typeof result[0].ok, "object");
ok("length" in result[0].ok);
equal(result[0].ok.length, 3);
ok(Future.is(result[0].ok[0]));
ok(Future.is(result[0].ok[1]));
ok(Future.is(result[0].ok[2]));
});
});