-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.test.js
50 lines (43 loc) · 1.62 KB
/
rpc.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
42
43
44
45
46
47
48
49
50
const {expect} = require("chai");
const {RPCClient, RPCService} = require("./rpc");
const {JSONPipe} = require("./json-pipe");
describe("Given an RPC mechanism", function () {
describe("When a registered call is invoked", function () {
it("calls the target", async function () {
const clientToService = new JSONPipe();
const serviceToClient = new JSONPipe();
const client = new RPCClient(serviceToClient, clientToService);
const service = new RPCService(clientToService, serviceToClient);
let called = false;
service.register("hello-world", () => called = true);
await client.call("hello-world");
expect(called).to.eq(true);
});
it("returns the target value", async function(){
const clientToService = new JSONPipe();
const serviceToClient = new JSONPipe();
const client = new RPCClient(serviceToClient, clientToService);
const service = new RPCService(clientToService, serviceToClient);
const greeting = "yo!";
service.register("hello-world", () => greeting);
const result = await client.call("hello-world");
expect(result).to.eq(greeting);
});
});
describe("When an unregistered call is invoked", function(){
it("raises an error", async function () {
const clientToService = new JSONPipe();
const serviceToClient = new JSONPipe();
const client = new RPCClient(serviceToClient, clientToService);
const service = new RPCService(clientToService, serviceToClient);
service.register("hello-world", () => {throw new Error("no")});
let error;
try {
await client.call("hello-world")
}catch(e){
error = e;
}
expect(error.name).to.eq("Error");
});
});
});