Files
web/src/core/utils/eventBus.test.ts
Dan Ditomaso 47f8264c31 Fix tsc errors (#649)
* fixed tsc errors

* fixed tsc errors

* fixed tsc errors

* fixing tsc errors

* fixing more tsc errors

* fixing more tsc errors

* fixed tsc errors

* fixing tsc errors

* fixing PR issues

* commented out tsc check

* completing tsc fixes

* updating lockfile

* removed react-hooks
2025-06-12 19:00:30 -04:00

72 lines
2.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { eventBus } from "@core/utils/eventBus.ts";
describe("EventBus", () => {
beforeEach(() => {
// Reset event listeners before each test
eventBus.offAll();
});
it("should register an event listener and trigger it on emit", () => {
const mockCallback = vi.fn();
eventBus.on("dialog:unsafeRoles", mockCallback);
eventBus.emit("dialog:unsafeRoles", { action: "confirm" });
expect(mockCallback).toHaveBeenCalledWith({ action: "confirm" });
});
it("should remove an event listener with off", () => {
const mockCallback = vi.fn();
eventBus.on("dialog:unsafeRoles", mockCallback);
eventBus.off("dialog:unsafeRoles", mockCallback);
eventBus.emit("dialog:unsafeRoles", { action: "dismiss" });
expect(mockCallback).not.toHaveBeenCalled();
});
it("should return an unsubscribe function from on", () => {
const mockCallback = vi.fn();
const unsubscribe = eventBus.on("dialog:unsafeRoles", mockCallback);
unsubscribe();
eventBus.emit("dialog:unsafeRoles", { action: "confirm" });
expect(mockCallback).not.toHaveBeenCalled();
});
it("should allow multiple listeners for the same event", () => {
const mockCallback1 = vi.fn();
const mockCallback2 = vi.fn();
eventBus.on("dialog:unsafeRoles", mockCallback1);
eventBus.on("dialog:unsafeRoles", mockCallback2);
eventBus.emit("dialog:unsafeRoles", { action: "confirm" });
expect(mockCallback1).toHaveBeenCalledWith({ action: "confirm" });
expect(mockCallback2).toHaveBeenCalledWith({ action: "confirm" });
});
it("should only remove the specific listener when off is called", () => {
const mockCallback1 = vi.fn();
const mockCallback2 = vi.fn();
eventBus.on("dialog:unsafeRoles", mockCallback1);
eventBus.on("dialog:unsafeRoles", mockCallback2);
eventBus.off("dialog:unsafeRoles", mockCallback1);
eventBus.emit("dialog:unsafeRoles", { action: "dismiss" });
expect(mockCallback1).not.toHaveBeenCalled();
expect(mockCallback2).toHaveBeenCalledWith({ action: "dismiss" });
});
it("should not fail when calling off on a non-existent listener", () => {
const mockCallback = vi.fn();
eventBus.off("dialog:unsafeRoles", mockCallback);
eventBus.emit("dialog:unsafeRoles", { action: "confirm" });
expect(mockCallback).not.toHaveBeenCalled(); // No error should occur
});
});