livebook/assets/test/lib/pub_sub.test.js
Jonatan Kłosko cd80bd7804
Implement user cursor and selection tracking (#260)
* Implement user cursor and selection tracking

* Separate jump-to-user and follow
2021-05-07 16:41:37 +02:00

38 lines
1.1 KiB
JavaScript

import PubSub from "../../js/lib/pub_sub";
describe("PubSub", () => {
test("subscribed callback is called on the specified topic", () => {
const pubsub = new PubSub();
const callback1 = jest.fn();
const callback2 = jest.fn();
pubsub.subscribe("topic1", callback1);
pubsub.subscribe("topic2", callback2);
pubsub.broadcast("topic1", { data: 1 });
expect(callback1).toHaveBeenCalledWith({ data: 1 });
expect(callback2).not.toHaveBeenCalled();
});
test("subscribe returns a function that unsubscribes", () => {
const pubsub = new PubSub();
const callback1 = jest.fn();
const unsubscribe = pubsub.subscribe("topic1", callback1);
unsubscribe();
pubsub.broadcast("topic1", {});
expect(callback1).not.toHaveBeenCalled();
});
test("unsubscribed callback is not called on the specified topic", () => {
const pubsub = new PubSub();
const callback1 = jest.fn();
pubsub.subscribe("topic1", callback1);
pubsub.unsubscribe("topic1", callback1);
pubsub.broadcast("topic1", {});
expect(callback1).not.toHaveBeenCalled();
});
});