Commit f9762169 authored by Paul Slaughter's avatar Paul Slaughter

Replace event_hub_factory with Vue impl

- Also add specs to harden the expected API
  from this function
parent 616cec34
import mitt from 'mitt'; import Vue from 'vue';
/**
* Return a Vue like event hub
*
* - $on
* - $off
* - $once
* - $emit
*
* Please note, this was once implemented with `mitt`, but since then has been reverted
* because of some API issues. https://gitlab.com/gitlab-org/gitlab/-/merge_requests/35074
*
* We'd like to shy away from using a full fledged Vue instance from this in the future.
*/
export default () => { export default () => {
const emitter = mitt(); return new Vue();
emitter.once = (event, handler) => {
const wrappedHandler = evt => {
handler(evt);
emitter.off(event, wrappedHandler);
};
emitter.on(event, wrappedHandler);
};
emitter.$on = emitter.on;
emitter.$once = emitter.once;
emitter.$off = emitter.off;
emitter.$emit = emitter.emit;
return emitter;
}; };
import createEventHub from '~/helpers/event_hub_factory'; import createEventHub from '~/helpers/event_hub_factory';
const TEST_EVENT = 'foobar';
describe('event bus factory', () => { describe('event bus factory', () => {
let eventBus; let eventBus;
let handler;
beforeEach(() => { beforeEach(() => {
eventBus = createEventHub(); eventBus = createEventHub();
handler = jest.fn();
}); });
afterEach(() => { afterEach(() => {
eventBus = null; eventBus = null;
}); });
describe('underlying module', () => { describe('instance', () => {
let mitt; it.each`
method
${'$on'}
${'$once'}
${'$off'}
${'$emit'}
`('has $method method', ({ method }) => {
expect(eventBus[method]).toEqual(expect.any(Function));
});
});
describe('$on', () => {
beforeEach(() => { beforeEach(() => {
jest.resetModules(); eventBus.$on(TEST_EVENT, handler);
jest.mock('mitt'); });
// eslint-disable-next-line global-require it('calls handler when event is emitted', () => {
mitt = require('mitt'); eventBus.$emit(TEST_EVENT);
mitt.mockReturnValue(() => ({})); expect(handler).toHaveBeenCalledWith();
});
const createEventHubActual = jest.requireActual('~/helpers/event_hub_factory').default; it('calls handler with multiple args', () => {
eventBus = createEventHubActual(); eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
expect(handler).toHaveBeenCalledWith('arg1', 'arg2', 'arg3');
}); });
it('creates an emitter', () => { it('calls handler multiple times', () => {
expect(mitt).toHaveBeenCalled(); eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
expect(handler).toHaveBeenCalledTimes(2);
}); });
});
describe('instance', () => { it('does not call handler after $off with handler', () => {
it.each` eventBus.$off(TEST_EVENT, handler);
method
${'on'} eventBus.$emit(TEST_EVENT);
${'once'}
${'off'} expect(handler).not.toHaveBeenCalled();
${'emit'}
`('binds $$method to $method ', ({ method }) => {
expect(typeof eventBus[method]).toBe('function');
expect(eventBus[method]).toBe(eventBus[`$${method}`]);
}); });
});
describe('once', () => { it('does not call handler after $off', () => {
const event = 'foobar'; eventBus.$off(TEST_EVENT);
let handler;
beforeEach(() => { eventBus.$emit(TEST_EVENT);
jest.spyOn(eventBus, 'on');
jest.spyOn(eventBus, 'off'); expect(handler).not.toHaveBeenCalled();
handler = jest.fn();
eventBus.once(event, handler);
}); });
});
it('calls on internally', () => { describe('$once', () => {
expect(eventBus.on).toHaveBeenCalled(); beforeEach(() => {
eventBus.$once(TEST_EVENT, handler);
}); });
it('calls handler when event is emitted', () => { it('calls handler when event is emitted', () => {
eventBus.emit(event); eventBus.$emit(TEST_EVENT);
expect(handler).toHaveBeenCalled(); expect(handler).toHaveBeenCalled();
}); });
it('calls off when event is emitted', () => {
eventBus.emit(event);
expect(eventBus.off).toHaveBeenCalled();
});
it('calls the handler only once when event is emitted multiple times', () => { it('calls the handler only once when event is emitted multiple times', () => {
eventBus.emit(event); eventBus.$emit(TEST_EVENT);
eventBus.emit(event); eventBus.$emit(TEST_EVENT);
expect(handler).toHaveBeenCalledTimes(1); expect(handler).toHaveBeenCalledTimes(1);
}); });
...@@ -80,14 +87,18 @@ describe('event bus factory', () => { ...@@ -80,14 +87,18 @@ describe('event bus factory', () => {
handler = jest.fn().mockImplementation(() => { handler = jest.fn().mockImplementation(() => {
throw new Error(); throw new Error();
}); });
eventBus.once(event, handler); eventBus.$once(TEST_EVENT, handler);
}); });
it('calls off when event is emitted', () => { it('calls off when event is emitted', () => {
expect(() => { expect(() => {
eventBus.emit(event); eventBus.$emit(TEST_EVENT);
}).toThrow(); }).toThrow();
expect(eventBus.off).toHaveBeenCalled(); expect(() => {
eventBus.$emit(TEST_EVENT);
}).not.toThrow();
expect(handler).toHaveBeenCalledTimes(1);
}); });
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment