livebook/assets/test/lib/pubsub.test.js

26 lines
748 B
JavaScript
Raw Normal View History

import PubSub from "../../js/lib/pubsub";
2024-02-02 17:39:28 +08:00
test("subscribed callback is called on the specified topic", () => {
const pubsub = new PubSub();
const callback1 = jest.fn();
const callback2 = jest.fn();
2024-02-02 17:39:28 +08:00
pubsub.subscribe("topic1", callback1);
pubsub.subscribe("topic2", callback2);
pubsub.broadcast("topic1", { data: 1 });
2024-02-02 17:39:28 +08:00
expect(callback1).toHaveBeenCalledWith({ data: 1 });
expect(callback2).not.toHaveBeenCalled();
});
2024-02-02 17:39:28 +08:00
test("subscribe returns a subscription object that can be destroyed", () => {
const pubsub = new PubSub();
const callback1 = jest.fn();
2024-02-02 17:39:28 +08:00
const subscription = pubsub.subscribe("topic1", callback1);
subscription.destroy();
pubsub.broadcast("topic1", {});
2024-02-02 17:39:28 +08:00
expect(callback1).not.toHaveBeenCalled();
});