livebook/assets/test/lib/emitter.test.js

26 lines
715 B
JavaScript
Raw Normal View History

import Emitter from "../../js/lib/emitter";
2024-02-02 17:39:28 +08:00
test("listener callbacks are called on dispatch", () => {
const emitter = new Emitter();
const callback1 = jest.fn();
const callback2 = jest.fn();
2024-02-02 17:39:28 +08:00
emitter.addListener(callback1);
emitter.addListener(callback2);
emitter.dispatch({ data: 1 });
2024-02-02 17:39:28 +08:00
expect(callback1).toHaveBeenCalledWith({ data: 1 });
expect(callback2).toHaveBeenCalledWith({ data: 1 });
});
2024-02-02 17:39:28 +08:00
test("addListener returns a subscription object that can be destroyed", () => {
const emitter = new Emitter();
const callback1 = jest.fn();
2024-02-02 17:39:28 +08:00
const subscription = emitter.addListener(callback1);
subscription.destroy();
emitter.dispatch({});
2024-02-02 17:39:28 +08:00
expect(callback1).not.toHaveBeenCalled();
});