You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

45 lines
1.3 KiB

import { AbstractConnection, AbstractMethodMap, IPacket } from "../../src";
type MethodMap = {
sendTest(value: number): void
};
class MockRequestHandler extends AbstractMethodMap {
methodMap = {};
}
class MockConnection extends AbstractConnection<MethodMap> {
public disconnect = () => {};
public read = (rawPacket: any) => <IPacket>rawPacket;
public write = jest.fn();
}
class MockConnectionWithReply extends AbstractConnection<MethodMap> {
public disconnect = () => {};
public read = (rawPacket: any) => <IPacket>rawPacket;
public write(packet: IPacket) {
this.receive({
requestId: packet.requestId,
result: 100
});
}
}
describe("Abstract IPC Connections", () => {
it("Should write complete packet", () => {
let connection = new MockConnection(new MockRequestHandler());
connection.send("sendTest", [10]);
expect(connection.write).toBeCalledWith(expect.objectContaining({
method: "sendTest",
args: [10]
}));
});
it("Request packet should timeout", async () => {
let connection = new MockConnection(new MockRequestHandler());
expect(connection.request("sendTest", [25])).rejects.toMatch("timeout");
});
it("Request should be resolved", async () => {
let connection = new MockConnectionWithReply(new MockRequestHandler());
expect(connection.request("sendTest", [25])).resolves.toEqual(100);
});
});