Skip to content

Commit

Permalink
Merge pull request #65 from canjs/cleanup-listeners
Browse files Browse the repository at this point in the history
ensuring all listeners are cleaned up for event delegation tests
  • Loading branch information
phillipskevin authored Apr 5, 2019
2 parents 475ac6b + 85959b9 commit 75c2117
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions can-dom-events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ unit.test('domEvents.addDelegateListener handles document correctly', function (
assert.ok(true, 'works');
});

require('./helpers/make-event-registry-test');
require('./helpers/add-event-compat-test');
require('./helpers/add-event-jquery-test');
require('./helpers/add-jquery-events-test');
require('./helpers/util-test');

unit.test('domEvents.addDelegateListener call inner-most handler first (#62)', function (assert) {
var done = assert.async();
var grandparent = document.createElement('div');
Expand All @@ -201,19 +195,21 @@ unit.test('domEvents.addDelegateListener call inner-most handler first (#62)', f
var handlerCallCount = 0;

var paragraphClickHandler = function paragraphClickHandler() {
domEvents.removeDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
assert.equal(++handlerCallCount, 2, 'outer handler called second');
};

var inputClickHandler = function inputClickHandler() {
domEvents.removeDelegateListener(grandparent, 'click', 'input', inputClickHandler);
assert.equal(++handlerCallCount, 1, 'inner handler called first');
};

domEvents.addDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
domEvents.addDelegateListener(grandparent, 'click', 'input', inputClickHandler);

domEvents.dispatch(child, 'click');

domEvents.removeDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
domEvents.removeDelegateListener(grandparent, 'click', 'input', inputClickHandler);

done();
});

Expand All @@ -228,20 +224,22 @@ unit.test('domEvents.addDelegateListener call inner-most handler first (#62)', f
parent.appendChild(child);

var paragraphClickHandler = function paragraphClickHandler() {
domEvents.removeDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
assert.ok(false, stopMethod + ' works');
};

var inputClickHandler = function inputClickHandler(event) {
event[stopMethod]();
domEvents.removeDelegateListener(grandparent, 'click', 'input', inputClickHandler);
assert.ok(true, 'inner-most click handler called first');
};

domEvents.addDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
domEvents.addDelegateListener(grandparent, 'click', 'input', inputClickHandler);

domEvents.dispatch(child, 'click');

domEvents.removeDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
domEvents.removeDelegateListener(grandparent, 'click', 'input', inputClickHandler);

done();
});

Expand All @@ -257,19 +255,27 @@ unit.test('domEvents.addDelegateListener call inner-most handler first (#62)', f

var delegatedClickHandler = function delegatedClickHandler(event) {
event[stopMethod]();
domEvents.removeDelegateListener(parent, 'click', 'input', delegatedClickHandler);
assert.ok(true, 'inner-most click handler called first');
};

var documentClickHandler = function documentClickHandler() {
domEvents.removeEventListener(document, 'click', documentClickHandler);
assert.ok(false, stopMethod + ' works');
};

domEvents.addDelegateListener(parent, 'click', 'input', delegatedClickHandler);
domEvents.addEventListener(document, 'click', documentClickHandler);

domEvents.dispatch(child, 'click');

domEvents.removeDelegateListener(parent, 'click', 'input', delegatedClickHandler);
domEvents.removeEventListener(document, 'click', documentClickHandler);

done();
});
});

require('./helpers/make-event-registry-test');
require('./helpers/add-event-compat-test');
require('./helpers/add-event-jquery-test');
require('./helpers/add-jquery-events-test');
require('./helpers/util-test');

0 comments on commit 75c2117

Please sign in to comment.