import { EventBus } from "./eventBus"; declare global { interface EventMapper { "": any; } } describe("eventBus", () => { const MockEvent: any = test; test("事件订阅", () => { const bus = new EventBus(); const fn = jest.fn(); const disposer = bus.on(MockEvent, fn); bus.emit(MockEvent, "foo"); expect(fn).lastCalledWith("foo"); disposer(); bus.emit(MockEvent, "bar"); expect(fn).toHaveBeenCalledTimes(1); }); test("单次事件订阅", () => { const bus = new EventBus(); const fn = jest.fn(); bus.once(MockEvent, fn); bus.emit(MockEvent, "foo"); bus.emit(MockEvent, "bar"); expect(fn).lastCalledWith("foo"); expect(fn).toHaveBeenCalledTimes(1); }); test("新增订阅者", () => { const bus = new EventBus(); const fn = jest.fn(); bus.onSubscriber(MockEvent, (listener) => { listener("foo"); }); bus.on(MockEvent, fn); expect(fn).lastCalledWith("foo"); }); });