-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReludeFetch_test.re
88 lines (74 loc) · 1.91 KB
/
ReludeFetch_test.re
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
78
79
80
81
82
83
84
85
86
87
88
open Jest;
open Expect;
open Relude.Js.Json.DSL;
[%raw "require('isomorphic-fetch')"];
module Error = ReludeFetch.Error;
module Headers = ReludeFetch.Headers;
module ResultE =
Relude.Result.WithError({
type t = string;
});
module IOE =
Relude.IO.WithError({
type t = Error.t(string);
});
let (>>=) = IOE.Infix.(>>=);
module Data = {
let baseURL = "https://jsonplaceholder.typicode.com";
module Todo = {
type t = {
userId: int,
id: int,
title: string,
completed: bool,
};
let todo1URL = baseURL ++ "/todos/1";
let make = (userId, id, title, completed) => {
userId,
id,
title,
completed,
};
let decode: Js.Json.t => Belt.Result.t(t, string) =
json => {
make
<$> JD.intFor("userId", json)
<*> JD.intFor("id", json)
<*> JD.stringFor("title", json)
<*> JD.boolFor("completed", json)
|> Relude.Result.fromValidation
|> Relude.Result.mapError(_ => "Decode failed");
};
};
};
describe("ReludeFetch integration tests", () => {
test("sync sanity check", () =>
expect(true) |> toBe(true)
);
testAsync("async sanity check", onDone =>
onDone(expect(true) |> toBe(true))
);
testAsync("fetch sample TODO", onDone => {
let expectedTodo: Data.Todo.t = {
userId: 1,
id: 1,
title: "delectus aut autem",
completed: false,
};
ReludeFetch.fetchWith(
~headers=
Headers.authorizationBearer("mytoken")
|> Headers.combine(Headers.acceptJson),
Data.Todo.todo1URL,
)
>>= ReludeFetch.Response.Json.decode(Data.Todo.decode)
|> Relude.IO.tap(json => Js.log(json))
|> Relude.IO.unsafeRunAsync(
fun
| Ok(actualTodo) =>
onDone(expect(actualTodo) |> toEqual(expectedTodo))
| Error(error) =>
onDone(fail("Failed: " ++ Error.show(e => e, error))),
);
});
});