2020-05-24 23:13:21 +05:30
|
|
|
import createEventHub from '~/helpers/event_hub_factory';
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
const TEST_EVENT = 'foobar';
|
|
|
|
const TEST_EVENT_2 = 'testevent';
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
describe('event bus factory', () => {
|
|
|
|
let eventBus;
|
2020-07-28 23:09:34 +05:30
|
|
|
let handler;
|
|
|
|
let otherHandlers;
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
eventBus = createEventHub();
|
2020-07-28 23:09:34 +05:30
|
|
|
handler = jest.fn();
|
|
|
|
otherHandlers = [jest.fn(), jest.fn()];
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.dispose();
|
2020-05-24 23:13:21 +05:30
|
|
|
eventBus = null;
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
describe('instance', () => {
|
|
|
|
it.each`
|
|
|
|
method
|
|
|
|
${'$on'}
|
|
|
|
${'$once'}
|
|
|
|
${'$off'}
|
|
|
|
${'$emit'}
|
|
|
|
`('has $method method', ({ method }) => {
|
|
|
|
expect(eventBus[method]).toEqual(expect.any(Function));
|
|
|
|
});
|
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
describe('$on', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
beforeEach(() => {
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$on(TEST_EVENT, handler);
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('calls handler when event is emitted', () => {
|
|
|
|
eventBus.$emit(TEST_EVENT);
|
|
|
|
expect(handler).toHaveBeenCalledWith();
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('calls handler with multiple args', () => {
|
|
|
|
eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
|
|
|
|
expect(handler).toHaveBeenCalledWith('arg1', 'arg2', 'arg3');
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('calls handler multiple times', () => {
|
|
|
|
eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
|
|
|
|
eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(handler).toHaveBeenCalledTimes(2);
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
describe('$once', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
eventBus.$once(TEST_EVENT, handler);
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('calls handler when event is emitted', () => {
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$emit(TEST_EVENT);
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(handler).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the handler only once when event is emitted multiple times', () => {
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$emit(TEST_EVENT);
|
|
|
|
eventBus.$emit(TEST_EVENT);
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(handler).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the handler thows an error', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
handler = jest.fn().mockImplementation(() => {
|
|
|
|
throw new Error();
|
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$once(TEST_EVENT, handler);
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('calls off when event is emitted', () => {
|
|
|
|
expect(() => {
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$emit(TEST_EVENT);
|
2020-05-24 23:13:21 +05:30
|
|
|
}).toThrow();
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(() => {
|
|
|
|
eventBus.$emit(TEST_EVENT);
|
|
|
|
}).not.toThrow();
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledTimes(1);
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
describe('$off', () => {
|
|
|
|
beforeEach(() => {
|
2021-03-08 18:12:59 +05:30
|
|
|
otherHandlers.forEach((x) => eventBus.$on(TEST_EVENT, x));
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$on(TEST_EVENT, handler);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be called on event with no handlers', () => {
|
|
|
|
expect(() => {
|
|
|
|
eventBus.$off(TEST_EVENT_2);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be called on event with no handlers, with a handler', () => {
|
|
|
|
expect(() => {
|
|
|
|
eventBus.$off(TEST_EVENT_2, handler);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with a handler, will no longer call that handler', () => {
|
|
|
|
eventBus.$off(TEST_EVENT, handler);
|
|
|
|
|
|
|
|
eventBus.$emit(TEST_EVENT);
|
|
|
|
|
|
|
|
expect(handler).not.toHaveBeenCalled();
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(otherHandlers.map((x) => x.mock.calls.length)).toEqual(otherHandlers.map(() => 1));
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('without a handler, will no longer call any handlers', () => {
|
|
|
|
eventBus.$off(TEST_EVENT);
|
|
|
|
|
|
|
|
eventBus.$emit(TEST_EVENT);
|
|
|
|
|
|
|
|
expect(handler).not.toHaveBeenCalled();
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(otherHandlers.map((x) => x.mock.calls.length)).toEqual(otherHandlers.map(() => 0));
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('$emit', () => {
|
|
|
|
beforeEach(() => {
|
2021-03-08 18:12:59 +05:30
|
|
|
otherHandlers.forEach((x) => eventBus.$on(TEST_EVENT_2, x));
|
2020-07-28 23:09:34 +05:30
|
|
|
eventBus.$on(TEST_EVENT, handler);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only calls handlers for given type', () => {
|
|
|
|
eventBus.$emit(TEST_EVENT, 'arg1');
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledWith('arg1');
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(otherHandlers.map((x) => x.mock.calls.length)).toEqual(otherHandlers.map(() => 0));
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|