Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(events): triggering with an object had incorrect target property on event object #4993

Merged
merged 3 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/js/utils/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ export function trigger(elem, event, hash) {
// If an event name was passed as a string, creates an event out of it
if (typeof event === 'string') {
event = {type: event, target: elem};
} else if (!event.target) {
event.target = elem;
}

// Normalizes the event properties.
event = fixEvent(event);

Expand Down
37 changes: 37 additions & 0 deletions test/unit/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,40 @@ QUnit.test('should execute remaining handlers after an exception in an event han

log.error = oldLogError;
});

QUnit.test('trigger with an object should set the correct target property', function(assert) {
const el = document.createElement('div');

Events.on(el, 'click', function(e) {
assert.equal(e.target, el, 'the event object target should be our element');
});
Events.trigger(el, { type: 'click'});
});

QUnit.test('retrigger with a string should use the new element as target', function(assert) {
const el1 = document.createElement('div');
const el2 = document.createElement('div');

Events.on(el2, 'click', function(e) {
assert.equal(e.target, el2, 'the event object target should be the new element');
});
Events.on(el1, 'click', function(e) {
Events.trigger(el2, 'click');
});
Events.trigger(el1, 'click');
Events.trigger(el1, {type: 'click'});
});

QUnit.test('retrigger with an object should use the old element as target', function(assert) {
const el1 = document.createElement('div');
const el2 = document.createElement('div');

Events.on(el2, 'click', function(e) {
assert.equal(e.target, el1, 'the event object target should be the old element');
});
Events.on(el1, 'click', function(e) {
Events.trigger(el2, e);
});
Events.trigger(el1, 'click');
Events.trigger(el1, {type: 'click'});
});