From 39776168a263b79837058c8a1a08b192bae84788 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 | 48 ++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/test/unit/ui_utils_spec.js b/test/unit/ui_utils_spec.js index eab665fe1..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,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({}); count++; } document.addEventListener('test', domEventListener); @@ -305,6 +321,30 @@ describe('ui_utils', function() { 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(); });