diff --git a/test/unit/ui_utils_spec.js b/test/unit/ui_utils_spec.js index 4ba2a22e0..145b30d6a 100644 --- a/test/unit/ui_utils_spec.js +++ b/test/unit/ui_utils_spec.js @@ -182,12 +182,25 @@ describe('ui_utils', function() { it('dispatch event', function () { var eventBus = new EventBus(); var count = 0; - eventBus.on('test', function () { + eventBus.on('test', function(evt) { + expect(evt).toEqual(undefined); count++; }); eventBus.dispatch('test'); expect(count).toEqual(1); }); + it('dispatch event with arguments', function() { + const eventBus = new EventBus(); + let count = 0; + eventBus.on('test', function(evt) { + expect(evt).toEqual({ abc: 123, }); + count++; + }); + eventBus.dispatch('test', { + abc: 123, + }); + expect(count).toEqual(1); + }); it('dispatch different event', function () { var eventBus = new EventBus(); var count = 0; @@ -269,16 +282,21 @@ describe('ui_utils', function() { } const eventBus = new EventBus(); let count = 0; - eventBus.on('test', function() { - count++; - }); - document.addEventListener('test', function() { + eventBus.on('test', function(evt) { + expect(evt).toEqual(undefined); count++; }); + function domEventListener() { + done.fail('shall not dispatch DOM event.'); + } + document.addEventListener('test', domEventListener); + eventBus.dispatch('test'); Promise.resolve().then(() => { expect(count).toEqual(1); + + document.removeEventListener('test', domEventListener); done(); }); }); @@ -288,16 +306,46 @@ describe('ui_utils', function() { } const eventBus = new EventBus({ dispatchToDOM: true, }); let count = 0; - eventBus.on('test', function() { + eventBus.on('test', function(evt) { + expect(evt).toEqual(undefined); count++; }); - document.addEventListener('test', function() { + function domEventListener(evt) { + expect(evt.detail).toEqual({}); count++; - }); + } + document.addEventListener('test', domEventListener); + eventBus.dispatch('test'); Promise.resolve().then(() => { expect(count).toEqual(2); + + document.removeEventListener('test', domEventListener); + done(); + }); + }); + it('should re-dispatch to DOM, with arguments (without internal listeners)', + function(done) { + if (isNodeJS()) { + pending('Document in not supported in Node.js.'); + } + const eventBus = new EventBus({ dispatchToDOM: true, }); + let count = 0; + function domEventListener(evt) { + expect(evt.detail).toEqual({ abc: 123, }); + count++; + } + document.addEventListener('test', domEventListener); + + eventBus.dispatch('test', { + abc: 123, + }); + + Promise.resolve().then(() => { + expect(count).toEqual(1); + + document.removeEventListener('test', domEventListener); done(); }); }); diff --git a/web/ui_utils.js b/web/ui_utils.js index d0e59d58d..c0c11f277 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -708,12 +708,13 @@ class EventBus { let eventListeners = this._listeners[eventName]; if (!eventListeners || eventListeners.length === 0) { if (this._dispatchToDOM) { - this._dispatchDOMEvent(eventName); + const args = Array.prototype.slice.call(arguments, 1); + this._dispatchDOMEvent(eventName, args); } return; } // Passing all arguments after the eventName to the listeners. - let args = Array.prototype.slice.call(arguments, 1); + const args = Array.prototype.slice.call(arguments, 1); // Making copy of the listeners array in case if it will be modified // during dispatch. eventListeners.slice(0).forEach(function (listener) {