Merge pull request #10090 from Snuffleupagus/pr-10019-followup
Ensure that all event properties are included, even if no (internal) listeners are registered, when re-dispatching events to the DOM (PR 10019 follow-up)
This commit is contained in:
commit
ad6ab88fdf
@ -182,12 +182,25 @@ describe('ui_utils', function() {
|
|||||||
it('dispatch event', function () {
|
it('dispatch event', function () {
|
||||||
var eventBus = new EventBus();
|
var eventBus = new EventBus();
|
||||||
var count = 0;
|
var count = 0;
|
||||||
eventBus.on('test', function () {
|
eventBus.on('test', function(evt) {
|
||||||
|
expect(evt).toEqual(undefined);
|
||||||
count++;
|
count++;
|
||||||
});
|
});
|
||||||
eventBus.dispatch('test');
|
eventBus.dispatch('test');
|
||||||
expect(count).toEqual(1);
|
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 () {
|
it('dispatch different event', function () {
|
||||||
var eventBus = new EventBus();
|
var eventBus = new EventBus();
|
||||||
var count = 0;
|
var count = 0;
|
||||||
@ -269,16 +282,21 @@ describe('ui_utils', function() {
|
|||||||
}
|
}
|
||||||
const eventBus = new EventBus();
|
const eventBus = new EventBus();
|
||||||
let count = 0;
|
let count = 0;
|
||||||
eventBus.on('test', function() {
|
eventBus.on('test', function(evt) {
|
||||||
count++;
|
expect(evt).toEqual(undefined);
|
||||||
});
|
|
||||||
document.addEventListener('test', function() {
|
|
||||||
count++;
|
count++;
|
||||||
});
|
});
|
||||||
|
function domEventListener() {
|
||||||
|
done.fail('shall not dispatch DOM event.');
|
||||||
|
}
|
||||||
|
document.addEventListener('test', domEventListener);
|
||||||
|
|
||||||
eventBus.dispatch('test');
|
eventBus.dispatch('test');
|
||||||
|
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
expect(count).toEqual(1);
|
expect(count).toEqual(1);
|
||||||
|
|
||||||
|
document.removeEventListener('test', domEventListener);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -288,16 +306,46 @@ describe('ui_utils', function() {
|
|||||||
}
|
}
|
||||||
const eventBus = new EventBus({ dispatchToDOM: true, });
|
const eventBus = new EventBus({ dispatchToDOM: true, });
|
||||||
let count = 0;
|
let count = 0;
|
||||||
eventBus.on('test', function() {
|
eventBus.on('test', function(evt) {
|
||||||
|
expect(evt).toEqual(undefined);
|
||||||
count++;
|
count++;
|
||||||
});
|
});
|
||||||
document.addEventListener('test', function() {
|
function domEventListener(evt) {
|
||||||
|
expect(evt.detail).toEqual({});
|
||||||
count++;
|
count++;
|
||||||
});
|
}
|
||||||
|
document.addEventListener('test', domEventListener);
|
||||||
|
|
||||||
eventBus.dispatch('test');
|
eventBus.dispatch('test');
|
||||||
|
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
expect(count).toEqual(2);
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -708,12 +708,13 @@ class EventBus {
|
|||||||
let eventListeners = this._listeners[eventName];
|
let eventListeners = this._listeners[eventName];
|
||||||
if (!eventListeners || eventListeners.length === 0) {
|
if (!eventListeners || eventListeners.length === 0) {
|
||||||
if (this._dispatchToDOM) {
|
if (this._dispatchToDOM) {
|
||||||
this._dispatchDOMEvent(eventName);
|
const args = Array.prototype.slice.call(arguments, 1);
|
||||||
|
this._dispatchDOMEvent(eventName, args);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Passing all arguments after the eventName to the listeners.
|
// Passing all arguments after the eventName to the listeners.
|
||||||
let args = Array.prototype.slice.call(arguments, 1);
|
const args = Array.prototype.slice.call(arguments, 1);
|
||||||
// Making copy of the listeners array in case if it will be modified
|
// Making copy of the listeners array in case if it will be modified
|
||||||
// during dispatch.
|
// during dispatch.
|
||||||
eventListeners.slice(0).forEach(function (listener) {
|
eventListeners.slice(0).forEach(function (listener) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user