Skip to content

Commit cffef3f

Browse files
author
Brian Vaughn
committed
Added some tests for event emitter
1 parent c28283f commit cffef3f

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
describe('events', () => {
11+
let dispatcher;
12+
13+
beforeEach(() => {
14+
const EventEmitter = require('../events').default;
15+
16+
dispatcher = new EventEmitter();
17+
});
18+
19+
it('can dispatch an event with no listeners', () => {
20+
dispatcher.emit('event', 123);
21+
});
22+
23+
it('handles a listener being attached multiple times', () => {
24+
const callback = jest.fn();
25+
26+
dispatcher.addListener('event', callback);
27+
dispatcher.addListener('event', callback);
28+
});
29+
30+
it('notifies all attached listeners of events', () => {
31+
const callback1 = jest.fn();
32+
const callback2 = jest.fn();
33+
const callback3 = jest.fn();
34+
35+
dispatcher.addListener('event', callback1);
36+
dispatcher.addListener('event', callback2);
37+
dispatcher.addListener('other-event', callback3);
38+
dispatcher.emit('event', 123);
39+
40+
expect(callback1).toHaveBeenCalledTimes(1);
41+
expect(callback1).toHaveBeenCalledWith(123);
42+
expect(callback2).toHaveBeenCalledTimes(1);
43+
expect(callback2).toHaveBeenCalledWith(123);
44+
expect(callback3).not.toHaveBeenCalled();
45+
});
46+
47+
it('calls later listeners before re-throwing if an earlier one throws', () => {
48+
const callbackThatThrows = jest.fn(() => {
49+
throw Error('expected');
50+
});
51+
const callback = jest.fn();
52+
53+
dispatcher.addListener('event', callbackThatThrows);
54+
dispatcher.addListener('event', callback);
55+
56+
expect(() => {
57+
dispatcher.emit('event', 123);
58+
}).toThrow('expected');
59+
60+
expect(callbackThatThrows).toHaveBeenCalledTimes(1);
61+
expect(callbackThatThrows).toHaveBeenCalledWith(123);
62+
expect(callback).toHaveBeenCalledTimes(1);
63+
expect(callback).toHaveBeenCalledWith(123);
64+
});
65+
66+
it('removes attached listeners', () => {
67+
const callback1 = jest.fn();
68+
const callback2 = jest.fn();
69+
70+
dispatcher.addListener('event', callback1);
71+
dispatcher.addListener('other-event', callback2);
72+
dispatcher.removeListener('event', callback1);
73+
dispatcher.emit('event', 123);
74+
expect(callback1).not.toHaveBeenCalled();
75+
dispatcher.emit('other-event', 123);
76+
expect(callback2).toHaveBeenCalledTimes(1);
77+
expect(callback2).toHaveBeenCalledWith(123);
78+
});
79+
80+
it('removes all listeners', () => {
81+
const callback1 = jest.fn();
82+
const callback2 = jest.fn();
83+
const callback3 = jest.fn();
84+
85+
dispatcher.addListener('event', callback1);
86+
dispatcher.addListener('event', callback2);
87+
dispatcher.addListener('other-event', callback3);
88+
dispatcher.removeAllListeners();
89+
dispatcher.emit('event', 123);
90+
dispatcher.emit('other-event', 123);
91+
92+
expect(callback1).not.toHaveBeenCalled();
93+
expect(callback2).not.toHaveBeenCalled();
94+
expect(callback3).not.toHaveBeenCalled();
95+
});
96+
});

0 commit comments

Comments
 (0)