-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneOf.test.ts
64 lines (52 loc) · 1.9 KB
/
oneOf.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
import test from "node:test";
import { ok, equal, deepEqual } from "node:assert/strict";
import Future from "./index.js";
test("oneOf should return the result of first successfully fullfilled Future", async () => {
const a = Future.oneOf(
Future.from<string, never>((ok) => setTimeout(() => ok("foo"), 10)),
Future.from<boolean, never>((ok) => setTimeout(() => ok(false), 20)),
Future.fail(3),
);
return a.then((s) => equal(s, "foo"));
});
test("oneOf should return an array with errors if no Future is resolved successfully", async () => {
const a = Future.oneOf(Future.fail(""), Future.fail(true), Future.fail(3));
return a.catch((r) => deepEqual(r, ["", true, 3]));
});
test("a single array argument should be treated as a list of futures for the oneOf function", async () => {
const a = Future.oneOf([
Future.from<string, never>((ok) => setTimeout(() => ok("foo"), 10)),
Future.from<boolean, never>((ok) => setTimeout(() => ok(false), 20)),
Future.fail(3),
]);
return a.then((s) => equal(s, "foo"));
});
test("a single iterable argument should be treated as a list of futures for the oneOf 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.oneOf(foo);
return a.then((s) => equal(s, "foo"));
});
test("should treat an arrayLike as non-iterable value", async (t) => {
const a = Future.oneOf({
0: Future.of(""),
1: Future.of(2),
2: Future.of([true]),
length: 3,
});
return a.then((result) => {
equal(typeof result[0], "object");
ok("length" in result);
equal(result.length, 3);
ok(Future.is(result[0]));
ok(Future.is(result[1]));
ok(Future.is(result[2]));
});
});