Skip to content

Commit

Permalink
Added some tests for event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Mar 24, 2020
1 parent c28283f commit cffef3f
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions packages/react-devtools-shared/src/__tests__/events-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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);
});

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();
});
});

0 comments on commit cffef3f

Please sign in to comment.