Skip to content

Commit a8c4016

Browse files
committed
Add CircleCI build and Jest (integration) test
1 parent 253c73f commit a8c4016

File tree

6 files changed

+10794
-109
lines changed

6 files changed

+10794
-109
lines changed

.circleci/config.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Javascript Node CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
- image: circleci/node:10.15.1
11+
12+
# Specify service dependencies here if necessary
13+
# CircleCI maintains a library of pre-built images
14+
# documented at https://circleci.com/docs/2.0/circleci-images/
15+
# - image: circleci/mongo:3.4.4
16+
17+
working_directory: ~/repo
18+
19+
steps:
20+
- checkout
21+
22+
# Download and cache dependencies
23+
- restore_cache:
24+
keys:
25+
- v1-dependencies-{{ checksum "package.json" }}
26+
# fallback to using the latest cache if no exact match is found
27+
- v1-dependencies-
28+
29+
- run:
30+
name: "npm install"
31+
command: |
32+
npm install
33+
34+
- save_cache:
35+
paths:
36+
- node_modules
37+
key: v1-dependencies-{{ checksum "package.json" }}
38+
39+
- run:
40+
name: Build & test
41+
command: |
42+
npm run cleantest

__tests__/ReludeFetch_test.re

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
open Jest;
2+
open Expect;
3+
open Relude.Js.Json.DSL;
4+
5+
[%raw "require('isomorphic-fetch')"];
6+
7+
module Error = ReludeFetch.Error;
8+
module Headers = ReludeFetch.Headers;
9+
10+
module ResultE =
11+
Relude.Result.WithError({
12+
type t = string;
13+
});
14+
15+
module IOE =
16+
Relude.IO.WithError({
17+
type t = Error.t(string);
18+
});
19+
20+
let (>>=) = IOE.Infix.(>>=);
21+
22+
module Data = {
23+
let baseURL = "https://jsonplaceholder.typicode.com";
24+
25+
module Todo = {
26+
type t = {
27+
userId: int,
28+
id: int,
29+
title: string,
30+
completed: bool,
31+
};
32+
33+
let todo1URL = baseURL ++ "/todos/1";
34+
35+
let make = (userId, id, title, completed) => {
36+
userId,
37+
id,
38+
title,
39+
completed,
40+
};
41+
42+
let decode: Js.Json.t => Belt.Result.t(t, string) =
43+
json => {
44+
make
45+
<$> JD.intFor("userId", json)
46+
<*> JD.intFor("id", json)
47+
<*> JD.stringFor("title", json)
48+
<*> JD.boolFor("completed", json)
49+
|> Relude.Result.fromValidation
50+
|> Relude.Result.mapError(_ => "Decode failed");
51+
};
52+
};
53+
};
54+
55+
describe("ReludeFetch integration tests", () => {
56+
test("sync sanity check", () =>
57+
expect(true) |> toBe(true)
58+
);
59+
60+
testAsync("async sanity check", onDone =>
61+
onDone(expect(true) |> toBe(true))
62+
);
63+
64+
testAsync("fetch sample TODO", onDone => {
65+
let expectedTodo: Data.Todo.t = {
66+
userId: 1,
67+
id: 1,
68+
title: "delectus aut autem",
69+
completed: false,
70+
};
71+
72+
ReludeFetch.fetchWith(
73+
~headers=
74+
Headers.authorizationBearer("mytoken")
75+
|> Headers.combine(Headers.acceptJson),
76+
Data.Todo.todo1URL,
77+
)
78+
>>= ReludeFetch.Response.Json.decode(Data.Todo.decode)
79+
|> Relude.IO.tap(json => Js.log(json))
80+
|> Relude.IO.unsafeRunAsync(
81+
fun
82+
| Ok(actualTodo) =>
83+
onDone(expect(actualTodo) |> toEqual(expectedTodo))
84+
| Error(error) =>
85+
onDone(fail("Failed: " ++ Error.show(e => e, error))),
86+
);
87+
});
88+
});

bsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"subdirs": true
88
},
99
{
10-
"dir": "examples",
10+
"dir": "__tests__",
1111
"subdirs": true,
1212
"type": "dev"
1313
}
@@ -22,6 +22,9 @@
2222
"bs-fetch",
2323
"relude"
2424
],
25+
"bs-dev-dependencies": [
26+
"@glennsl/bs-jest",
27+
],
2528
"warnings": {
2629
"error": "+101"
2730
},

examples/Demo.re

-60
This file was deleted.

0 commit comments

Comments
 (0)