Skip to content

Commit

Permalink
Fix delegated events on window
Browse files Browse the repository at this point in the history
  • Loading branch information
tadatuta committed Jul 24, 2018
1 parent dd0ce4a commit 2499726
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var undef,
expect = chai.expect;

describe('DOM events', function() {
var Block1, Block2, Block3, block1, spy1, spy2, spy3, spy4, spy5, spy6, spy7, spy8,
var Block1, Block2, Block3, block1, spy1, spy2, spy3, spy4, spy5, spy6, spy7, spy8, spy9,
wrapSpy = function(spy) {
return function(e) {
// NOTE: we need to pass bemTarget and data explicitly, as `e` is being
Expand All @@ -26,6 +26,7 @@ describe('DOM events', function() {
spy6 = sinon.spy();
spy7 = sinon.spy();
spy8 = sinon.spy();
spy9 = sinon.spy();
});

afterEach(function() {
Expand Down Expand Up @@ -1038,6 +1039,38 @@ describe('DOM events', function() {
});
});
});

describe('window events', function() {
beforeEach(function() {
Block1 = bemDom.declBlock('block1', {}, {
onInit : function() {
this._domEvents(window).on('resize', spy9);
}
});

block1 = initBemEntity({ block : 'block1' }, Block1);
});

it('should properly bind handlers', function() {
windowDispatchEvent('resize');

spy9.should.have.been.called;
});

it('should properly unbind all handlers', function() {
Block1._domEvents(window).un('resize');
windowDispatchEvent('resize');

spy9.should.not.have.been.called;
});

it('should properly unbind specified handler', function() {
Block1._domEvents(window).un('resize', spy9);
windowDispatchEvent('resize');

spy9.should.not.have.been.called;
});
});
});
});

Expand Down
4 changes: 4 additions & 0 deletions common.blocks/i-bem-dom/__events/i-bem-dom__events.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ var undef,
} else {
do {
if(!targetDomNode) {
if(domNode === window) {
return { instance : window, targetDomNode : window };
}

if(domNode.classList.contains(params.bindClassName)) {
targetDomNode = domNode;
} else continue;
Expand Down

0 comments on commit 2499726

Please sign in to comment.