Files
workavia-calendar-front/__test__/features/websocket/websocketAPI/unregisterToCalendars.test.tsx
T
Camille Moussu 00c3c0d6f0 444 use websocket to trigger reloads (#458)
* [#444] added refresh on sync token calendar update

* [#444] added refresh on register and tests

* [#444] extracted logic for useEffects and updates

* [#444] refactored code structure to use absolute paths
2026-01-19 09:09:23 +01:00

61 lines
1.5 KiB
TypeScript

// src/websocket/__tests__/unregisterToCalendars.test.ts
import { unregisterToCalendars } from "@/websocket/operations/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();
});
});