Add EventBus unit-tests to ensure that the (optional) argument handling works correctly

This commit is contained in:
Jonas Jenwald 2018-09-21 14:12:01 +02:00
parent f317a2cb40
commit 39776168a2

View File

@ -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();
});