Skip to content

Commit a1883c5

Browse files
committed
- optimizing code and adding tests
1 parent 83c280f commit a1883c5

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/client-proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ export class JSONRPCClient extends ClientProxy {
4343
jsonrpc: "2.0",
4444
id: ++id
4545
})
46-
.then(res => Promise.resolve({ jsonrpc, result: res, id }))
46+
.then(res => ({ jsonrpc, result: res, id }))
4747
.catch(err => {
4848
const { code, message, data } = err.response.data;
4949
let resp = { code, message, data };
5050

51-
return Promise.resolve({ jsonrpc, error: resp, id });
51+
return { jsonrpc, error: resp, id };
5252
});
5353
};
5454
}

src/index.spec.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,34 @@ describe("json-rpc-e2e", () => {
3737
.then(res => expect(res.result.data).toStrictEqual({ data: "hi" }));
3838
});
3939

40-
it(`should return an error from JSONRPCClient call`, () => {
40+
it(`should check error object properties from JSONRPCClient call`, () => {
41+
const jsonRpcErrorObj = {
42+
id: expect.stringMatching,
43+
jsonrpc: "",
44+
error: {
45+
message: "",
46+
code: 403,
47+
data: {
48+
fromService: "",
49+
params: { data: "" }
50+
}
51+
}
52+
};
53+
return service.testError({ data: "hi" }).then(res => {
54+
expect(res).toHaveProperty("id");
55+
expect(res).toHaveProperty("jsonrpc");
56+
expect(res).toHaveProperty("error");
57+
});
58+
});
59+
60+
it(`should return an error and check error data from JSONRPCClient call`, () => {
4161
const errorObj = {
4262
message: "RPC EXCEPTION",
4363
code: 403,
4464
data: {
4565
fromService: "Test Service",
4666
params: { data: "hi" }
47-
},
48-
id: 2
67+
}
4968
};
5069

5170
return service.testError({ data: "hi" }).then(res => expect(res.error).toStrictEqual(errorObj));

0 commit comments

Comments
 (0)