Skip to content

Commit

Permalink
Add EventBus unit-tests to ensure that the (optional) argument hand…
Browse files Browse the repository at this point in the history
…ling works correctly
  • Loading branch information
Snuffleupagus committed Sep 21, 2018
1 parent f317a2c commit 3977616
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions test/unit/ui_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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();
});
Expand Down

0 comments on commit 3977616

Please sign in to comment.