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 f211611
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 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.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;
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(Object.create(null));
count++;
}
document.addEventListener('test', domEventListener);
Expand All @@ -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();
});
Expand Down

0 comments on commit f211611

Please sign in to comment.