-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubsub.spec.js
49 lines (38 loc) · 1.06 KB
/
pubsub.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* eslint-env jest */
const pubsub = require("./pubsub");
describe("PubSub", () => {
let events;
const callback = jest.fn();
beforeEach(() => {
events = pubsub();
callback.mockClear();
});
it("has simple API", () => {
const keys = Object.keys(events).sort().join();
expect(keys).toBe("pub,sub");
});
it("passes data to callback", () => {
events.sub(callback);
events.pub("foo");
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith("foo");
});
it("removes callback when it is unsubscibed", () => {
const unSub = events.sub(callback);
unSub();
events.pub("foo");
expect(callback).not.toHaveBeenCalled();
});
it("does not call a callback twice", () => {
events.sub(callback);
events.sub(callback);
events.pub("foo");
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith("foo");
});
it("does not leak on pub", () => {
events.sub(callback);
const result = events.pub("foo");
expect(result).toBeUndefined();
});
});