-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventEmitter.test.js
98 lines (80 loc) · 2.35 KB
/
eventEmitter.test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import createEventEmitter from "./eventEmitter";
describe("eventEmitter", () => {
var emitter;
beforeEach(() => {
emitter = createEventEmitter("foo", "bar");
});
it("returns object with getEvents", () => {
var e = createEventEmitter();
expect(e.getEvents()).toEqual({"*": []});
});
it("takes list of eventNames to setup", () => {
expect(emitter.getEvents()).toEqual({
foo: [],
bar: [],
"*": [],
});
});
it("can add a new event type", () => {
emitter.add("baz");
expect(emitter.getEvents()).toEqual({
foo: [],
bar: [],
baz: [],
"*": [],
});
});
it("can remove an existing event type", () => {
emitter.del("bar");
expect(emitter.getEvents()).toEqual({
foo: [],
"*": [],
});
});
it("can add listener to event", () => {
var a = jest.fn();
var b = jest.fn();
var c = jest.fn();
emitter.on("foo", a);
emitter.on("foo", b);
emitter.on("foo", c);
expect(emitter.getEvents().foo.length).toBe(3);
expect(emitter.getEvents().foo).toEqual([a, b, c]);
});
it("emits event to listeners for event type AND '*' events", () => {
var a = jest.fn();
var b = jest.fn();
var c = jest.fn();
emitter.on("foo", a);
emitter.on("foo", b);
emitter.on(c);
expect(emitter.getEvents().foo.length).toBe(2);
expect(emitter.getEvents()["*"].length).toBe(1);
emitter.emit("foo", "test1", "test2");
expect(a.mock.calls.length).toBe(1);
expect(b.mock.calls.length).toBe(1);
expect(c.mock.calls.length).toBe(1);
expect(a.mock.calls[0]).toEqual(["test1", "test2"]);
expect(b.mock.calls[0]).toEqual(["test1", "test2"]);
expect(c.mock.calls[0]).toEqual(["foo", "test1", "test2"]);
});
it("can remove listener from event", () => {
var a = jest.fn();
var b = jest.fn();
var c = jest.fn();
var unsubA = emitter.on("foo", a);
var unsubB = emitter.on("foo", b);
var unsubC = emitter.on("foo", c);
expect(emitter.getEvents().foo.length).toBe(3);
unsubA();
unsubB();
unsubC();
expect(emitter.getEvents().foo.length).toBe(0);
});
it("only triggers '*' event once when that event is called manually", () => {
var a = jest.fn();
emitter.on(a);
emitter.emit("*", "test1", "test2");
expect(a.mock.calls[0]).toEqual(["*", "test1", "test2"]);
});
});