Sitelet https://github.com/open-rpc/client-js/commit/586ef40
Skip to content

Commit 586ef40

Browse files
committed
fix: mocking
1 parent fd457a9 commit 586ef40

3 files changed

Lines changed: 27 additions & 25 deletions

File tree

‎src/RequestManager.test.ts‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import EventEmitterTransport from "./transports/EventEmitterTransport";
33
import { EventEmitter } from "events";
44

55
describe("client-js", () => {
6+
67
it("can be constructed", () => {
78
const emitter = new EventEmitter();
89
const transport = new EventEmitterTransport(emitter, "from1", "to1");
@@ -32,17 +33,15 @@ describe("client-js", () => {
3233
c.close();
3334
});
3435

35-
it("can send a request", (done) => {
36+
it("can send a request", async () => {
3637
const emitter = new EventEmitter();
3738
const transport = new EventEmitterTransport(emitter, "from1", "to1");
3839
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
3940
const c = new RequestManager([transport]);
40-
c.connect().then(() => {
41-
c.request("foo", []).then(() => {
42-
done();
43-
});
44-
serverTransport.sendData(JSON.stringify({ id: 0, result: { foo: "foofoo" } }));
45-
});
41+
await c.connect();
42+
const reqPromise = c.request("foo", []);
43+
serverTransport.sendData(JSON.stringify({ id: 0, result: { foo: "foofoo" } }));
44+
await expect(reqPromise).resolves.toEqual({ foo: "foofoo" });
4645
});
4746

4847
it("can error on malformed response", (done) => {
@@ -63,9 +62,8 @@ describe("client-js", () => {
6362
const emitter = new EventEmitter();
6463
const transport = new EventEmitterTransport(emitter, "from1", "to1");
6564
const c = new RequestManager([transport]);
66-
return c.connect().then(() => {
67-
expect(() => c.stopBatch()).toThrow();
68-
});
65+
await c.connect();
66+
expect(() => c.stopBatch()).toThrow();
6967
});
7068

7169
it("can return errors on batch requests", (done) => {

‎src/RequestManager.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,27 @@ class RequestManager {
4646

4747
public connect(): Promise<any> {
4848
return Promise.all(this.transports.map(async (transport) => {
49-
await transport.connect();
5049
transport.onData(this.onData.bind(this));
50+
await transport.connect();
5151
}));
5252
}
5353

5454
public async request(method: string, params: any): Promise<any> {
5555
const i = (++this.lastId).toString();
56+
57+
// naively grab first transport and use it
58+
const transport = this.transports[0];
59+
60+
const payload: IJSONRPCRequest = {
61+
jsonrpc: "2.0",
62+
id: i,
63+
method,
64+
params,
65+
};
66+
5667
return new Promise((resolve, reject) => {
57-
// naively grab first transport and use it
58-
const transport = this.transports[0];
59-
this.requests[i] = {
60-
resolve,
61-
reject,
62-
};
63-
64-
const payload: IJSONRPCRequest = {
65-
jsonrpc: "2.0",
66-
id: i,
67-
method,
68-
params,
69-
};
68+
this.requests[i] = { resolve, reject };
69+
7070
if (this.batchStarted) {
7171
this.batch.push(payload);
7272
} else {
@@ -124,7 +124,7 @@ class RequestManager {
124124
} else if (response.result) {
125125
promiseForResult.resolve(response.result);
126126
} else {
127-
promiseForResult.reject(new Error(`Malformed JSON-RPC response object: ${response}`));
127+
promiseForResult.reject(new Error(`Malformed JSON-RPC response object: ${JSON.stringify(response)}`));
128128
}
129129
});
130130
}

‎src/transports/EventEmitterTransport.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ class EventEmitterTransport implements ITransport {
1010
this.reqUri = reqUri;
1111
this.resUri = resUri;
1212
}
13+
1314
public connect(): Promise<any> {
1415
return Promise.resolve();
1516
}
17+
1618
public onData(callback: (data: string) => any) {
1719
this.connection.on(this.reqUri, (data: any) => {
1820
callback(data);
1921
});
2022
}
23+
2124
public sendData(data: string) {
2225
this.connection.emit(this.resUri, data);
2326
}
27+
2428
public close() {
2529
this.connection.removeAllListeners();
2630
}

0 commit comments

Comments
 (0)