Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/helpers/event_hub_factory_spec.js')
-rw-r--r--spec/frontend/helpers/event_hub_factory_spec.js142
1 files changed, 96 insertions, 46 deletions
diff --git a/spec/frontend/helpers/event_hub_factory_spec.js b/spec/frontend/helpers/event_hub_factory_spec.js
index dcfec6b836a..c4f63ff6049 100644
--- a/spec/frontend/helpers/event_hub_factory_spec.js
+++ b/spec/frontend/helpers/event_hub_factory_spec.js
@@ -1,77 +1,72 @@
import createEventHub from '~/helpers/event_hub_factory';
+const TEST_EVENT = 'foobar';
+const TEST_EVENT_2 = 'testevent';
+
describe('event bus factory', () => {
let eventBus;
+ let handler;
+ let otherHandlers;
beforeEach(() => {
eventBus = createEventHub();
+ handler = jest.fn();
+ otherHandlers = [jest.fn(), jest.fn()];
});
afterEach(() => {
+ eventBus.dispose();
eventBus = null;
});
- describe('underlying module', () => {
- let mitt;
+ describe('instance', () => {
+ it.each`
+ method
+ ${'$on'}
+ ${'$once'}
+ ${'$off'}
+ ${'$emit'}
+ `('has $method method', ({ method }) => {
+ expect(eventBus[method]).toEqual(expect.any(Function));
+ });
+ });
+ describe('$on', () => {
beforeEach(() => {
- jest.resetModules();
- jest.mock('mitt');
-
- // eslint-disable-next-line global-require
- mitt = require('mitt');
- mitt.mockReturnValue(() => ({}));
-
- const createEventHubActual = jest.requireActual('~/helpers/event_hub_factory').default;
- eventBus = createEventHubActual();
+ eventBus.$on(TEST_EVENT, handler);
});
- it('creates an emitter', () => {
- expect(mitt).toHaveBeenCalled();
+ it('calls handler when event is emitted', () => {
+ eventBus.$emit(TEST_EVENT);
+ expect(handler).toHaveBeenCalledWith();
});
- });
- describe('instance', () => {
- it.each`
- method
- ${'on'}
- ${'once'}
- ${'off'}
- ${'emit'}
- `('binds $$method to $method ', ({ method }) => {
- expect(typeof eventBus[method]).toBe('function');
- expect(eventBus[method]).toBe(eventBus[`$${method}`]);
+ it('calls handler with multiple args', () => {
+ eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
+ expect(handler).toHaveBeenCalledWith('arg1', 'arg2', 'arg3');
});
- });
- describe('once', () => {
- const event = 'foobar';
- let handler;
+ it('calls handler multiple times', () => {
+ eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
+ eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
- beforeEach(() => {
- jest.spyOn(eventBus, 'on');
- jest.spyOn(eventBus, 'off');
- handler = jest.fn();
- eventBus.once(event, handler);
+ expect(handler).toHaveBeenCalledTimes(2);
});
+ });
- it('calls on internally', () => {
- expect(eventBus.on).toHaveBeenCalled();
+ describe('$once', () => {
+ beforeEach(() => {
+ eventBus.$once(TEST_EVENT, handler);
});
it('calls handler when event is emitted', () => {
- eventBus.emit(event);
+ eventBus.$emit(TEST_EVENT);
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', () => {
- eventBus.emit(event);
- eventBus.emit(event);
+ eventBus.$emit(TEST_EVENT);
+ eventBus.$emit(TEST_EVENT);
expect(handler).toHaveBeenCalledTimes(1);
});
@@ -80,15 +75,70 @@ describe('event bus factory', () => {
handler = jest.fn().mockImplementation(() => {
throw new Error();
});
- eventBus.once(event, handler);
+ eventBus.$once(TEST_EVENT, handler);
});
it('calls off when event is emitted', () => {
expect(() => {
- eventBus.emit(event);
+ eventBus.$emit(TEST_EVENT);
}).toThrow();
- expect(eventBus.off).toHaveBeenCalled();
+ expect(() => {
+ eventBus.$emit(TEST_EVENT);
+ }).not.toThrow();
+
+ expect(handler).toHaveBeenCalledTimes(1);
});
});
});
+
+ describe('$off', () => {
+ beforeEach(() => {
+ otherHandlers.forEach(x => eventBus.$on(TEST_EVENT, x));
+ 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();
+ expect(otherHandlers.map(x => x.mock.calls.length)).toEqual(otherHandlers.map(() => 1));
+ });
+
+ it('without a handler, will no longer call any handlers', () => {
+ eventBus.$off(TEST_EVENT);
+
+ eventBus.$emit(TEST_EVENT);
+
+ expect(handler).not.toHaveBeenCalled();
+ expect(otherHandlers.map(x => x.mock.calls.length)).toEqual(otherHandlers.map(() => 0));
+ });
+ });
+
+ describe('$emit', () => {
+ beforeEach(() => {
+ otherHandlers.forEach(x => eventBus.$on(TEST_EVENT_2, x));
+ eventBus.$on(TEST_EVENT, handler);
+ });
+
+ it('only calls handlers for given type', () => {
+ eventBus.$emit(TEST_EVENT, 'arg1');
+
+ expect(handler).toHaveBeenCalledWith('arg1');
+ expect(otherHandlers.map(x => x.mock.calls.length)).toEqual(otherHandlers.map(() => 0));
+ });
+ });
});