Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ import { MockSocket } from "../__mocks__/ws";
const deserializer = mock<Deserializer>();

describe("WebsocketDuplexConnection", function () {
it("reports availability based on open/close status", () => {
const socket = new MockSocket() as unknown as WebSocket;
const multiplexerDemultiplexer = mock<
Multiplexer & Demultiplexer & FrameHandler
>();
const connection = new WebsocketDuplexConnection(
socket,
deserializer,
() => multiplexerDemultiplexer
);

expect(connection.availability).toEqual(1);

connection.close();

expect(connection.availability).toEqual(0);
});

describe("when closed", () => {
it("removes listeners from the underlying socket event emitter", () => {
// arrange
Expand Down Expand Up @@ -180,6 +198,22 @@ describe("WebsocketDuplexConnection", function () {
expect(onCloseCallback).toBeCalledTimes(1);
expect(onCloseCallback).toHaveBeenCalledWith(expectedError);
});

it("reports 0 availability", () => {
const socket = new MockSocket() as unknown as WebSocket;
const multiplexerDemultiplexer = mock<
Multiplexer & Demultiplexer & FrameHandler
>();
const connection = new WebsocketDuplexConnection(
socket,
deserializer,
() => multiplexerDemultiplexer
);

connection.close();

expect(connection.availability).toEqual(0);
});
});

describe("send()", () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/rsocket-websocket-server/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from "@jest/types";
import { pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "../../tsconfig.json";

const config: Config.InitialOptions = {
preset: "ts-jest",
testRegex: "(\\/__tests__\\/.*|\\.(test|spec))\\.(ts)$",
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
// This has to match the baseUrl defined in tsconfig.json.
prefix: "<rootDir>/../../",
}),
modulePathIgnorePatterns: [
"<rootDir>/__tests__/test-utils",
"<rootDir>/__tests__/*.d.ts",
],
collectCoverage: true,
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!**/node_modules/**"],
};

export default config;
2 changes: 1 addition & 1 deletion packages/rsocket-websocket-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"clean": "rimraf -rf ./dist",
"compile": "tsc -p tsconfig.build.json",
"prepublishOnly": "yarn run build",
"test": "echo \"Error: no test specified\" && exit 0"
"test": "jest"
},
"dependencies": {
"rsocket-core": "^1.0.0-alpha.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class WebsocketDuplexConnection
}

get availability(): number {
return this.websocketDuplex.destroyed ? 0 : 1;
return this.done ? 0 : 1;
}

close(error?: Error) {
Expand Down
Loading