-
Notifications
You must be signed in to change notification settings - Fork 45.7k
/
events-test.js
126 lines (103 loc) · 3.92 KB
/
events-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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
describe('events', () => {
let dispatcher;
beforeEach(() => {
const EventEmitter = require('../events').default;
dispatcher = new EventEmitter();
});
it('can dispatch an event with no listeners', () => {
dispatcher.emit('event', 123);
});
it('handles a listener being attached multiple times', () => {
const callback = jest.fn();
dispatcher.addListener('event', callback);
dispatcher.addListener('event', callback);
dispatcher.emit('event', 123);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(123);
});
it('notifies all attached listeners of events', () => {
const callback1 = jest.fn();
const callback2 = jest.fn();
const callback3 = jest.fn();
dispatcher.addListener('event', callback1);
dispatcher.addListener('event', callback2);
dispatcher.addListener('other-event', callback3);
dispatcher.emit('event', 123);
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith(123);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith(123);
expect(callback3).not.toHaveBeenCalled();
});
it('calls later listeners before re-throwing if an earlier one throws', () => {
const callbackThatThrows = jest.fn(() => {
throw Error('expected');
});
const callback = jest.fn();
dispatcher.addListener('event', callbackThatThrows);
dispatcher.addListener('event', callback);
expect(() => {
dispatcher.emit('event', 123);
}).toThrow('expected');
expect(callbackThatThrows).toHaveBeenCalledTimes(1);
expect(callbackThatThrows).toHaveBeenCalledWith(123);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(123);
});
it('removes attached listeners', () => {
const callback1 = jest.fn();
const callback2 = jest.fn();
dispatcher.addListener('event', callback1);
dispatcher.addListener('other-event', callback2);
dispatcher.removeListener('event', callback1);
dispatcher.emit('event', 123);
expect(callback1).not.toHaveBeenCalled();
dispatcher.emit('other-event', 123);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith(123);
});
it('removes all listeners', () => {
const callback1 = jest.fn();
const callback2 = jest.fn();
const callback3 = jest.fn();
dispatcher.addListener('event', callback1);
dispatcher.addListener('event', callback2);
dispatcher.addListener('other-event', callback3);
dispatcher.removeAllListeners();
dispatcher.emit('event', 123);
dispatcher.emit('other-event', 123);
expect(callback1).not.toHaveBeenCalled();
expect(callback2).not.toHaveBeenCalled();
expect(callback3).not.toHaveBeenCalled();
});
it('should call the initial listeners even if others are added or removed during a dispatch', () => {
const callback1 = jest.fn(() => {
dispatcher.removeListener('event', callback2);
dispatcher.addListener('event', callback3);
});
const callback2 = jest.fn();
const callback3 = jest.fn();
dispatcher.addListener('event', callback1);
dispatcher.addListener('event', callback2);
dispatcher.emit('event', 123);
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith(123);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith(123);
expect(callback3).not.toHaveBeenCalled();
dispatcher.emit('event', 456);
expect(callback1).toHaveBeenCalledTimes(2);
expect(callback1).toHaveBeenCalledWith(456);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenCalledWith(456);
});
});