2fa8a7f16a
* [#451] added websocket connexion * [#451] added tests * [#451] add missing cleanup for web socket causing memory leaks
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { registerToCalendars } from "../../../../src/websocket/ws/registerToCalendars";
|
|
|
|
describe("registerToCalendars", () => {
|
|
let mockSocket: any;
|
|
|
|
beforeEach(() => {
|
|
mockSocket = {
|
|
readyState: WebSocket.OPEN,
|
|
send: jest.fn(),
|
|
};
|
|
});
|
|
|
|
it("should send registration message with calendar URIs", () => {
|
|
const calendarURIs = ["/calendars/cal1", "/calendars/cal2"];
|
|
|
|
registerToCalendars(mockSocket, calendarURIs);
|
|
|
|
expect(mockSocket.send).toHaveBeenCalledWith(
|
|
JSON.stringify({
|
|
register: calendarURIs,
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should throw error if socket is not open", () => {
|
|
mockSocket.readyState = WebSocket.CONNECTING;
|
|
const calendarURIs = ["/calendars/cal1"];
|
|
|
|
expect(() => registerToCalendars(mockSocket, calendarURIs)).toThrow(
|
|
"Cannot register: WebSocket is not open"
|
|
);
|
|
});
|
|
|
|
it("should handle empty calendar list", () => {
|
|
registerToCalendars(mockSocket, []);
|
|
|
|
expect(mockSocket.send).toHaveBeenCalledWith(
|
|
JSON.stringify({
|
|
register: [],
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should log registration", () => {
|
|
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
|
|
const calendarURIs = ["/calendars/cal1", "/calendars/cal2"];
|
|
|
|
registerToCalendars(mockSocket, calendarURIs);
|
|
|
|
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
"Registered to calendars",
|
|
calendarURIs
|
|
);
|
|
|
|
consoleLogSpy.mockRestore();
|
|
});
|
|
});
|