-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp_test.ts
77 lines (66 loc) · 2.76 KB
/
fp_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
67
68
69
70
71
72
73
74
75
76
77
import { pipe } from "fp-ts/lib/function";
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { add1, multiply2 } from "./util/fp/math.ts";
import { createGetPlanetsWithMoonsFp } from "./util/fp/getPlanetsWithMoonsFp.ts";
import { Either, fold } from "fp-ts/lib/Either";
import { identity } from "fp-ts/lib/function";
import { assertObjectMatch } from "https://deno.land/[email protected]/assert/assert_object_match.ts";
import { createGetPlanetsWithMoonsFpAlt } from "./util/fp/getPlanetsWithMoonFpAlt.ts";
// Deno.test("url test", () => {
// const url = new URL("./foo.js", "https://deno.land/");
// assertEquals(url.href, "https://deno.land/foo.js");
// });
Deno.test("👨🌾 can pipe add1 and multiply2", () => {
assertEquals(pipe(1, add1, multiply2), 4);
});
/**
* Extract value from either or throw (if left).
*/
const getRight = <A>(either: Either<Error, A>): A => {
return fold<Error, A, A>((e) => {
throw e;
}, identity)(either);
};
Deno.test("👨🌾 can get PlanetsWithMoons (Fp)", async () => {
const getWaypointsFp = createGetPlanetsWithMoonsFp(
// "https://httpstat.us/500"
"http://localhost:3000/waypointsForSystem.json"
);
const thunk = getWaypointsFp();
const resultEither = await thunk();
const result = getRight(resultEither);
// old
// assertObjectMatch(result, {
// data: [{ systemSymbol: "X1-JX88" }],
// meta: { total: 10, page: 1, limit: 10 },
// });
assertEquals(result.length, 3);
// console.log(result);
assertObjectMatch(result[0], { symbol: "X1-QB20-78791D" });
assertObjectMatch(result[1], { symbol: "X1-QB20-61050B" });
assertEquals(result[1].moons.length, 3);
assertObjectMatch(result[1].moons[0], { symbol: "X1-QB20-40572B" });
assertObjectMatch(result[1].moons[1], { symbol: "X1-QB20-88193B" });
assertObjectMatch(result[1].moons[2], { symbol: "X1-QB20-92514D" });
assertObjectMatch(result[2], { symbol: "X1-QB20-57458X" });
});
Deno.test("👨🌾 can get PlanetsWithMoons ALT (Fp)", async () => {
const getWaypointsFp = createGetPlanetsWithMoonsFpAlt(
"http://localhost:3000/waypointsForSystem.json"
);
const thunk = getWaypointsFp();
const resultEither = await thunk();
const result = getRight(resultEither);
// console.log(result);
assertEquals(result.length, 3);
/*
// console.log(result);
assertObjectMatch(result[0], { symbol: "X1-QB20-78791D" });
assertObjectMatch(result[1], { symbol: "X1-QB20-61050B" });
assertEquals(result[1].moons.length, 3);
assertObjectMatch(result[1].moons[0], { symbol: "X1-QB20-40572B" });
assertObjectMatch(result[1].moons[1], { symbol: "X1-QB20-88193B" });
assertObjectMatch(result[1].moons[2], { symbol: "X1-QB20-92514D" });
assertObjectMatch(result[2], { symbol: "X1-QB20-57458X" });
*/
});