Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Support listening to events triggered directly on the window #61

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion lib/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,16 @@ function matchesTag(tagName, element) {
*/
function matchesRoot(selector, element) {
/*jshint validthis:true*/
if (this.rootElement === window) return element === document;
if (this.rootElement === window) {
return (
// Match the outer document (dispatched from document)
element === document ||
// The <html> element (dispatched from document.body or document.documentElement)
element === document.documentElement ||
// Or the window itself (dispatched from window)
element === window
);
}
return this.rootElement === element;
}

Expand Down
62 changes: 61 additions & 1 deletion test/tests/delegateTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*jshint laxbreak:true*/

/*global buster, assert, refute, Delegate*/
/*global buster, Delegate*/

var assert = buster.referee.assert;
var refute = buster.referee.refute;

var setupHelper = {};

Expand Down Expand Up @@ -593,6 +596,63 @@ buster.testCase('Delegate', {
assert.calledOnce(captureSpy);
assert.calledOnce(bubbleSpy);
},
'Delegate instances on window catch events when bubbled from the body' : function() {
var delegate, spy;

delegate = new Delegate(window);
spy = this.spy();

delegate.on('click', spy);

setupHelper.fireMouseEvent(document.body, 'click');

assert.calledOnce(spy);

delegate.off();
},
'Delegate instances on window catch events when bubbled from the document' : function() {
var delegate, spy;

delegate = new Delegate(window);
spy = this.spy();

delegate.on('click', spy);

setupHelper.fireMouseEvent(document, 'click');

// assert.calledOnce(spy); // "Invalid Argument" in IE 9
assert(spy.callCount === 1); // Workaround for Buster.js IE 9 error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was VERY curious. I was getting "Invalid Argument" error in IE 9 when using the assert.calledOnce(spy) statement, but the error went away and the test passed when I used assert(spy.callCount === 1).

Looks like Buster needs better cross-browser testing ;)


delegate.off();
},
'Delegate instances on window catch events when bubbled from the <html> element' : function() {
var delegate, spy;

delegate = new Delegate(window);
spy = this.spy();

delegate.on('click', spy);

setupHelper.fireMouseEvent(document.documentElement, 'click');

assert.calledOnce(spy);

delegate.off();
},
'Delegate instances on window cause events when dispatched directly on window' : function() {
var delegate, spy;

delegate = new Delegate(window);
spy = this.spy();

delegate.on('click', spy);

setupHelper.fireMouseEvent(window, 'click');

assert.calledOnce(spy);

delegate.off();
},

'tearDown': function() {
setupHelper.tearDown();
Expand Down