* [#451] added websocket connexion * [#451] added tests * [#451] add missing cleanup for web socket causing memory leaks
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
// src/websocket/__tests__/unregisterToCalendars.test.ts
|
||||
|
||||
import { unregisterToCalendars } from "../../../../src/websocket/ws/unregisterToCalendars";
|
||||
|
||||
describe("unregisterToCalendars", () => {
|
||||
let mockSocket: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockSocket = {
|
||||
readyState: WebSocket.OPEN,
|
||||
send: jest.fn(),
|
||||
};
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should send unregistration message with calendar URIs", () => {
|
||||
const calendarURIs = ["/calendars/cal1", "/calendars/cal2"];
|
||||
|
||||
unregisterToCalendars(mockSocket, calendarURIs);
|
||||
|
||||
expect(mockSocket.send).toHaveBeenCalledWith(
|
||||
JSON.stringify({
|
||||
unregister: calendarURIs,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error if socket is not open", () => {
|
||||
mockSocket.readyState = WebSocket.CONNECTING;
|
||||
const calendarURIs = ["/calendars/cal1"];
|
||||
|
||||
expect(() => unregisterToCalendars(mockSocket, calendarURIs)).toThrow(
|
||||
"Cannot unregister: WebSocket is not open"
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle empty calendar list", () => {
|
||||
unregisterToCalendars(mockSocket, []);
|
||||
|
||||
expect(mockSocket.send).toHaveBeenCalledWith(
|
||||
JSON.stringify({
|
||||
unregister: [],
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should log unregistration", () => {
|
||||
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
|
||||
const calendarURIs = ["/calendars/cal1", "/calendars/cal2"];
|
||||
|
||||
unregisterToCalendars(mockSocket, calendarURIs);
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
||||
"Unregistered to calendars",
|
||||
calendarURIs
|
||||
);
|
||||
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user