From 6ff7abddd166c96be98d2af01826f136776728a6 Mon Sep 17 00:00:00 2001 From: Andrew Courtice Date: Thu, 9 Sep 2021 09:24:59 +1000 Subject: [PATCH] test(core): added event emitter test --- core/test/index.test.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/core/test/index.test.ts b/core/test/index.test.ts index 9b028125..6253e442 100644 --- a/core/test/index.test.ts +++ b/core/test/index.test.ts @@ -1,3 +1,7 @@ +import { + EventEmitter, +} from '../src/event-emitter'; + import { createStore, } from '../src/index'; @@ -59,6 +63,31 @@ describe('Harlem Core', () => { store = getStore(); }); + describe('Event Emitter', () => { + + test('Should handle on, once and emit', () => { + const eventEmitter = new EventEmitter(); + + const eventName = 'test-event'; + const onListener = jest.fn(); + const onceListener = jest.fn(); + + const listeners = [ + eventEmitter.on(eventName, onListener), + eventEmitter.once(eventName, onceListener), + ]; + + eventEmitter.emit(eventName); + eventEmitter.emit(eventName); + + expect(onListener).toHaveBeenCalledTimes(2); + expect(onceListener).toHaveBeenCalledTimes(1); + + listeners.forEach(({ dispose }) => dispose()); + }); + + }); + describe('Store', () => { test('Should prevent duplicate creation of store objects', () => { @@ -107,7 +136,7 @@ describe('Harlem Core', () => { state, } = store; - // @ts-expect-error + // @ts-expect-error This is readonly state.firstName = 'Billy'; expect(state.firstName).toBe('John');