From f21161123bb223b416d32bc85a766d22ab4a9ef7 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 21 Sep 2018 14:12:01 +0200 Subject: [PATCH] Add `EventBus` unit-tests to ensure that the (optional) argument handling works correctly --- test/unit/ui_utils_spec.js | 47 ++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/test/unit/ui_utils_spec.js b/test/unit/ui_utils_spec.js index eab665fe13bab8..5cecf2b745b52a 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.abc).toEqual(123); + count++; + }); + eventBus.dispatch('test', { + abc: 123, + }); + expect(count).toEqual(1); + }); it('dispatch different event', function () { var eventBus = new EventBus(); var count = 0; @@ -269,7 +282,8 @@ describe('ui_utils', function() { } const eventBus = new EventBus(); let count = 0; - eventBus.on('test', function() { + eventBus.on('test', function(evt) { + expect(evt).toEqual(undefined); count++; }); function domEventListener() { @@ -292,10 +306,12 @@ 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++; }); - function domEventListener() { + function domEventListener(evt) { + expect(evt.detail).toEqual(Object.create(null)); count++; } document.addEventListener('test', domEventListener); @@ -305,6 +321,29 @@ describe('ui_utils', function() { Promise.resolve().then(() => { expect(count).toEqual(2); + document.removeEventListener('test', domEventListener); + done(); + }); + }); + it('should re-dispatch to DOM, 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.abc).toEqual(123); + count++; + } + document.addEventListener('test', domEventListener); + + eventBus.dispatch('test', { + abc: 123, + }); + + Promise.resolve().then(() => { + expect(count).toEqual(1); + document.removeEventListener('test', domEventListener); done(); });