import { NotificationsGateway } from "./notifications.gateway"; import { WsAuthService } from "./ws-auth.service"; const gateway = () => new NotificationsGateway({} as WsAuthService); describe("NotificationsGateway", () => { it("skips emitNew rather than throwing when no WebSocket server is attached", () => { const g = gateway(); expect(() => g.emitNew("user-1", { id: "n-1" } as never, 3)).not.toThrow(); }); it("skips emitUnreadCount rather than throwing when no WebSocket server is attached", () => { const g = gateway(); expect(() => g.emitUnreadCount("user-1", 3)).not.toThrow(); }); it("pushes to the user's room once a server is attached", () => { const g = gateway(); const emit = jest.fn(); const to = jest.fn().mockReturnValue({ emit }); (g as unknown as { server: { to: typeof to } }).server = { to }; g.emitNew("user-1", { id: "n-1" } as never, 3); expect(to).toHaveBeenCalledWith("user:user-1"); expect(emit).toHaveBeenCalledWith("notification:new", { id: "n-1" }); expect(emit).toHaveBeenCalledWith("notification:unread-count", 3); }); });