2024-01-24 23:01:47 +08:00
|
|
|
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-01-24 23:01:47 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
emitter.addListener(callback1);
|
|
|
|
emitter.addListener(callback2);
|
|
|
|
emitter.dispatch({ data: 1 });
|
2024-01-24 23:01:47 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
expect(callback1).toHaveBeenCalledWith({ data: 1 });
|
|
|
|
expect(callback2).toHaveBeenCalledWith({ data: 1 });
|
|
|
|
});
|
2024-01-24 23:01:47 +08:00
|
|
|
|
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-01-24 23:01:47 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
const subscription = emitter.addListener(callback1);
|
|
|
|
subscription.destroy();
|
|
|
|
emitter.dispatch({});
|
2024-01-24 23:01:47 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
expect(callback1).not.toHaveBeenCalled();
|
2024-01-24 23:01:47 +08:00
|
|
|
});
|