diff --git a/src/index.ts b/src/index.ts index 73b5d11..1878c68 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ export type Subscriptions = Map< >; export type Unsubscribe = { unsubscribe: () => void }; -export class EventEmitter { +export default class EventEmitter { subscriptions: Subscriptions; constructor() { @@ -41,7 +41,7 @@ export class EventEmitter { * @param {string} eventName * @param {Array} args */ - emit(eventName: string, args: Array = []): void { + emit(eventName: string, ...args: Array): void { (this.subscriptions.get(eventName) || []).forEach(({ callback }) => callback(...args) ); diff --git a/test/index.spec.ts b/test/index.spec.ts index d305240..3939740 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,13 +1,85 @@ -import { myPackage } from '../src'; +import EventEmitter from '../src'; -describe('index', () => { - describe('myPackage', () => { - it('should return a string containing the message', () => { - const message = 'Hello'; +describe('eventEmitter', () => { + it('should add callback to subscriptions if it has no subscriptions', () => { + const testCallbackA = jest.fn(); - const result = myPackage(message); + const eventEmitter = new EventEmitter(); + eventEmitter.subscribe('test-event', testCallbackA); - expect(result).toMatch(message); + expect(eventEmitter.subscriptions.has('test-event')).toEqual(true); + expect(eventEmitter.subscriptions.get('test-event')).toEqual([ + { id: expect.any(Symbol), callback: testCallbackA }, + ]); + }); + + it('should add callback to subscriptions if it has subscriptions', () => { + const testCallbackA = jest.fn(); + const testCallbackB = jest.fn(); + + const eventEmitter = new EventEmitter(); + eventEmitter.subscribe('test-event', testCallbackA); + eventEmitter.subscribe('test-event', testCallbackB); + + expect(eventEmitter.subscriptions.has('test-event')).toEqual(true); + expect(eventEmitter.subscriptions.get('test-event')).toContainEqual({ + id: expect.any(Symbol), + callback: testCallbackA, + }); + }); + + it('should add callback for another event to subscriptions', () => { + const testCallbackA = jest.fn(); + const testCallbackB = jest.fn(); + + const eventEmitter = new EventEmitter(); + eventEmitter.subscribe('test-event-a', testCallbackA); + eventEmitter.subscribe('test-event-b', testCallbackB); + + expect(eventEmitter.subscriptions.has('test-event-a')).toEqual(true); + expect(eventEmitter.subscriptions.get('test-event-a')).toContainEqual({ + id: expect.any(Symbol), + callback: testCallbackA, }); + expect(eventEmitter.subscriptions.has('test-event-b')).toEqual(true); + expect(eventEmitter.subscriptions.get('test-event-b')).toContainEqual({ + id: expect.any(Symbol), + callback: testCallbackB, + }); + }); + + it('should call all subscriptions', () => { + const testCallbackA = jest.fn(); + const testCallbackB = jest.fn(); + const mockEventArgs = { a: 'a' }; + + const eventEmitter = new EventEmitter(); + eventEmitter.subscribe('test-event', testCallbackA); + eventEmitter.subscribe('test-event', testCallbackB); + eventEmitter.emit('test-event', mockEventArgs); + + expect(testCallbackA).toHaveBeenCalled(); + expect(testCallbackA).toHaveBeenCalledWith(mockEventArgs); + expect(testCallbackB).toHaveBeenCalled(); + expect(testCallbackB).toHaveBeenCalledWith(mockEventArgs); + }); + + it('should not call unsubscribed callbacks', () => { + const testCallbackA = jest.fn(); + const testCallbackB = jest.fn(); + const mockEventArgs = { a: 'a' }; + + const eventEmitter = new EventEmitter(); + const { unsubscribe: unsubscribeCallbackA } = eventEmitter.subscribe( + 'test-event', + testCallbackA + ); + eventEmitter.subscribe('test-event', testCallbackB); + unsubscribeCallbackA(); + eventEmitter.emit('test-event', mockEventArgs); + + expect(testCallbackA).not.toHaveBeenCalled(); + expect(testCallbackB).toHaveBeenCalled(); + expect(testCallbackB).toHaveBeenCalledWith(mockEventArgs); }); });