Skip to content

Commit

Permalink
Fix unbind on block destruct
Browse files Browse the repository at this point in the history
  • Loading branch information
belozer committed Jun 20, 2018
1 parent 9ab08cc commit d812b60
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,45 @@ describe('BEM events', function() {
});
});

describe('complex unbind on block destruct', function() {
var spy1, spy2, block1, block2, block3;

beforeEach(function() {
spy1 = sinon.spy();
spy2 = sinon.spy();

var Block = bemDom.declBlock('block'),
dom = initDom([
{ block : 'block' },
{ block : 'block' },
{ block : 'block' }
]);

block1 = dom.eq(0).bem(Block);
block2 = dom.eq(1).bem(Block);
block3 = dom.eq(2).bem(Block);
});

it('should properly unbind all handlers after destruct', function() {
block1._events(block2).on('click', spy1);
block1._events(block3).on('click', spy2);
block2._emit('click');
block3._emit('click');

spy1.should.have.been.called;
spy2.should.have.been.called;

spy1.reset();
spy2.reset();
bemDom.destruct(block1.domElem);
block2._emit('click');
block3._emit('click');

spy1.should.have.not.been.called;
spy2.should.have.not.been.called;
});
});

describe('nested blocks events', function() {
beforeEach(function() {
Block1 = bemDom.declBlock('block', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,51 @@ describe('DOM events', function() {
});
});

describe('complex unbind on block destruct', function() {
var spy1, spy2, spy3, block1, block2;
beforeEach(function() {
spy1 = sinon.spy();
spy2 = sinon.spy();
spy3 = sinon.spy();

var Block = bemDom.declBlock('block'),
dom = createDomNode([
{ block : 'block' },
{ block : 'block' }
]);

block1 = dom.eq(0).bem(Block);
block2 = dom.eq(1).bem(Block);
});

it('should properly unbind all handlers', function() {
block1._domEvents(bemDom.win).on('resize', spy1);
block1._domEvents(bemDom.doc).on('click', spy2);
block1._domEvents(block2).on('click', spy3);

bemDom.win.trigger('resize');
bemDom.doc.trigger('click');
block2.domElem.trigger('click');

spy1.should.have.been.called;
spy2.should.have.been.called;
spy3.should.have.been.called;

spy1.reset();
spy2.reset();
spy3.reset();

bemDom.destruct(block1.domElem);
bemDom.win.trigger('resize');
bemDom.doc.trigger('click');
block2.domElem.trigger('click');

spy1.should.have.not.been.called;
spy2.should.have.not.been.called;
spy3.should.have.not.been.called;
});
});

describe('block elems events', function() {
['string', 'Class'].forEach(function(elemType) {
var elem1, elem2;
Expand Down
7 changes: 4 additions & 3 deletions common.blocks/i-bem-dom/__events/i-bem-dom__events.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ var undef,
ctxStorage = eventStorage[ctxId] = {};
if(isBindToInstance) {
ctx._events().on({ modName : 'js', modVal : '' }, function() {
params.bindToArbitraryDomElem && ctxStorage[storageKey] &&
ctxStorage[storageKey].un();
delete ctxStorage[ctxId];
objects.each(ctxStorage, function(eventManager) {
eventManager.un();
});
delete eventStorage[ctxId];
});
}
}
Expand Down

0 comments on commit d812b60

Please sign in to comment.