-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.test.js
41 lines (35 loc) · 955 Bytes
/
server.test.js
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
const test = require("ava");
const request = require("supertest");
const { rpcService } = require("./server.js");
test("exports a function", t => {
t.is(typeof rpcService, "function");
});
test("calling rpcService returns an express handler function", t => {
const handler = rpcService({
foo() {},
});
t.is(typeof handler, "function");
});
test("handler calls method with args, returns result as JSON", async t => {
t.plan(4);
const methods = {
greet(name, exclaim = false) {
t.is(name, "World");
const punctuation = exclaim ? "!": "."
const message = `Hello, ${name}` + punctuation;
return {
message,
};
},
};
const handler = rpcService(methods);
const res = await request(handler).post("/").send({
path: ["greet"],
args: ["World", true],
});
t.is(res.ok, true);
t.is(res.type, "application/json");
t.like(res.body, {
message: "Hello, World!",
});
});