Skip to content

Commit

Permalink
test(index): add tests
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
bencelaszlo committed Aug 9, 2024
1 parent a58f45b commit b80e421
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type Subscriptions = Map<
>;
export type Unsubscribe = { unsubscribe: () => void };

export class EventEmitter {
export default class EventEmitter {
subscriptions: Subscriptions;

constructor() {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class EventEmitter {
* @param {string} eventName
* @param {Array<any>} args
*/
emit(eventName: string, args: Array<any> = []): void {
emit(eventName: string, ...args: Array<any>): void {
(this.subscriptions.get(eventName) || []).forEach(({ callback }) =>
callback(...args)
);
Expand Down
86 changes: 79 additions & 7 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit b80e421

Please sign in to comment.