Skip to content

Commit

Permalink
check for cleaned up references in listenToOnce with stopListening
Browse files Browse the repository at this point in the history
  • Loading branch information
akre54 committed Nov 3, 2014
1 parent 3c0cafb commit 7409461
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@
equal(_.size(b._events), 0);
});

test("stopListening cleans up references from listenToOnce", 8, function() {
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var fn = function() {};
a.listenToOnce(b, 'event', fn).stopListening();
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events), 0);
a.listenToOnce(b, 'event', fn).stopListening(b);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events), 0);
a.listenToOnce(b, 'event', fn).stopListening(b, 'event');
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events), 0);
a.listenToOnce(b, 'event', fn).stopListening(b, 'event', fn);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events), 0);
});

test("listenTo and stopListening cleaning up references", 2, function() {
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
Expand All @@ -193,13 +211,23 @@
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, {
one: function() { ok(this === a); },
one: function() { ok(true); },
two: function() { ok(false); }
});
b.trigger('one');
equal(_.keys(a._listeningTo).length, 1);
});

test("listenToOnce with event maps binds the correct `this`", 1, function() {
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, {
one: function() { ok(this === a); },
two: function() { ok(false); }
});
b.trigger('one');
});

test("listenToOnce, stopListening cleans up references", 8, function() {
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
Expand Down

0 comments on commit 7409461

Please sign in to comment.