events.js (lodash-4.0.0) | : | events.js (lodash-4.17.21) | ||
---|---|---|---|---|
(function() { | (function(QUnit) { | |||
QUnit.module('Backbone.Events'); | QUnit.module('Backbone.Events'); | |||
QUnit.test('on and trigger', function(assert) { | QUnit.test('on and trigger', function(assert) { | |||
assert.expect(2); | assert.expect(2); | |||
var obj = {counter: 0}; | var obj = {counter: 0}; | |||
_.extend(obj, Backbone.Events); | _.extend(obj, Backbone.Events); | |||
obj.on('event', function() { obj.counter += 1; }); | obj.on('event', function() { obj.counter += 1; }); | |||
obj.trigger('event'); | obj.trigger('event'); | |||
assert.equal(obj.counter, 1, 'counter should be incremented.'); | assert.equal(obj.counter, 1, 'counter should be incremented.'); | |||
skipping to change at line 355 | skipping to change at line 355 | |||
e.trigger('foo'); | e.trigger('foo'); | |||
assert.ok(true); | assert.ok(true); | |||
}); | }); | |||
QUnit.test('trigger all for each event', function(assert) { | QUnit.test('trigger all for each event', function(assert) { | |||
assert.expect(3); | assert.expect(3); | |||
var a, b, obj = {counter: 0}; | var a, b, obj = {counter: 0}; | |||
_.extend(obj, Backbone.Events); | _.extend(obj, Backbone.Events); | |||
obj.on('all', function(event) { | obj.on('all', function(event) { | |||
obj.counter++; | obj.counter++; | |||
if (event == 'a') a = true; | if (event === 'a') a = true; | |||
if (event == 'b') b = true; | if (event === 'b') b = true; | |||
}) | }) | |||
.trigger('a b'); | .trigger('a b'); | |||
assert.ok(a); | assert.ok(a); | |||
assert.ok(b); | assert.ok(b); | |||
assert.equal(obj.counter, 2); | assert.equal(obj.counter, 2); | |||
}); | }); | |||
QUnit.test('on, then unbind all functions', function(assert) { | QUnit.test('on, then unbind all functions', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
var obj = {counter: 0}; | var obj = {counter: 0}; | |||
skipping to change at line 420 | skipping to change at line 420 | |||
var incrB = function(){ obj.counterB += 1; obj.off('event', incrB); }; | var incrB = function(){ obj.counterB += 1; obj.off('event', incrB); }; | |||
obj.on('event', incrA); | obj.on('event', incrA); | |||
obj.on('event', incrB); | obj.on('event', incrB); | |||
obj.trigger('event'); | obj.trigger('event'); | |||
obj.trigger('event'); | obj.trigger('event'); | |||
obj.trigger('event'); | obj.trigger('event'); | |||
assert.equal(obj.counterA, 1, 'counterA should have only been incremented on ce.'); | assert.equal(obj.counterA, 1, 'counterA should have only been incremented on ce.'); | |||
assert.equal(obj.counterB, 1, 'counterB should have only been incremented on ce.'); | assert.equal(obj.counterB, 1, 'counterB should have only been incremented on ce.'); | |||
}); | }); | |||
QUnit.test('bind a callback with a default context when none supplied', functi | ||||
on(assert) { | ||||
assert.expect(1); | ||||
var obj = _.extend({ | ||||
assertTrue: function() { | ||||
assert.equal(this, obj, '`this` was bound to the callback'); | ||||
} | ||||
}, Backbone.Events); | ||||
obj.once('event', obj.assertTrue); | ||||
obj.trigger('event'); | ||||
}); | ||||
QUnit.test('bind a callback with a supplied context', function(assert) { | QUnit.test('bind a callback with a supplied context', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
var TestClass = function() { | var TestClass = function() { | |||
return this; | return this; | |||
}; | }; | |||
TestClass.prototype.assertTrue = function() { | TestClass.prototype.assertTrue = function() { | |||
assert.ok(true, '`this` was bound to the callback'); | assert.ok(true, '`this` was bound to the callback'); | |||
}; | }; | |||
var obj = _.extend({}, Backbone.Events); | var obj = _.extend({}, Backbone.Events); | |||
skipping to change at line 480 | skipping to change at line 492 | |||
}); | }); | |||
QUnit.test('if no callback is provided, `on` is a noop', function(assert) { | QUnit.test('if no callback is provided, `on` is a noop', function(assert) { | |||
assert.expect(0); | assert.expect(0); | |||
_.extend({}, Backbone.Events).on('test').trigger('test'); | _.extend({}, Backbone.Events).on('test').trigger('test'); | |||
}); | }); | |||
QUnit.test('if callback is truthy but not a function, `on` should throw an err or just like jQuery', function(assert) { | QUnit.test('if callback is truthy but not a function, `on` should throw an err or just like jQuery', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
var view = _.extend({}, Backbone.Events).on('test', 'noop'); | var view = _.extend({}, Backbone.Events).on('test', 'noop'); | |||
assert.throws(function() { | assert.raises(function() { | |||
view.trigger('test'); | view.trigger('test'); | |||
}); | }); | |||
}); | }); | |||
QUnit.test('remove all events for a specific context', function(assert) { | QUnit.test('remove all events for a specific context', function(assert) { | |||
assert.expect(4); | assert.expect(4); | |||
var obj = _.extend({}, Backbone.Events); | var obj = _.extend({}, Backbone.Events); | |||
obj.on('x y all', function() { assert.ok(true); }); | obj.on('x y all', function() { assert.ok(true); }); | |||
obj.on('x y all', function() { assert.ok(false); }, obj); | obj.on('x y all', function() { assert.ok(false); }, obj); | |||
obj.off(null, null, obj); | obj.off(null, null, obj); | |||
skipping to change at line 590 | skipping to change at line 602 | |||
obj.trigger('a b'); | obj.trigger('a b'); | |||
assert.equal(obj.counter, 2); | assert.equal(obj.counter, 2); | |||
obj.trigger('c'); | obj.trigger('c'); | |||
assert.equal(obj.counter, 3); | assert.equal(obj.counter, 3); | |||
obj.trigger('a b c'); | obj.trigger('a b c'); | |||
assert.equal(obj.counter, 3); | assert.equal(obj.counter, 3); | |||
}); | }); | |||
QUnit.test('bind a callback with a supplied context using once with object not | ||||
ation', function(assert) { | ||||
assert.expect(1); | ||||
var obj = {counter: 0}; | ||||
var context = {}; | ||||
_.extend(obj, Backbone.Events); | ||||
obj.once({ | ||||
a: function() { | ||||
assert.strictEqual(this, context, 'defaults `context` to `callback` para | ||||
m'); | ||||
} | ||||
}, context).trigger('a'); | ||||
}); | ||||
QUnit.test('once with off only by context', function(assert) { | QUnit.test('once with off only by context', function(assert) { | |||
assert.expect(0); | assert.expect(0); | |||
var context = {}; | var context = {}; | |||
var obj = _.extend({}, Backbone.Events); | var obj = _.extend({}, Backbone.Events); | |||
obj.once('event', function(){ assert.ok(false); }, context); | obj.once('event', function(){ assert.ok(false); }, context); | |||
obj.off(null, null, context); | obj.off(null, null, context); | |||
obj.trigger('event'); | obj.trigger('event'); | |||
}); | }); | |||
QUnit.test('Backbone object inherits Events', function(assert) { | QUnit.test('Backbone object inherits Events', function(assert) { | |||
skipping to change at line 681 | skipping to change at line 706 | |||
var one = _.extend({}, Backbone.Events); | var one = _.extend({}, Backbone.Events); | |||
var two = _.extend({}, Backbone.Events); | var two = _.extend({}, Backbone.Events); | |||
var count = 1; | var count = 1; | |||
one.listenToOnce(two, 'x y', function(n) { assert.ok(n === count++); }); | one.listenToOnce(two, 'x y', function(n) { assert.ok(n === count++); }); | |||
two.trigger('x', 1); | two.trigger('x', 1); | |||
two.trigger('x', 1); | two.trigger('x', 1); | |||
two.trigger('y', 2); | two.trigger('y', 2); | |||
two.trigger('y', 2); | two.trigger('y', 2); | |||
}); | }); | |||
})(); | })(QUnit); | |||
End of changes. 6 change blocks. | ||||
4 lines changed or deleted | 32 lines changed or added |