From 221abd8c481f6200bca4103bd2587aa30d786c13 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Mon, 21 Jan 2019 16:05:59 +0100 Subject: [PATCH] Add test for EventDispatcher --- .../src/Model/spec/EventDispatcher.spec.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 backend/src/Model/spec/EventDispatcher.spec.ts diff --git a/backend/src/Model/spec/EventDispatcher.spec.ts b/backend/src/Model/spec/EventDispatcher.spec.ts new file mode 100644 index 0000000..d9ab80a --- /dev/null +++ b/backend/src/Model/spec/EventDispatcher.spec.ts @@ -0,0 +1,43 @@ +import 'mocha' +import { EventDispatcher } from '../../../../events' + +import { expect } from 'chai' +import { doesNotReject } from 'assert' + +describe('EventDispatcher', async () => { + it('should dispatch', async function () { + const dispatcher = new EventDispatcher('me') + this.timeout(300) + + setTimeout(() => dispatcher.dispatch('hello'), 5) + const response = await new Promise((resolve) => { + dispatcher.subscribe((msg) => { + resolve(msg) + }) + }) + + expect(response).to.eq('hello') + }) + + it('should unsubscribe', async function () { + const dispatcher = new EventDispatcher('me') + this.timeout(300) + let incrementee = 0 + const callback = (msg: any) => { + incrementee += 1 + } + + dispatcher.subscribe(callback) + dispatcher.subscribe(callback) + dispatcher.unsubscribe(callback) + + // Should increment to 1 + dispatcher.dispatch('hello') + dispatcher.unsubscribe(callback) + + // should not cause any increment + dispatcher.dispatch('hello') + + expect(incrementee).to.eq(1) + }) +})