2024-01-24 23:01:47 +08:00
|
|
|
import PubSub from "../../js/lib/pubsub";
|
2021-03-11 22:28:18 +08:00
|
|
|
|
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();
|
2021-03-11 22:28:18 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
pubsub.subscribe("topic1", callback1);
|
|
|
|
pubsub.subscribe("topic2", callback2);
|
|
|
|
pubsub.broadcast("topic1", { data: 1 });
|
2021-03-11 22:28:18 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
expect(callback1).toHaveBeenCalledWith({ data: 1 });
|
|
|
|
expect(callback2).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-03-11 22:28:18 +08:00
|
|
|
|
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();
|
2021-05-07 22:41:37 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
const subscription = pubsub.subscribe("topic1", callback1);
|
|
|
|
subscription.destroy();
|
|
|
|
pubsub.broadcast("topic1", {});
|
2021-03-11 22:28:18 +08:00
|
|
|
|
2024-02-02 17:39:28 +08:00
|
|
|
expect(callback1).not.toHaveBeenCalled();
|
2021-03-11 22:28:18 +08:00
|
|
|
});
|