mirror of
https://github.com/parnic/node-intellicenter.git
synced 2025-06-17 02:21:53 -05:00
Get basic test up and passing
This replaces the 'ws' library with built-in websockets so that we can mock a server and successfully connect to it. A simple test verifies that Unit is handling a message request and response as we expect it to.
This commit is contained in:
@ -1,3 +1,45 @@
|
||||
describe("empty test", () => {
|
||||
it("passes", () => {});
|
||||
import { Unit } from "../src/index";
|
||||
import * as messages from "../src/messages/messages";
|
||||
import WS from "jest-websocket-mock";
|
||||
|
||||
function makeid(length: number) {
|
||||
const characters =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
const charactersLength = characters.length;
|
||||
|
||||
let result = "";
|
||||
let counter = 0;
|
||||
while (counter < length) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
describe("basic message tests", () => {
|
||||
let unit: Unit;
|
||||
let mockServer: WS;
|
||||
beforeEach(async () => {
|
||||
mockServer = new WS("ws://localhost:6680");
|
||||
|
||||
unit = new Unit("localhost", 6680);
|
||||
await unit.connect();
|
||||
await mockServer.connected;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
unit.close();
|
||||
WS.clean();
|
||||
});
|
||||
|
||||
it("can send a message and return its response", async () => {
|
||||
const msg = messages.GetBodyStatus();
|
||||
const sender = unit.send(msg);
|
||||
await expect(mockServer).toReceiveMessage(JSON.stringify(msg));
|
||||
const response = { messageID: msg.messageID, command: makeid(8) };
|
||||
mockServer.send(JSON.stringify(response));
|
||||
const clientResp = await sender;
|
||||
expect(clientResp).toEqual(response);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user