UNPKG

@novo-learning/novo-sdk

Version:

SDK for the Novolanguage Speech Analysis API

162 lines (141 loc) 5.49 kB
import { EventBus } from '../../src/clients/speech-api/event-bus'; describe('Event bus', () => { describe('Dispatch events on root', () => { let eventbus: EventBus; beforeEach(() => { eventbus = new EventBus(); }); it('should send dispatched events to event listeners which registered for the event', () => { const callback = jest.fn((): void => { /** do nothing */ }); eventbus.addEventListener('myevent', callback); eventbus.dispatch('myevent'); expect(callback).toBeCalled(); }); it('should not send dispatched events to event listeners which did not registered for the event', () => { const callback = jest.fn((): void => { /** do nothing */ }); eventbus.addEventListener('myevent', callback); eventbus.dispatch('myotherevent'); expect(callback).not.toBeCalled(); }); it('should send dispatched events with the data of the event to event listeners which registered for the event', () => { const callback = jest.fn((data): void => { expect(data).toEqual('data'); }); eventbus.addEventListener('myevent', callback); eventbus.dispatch('myevent', 'data'); expect(callback).toBeCalled(); }); it('should not send dispatched events after listener has been removed', () => { const callback = jest.fn((): void => { /** do nothing */ }); eventbus.addEventListener('myevent', callback); eventbus.removeEventListener('myevent', callback); eventbus.dispatch('myevent'); expect(callback).not.toBeCalled(); }); }); describe('Create channels', () => { let root: EventBus; let channel: EventBus; beforeEach(() => { root = new EventBus(); channel = root.channel('channel'); }); it('should create a new EventBus when calling channel', () => { expect(root).not.toBe(channel); expect(root).toBe(channel.parent); expect(root).toBe(channel.root()); }); it('should find the root EventBus when calling root', () => { const subchannel = channel.channel('subchannel'); expect(channel).toBe(subchannel.parent); expect(root).toBe(subchannel.root()); }); }); describe('Dispatch events on channel without broadcast', () => { let root: EventBus; let channel: EventBus; beforeEach(() => { root = new EventBus(); channel = root.channel('channel'); }); it('should send dispatched events on channel to event listeners on channel and not on root which registered for the event', () => { const callbackRoot = jest.fn((): void => { /** do nothing */ }); const callbackChannel = jest.fn((): void => { /** do nothing */ }); root.addEventListener('myevent', callbackRoot); channel.addEventListener('myevent', callbackChannel); channel.dispatch('myevent', undefined, false); expect(callbackRoot).not.toBeCalled(); expect(callbackChannel).toBeCalled(); }); it('should not send dispatched events to event listeners which did not registered for the event', () => { const callbackRoot = jest.fn((): void => { /** do nothing */ }); const callbackChannel = jest.fn((): void => { /** do nothing */ }); root.addEventListener('myevent', callbackRoot); channel.addEventListener('myevent', callbackRoot); channel.dispatch('myotherevent', undefined, false); expect(callbackRoot).not.toBeCalled(); expect(callbackChannel).not.toBeCalled(); }); }); describe('Dispatch events on channel with broadcast', () => { let root: EventBus; let channel: EventBus; beforeEach(() => { root = new EventBus(); channel = root.channel('channel'); }); it('should send by default dispatched events on channel to event listeners on channel and root which registered for the event', () => { const callbackRoot = jest.fn((): void => { /** do nothing */ }); const callbackChannel = jest.fn((): void => { /** do nothing */ }); root.addEventListener('myevent', callbackRoot); channel.addEventListener('myevent', callbackChannel); channel.dispatch('myevent'); expect(callbackRoot).toBeCalled(); expect(callbackChannel).toBeCalled(); }); it('should send dispatched events on channel to event listeners on channel and root which registered for the event', () => { const callbackRoot = jest.fn((): void => { /** do nothing */ }); const callbackChannel = jest.fn((): void => { /** do nothing */ }); root.addEventListener('myevent', callbackRoot); channel.addEventListener('myevent', callbackChannel); channel.dispatch('myevent', undefined, true); expect(callbackRoot).toBeCalled(); expect(callbackChannel).toBeCalled(); }); it('should not send dispatched events to event listeners which did not registered for the event', () => { const callbackRoot = jest.fn((): void => { /** do nothing */ }); const callbackChannel = jest.fn((): void => { /** do nothing */ }); root.addEventListener('myevent', callbackRoot); channel.addEventListener('myevent', callbackRoot); channel.dispatch('myotherevent', undefined, true); expect(callbackRoot).not.toBeCalled(); expect(callbackChannel).not.toBeCalled(); }); }); });