Update vendor/backbone and vendor/underscore.

This commit is contained in:
John-David Dalton
2015-12-10 00:22:20 -08:00
parent 2192b7748e
commit 08568fcc8f
18 changed files with 3486 additions and 2939 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,41 +1,43 @@
(function() {
module("Backbone.Events");
QUnit.module("Backbone.Events");
test("on and trigger", 2, function() {
QUnit.test("on and trigger", function(assert) {
assert.expect(2);
var obj = { counter: 0 };
_.extend(obj,Backbone.Events);
obj.on('event', function() { obj.counter += 1; });
obj.trigger('event');
equal(obj.counter,1,'counter should be incremented.');
assert.equal(obj.counter, 1, 'counter should be incremented.');
obj.trigger('event');
obj.trigger('event');
obj.trigger('event');
obj.trigger('event');
equal(obj.counter, 5, 'counter should be incremented five times.');
assert.equal(obj.counter, 5, 'counter should be incremented five times.');
});
test("binding and triggering multiple events", 4, function() {
QUnit.test("binding and triggering multiple events", function(assert) {
assert.expect(4);
var obj = { counter: 0 };
_.extend(obj, Backbone.Events);
obj.on('a b c', function() { obj.counter += 1; });
obj.trigger('a');
equal(obj.counter, 1);
assert.equal(obj.counter, 1);
obj.trigger('a b');
equal(obj.counter, 3);
assert.equal(obj.counter, 3);
obj.trigger('c');
equal(obj.counter, 4);
assert.equal(obj.counter, 4);
obj.off('a c');
obj.trigger('a b c');
equal(obj.counter, 5);
assert.equal(obj.counter, 5);
});
test("binding and triggering with event maps", function() {
QUnit.test("binding and triggering with event maps", function(assert) {
var obj = { counter: 0 };
_.extend(obj, Backbone.Events);
@@ -50,23 +52,23 @@
}, obj);
obj.trigger('a');
equal(obj.counter, 1);
assert.equal(obj.counter, 1);
obj.trigger('a b');
equal(obj.counter, 3);
assert.equal(obj.counter, 3);
obj.trigger('c');
equal(obj.counter, 4);
assert.equal(obj.counter, 4);
obj.off({
a: increment,
c: increment
}, obj);
obj.trigger('a b c');
equal(obj.counter, 5);
assert.equal(obj.counter, 5);
});
test("binding and triggering multiple event names with event maps", function() {
QUnit.test("binding and triggering multiple event names with event maps", function(assert) {
var obj = { counter: 0 };
_.extend(obj, Backbone.Events);
@@ -79,29 +81,30 @@
});
obj.trigger('a');
equal(obj.counter, 1);
assert.equal(obj.counter, 1);
obj.trigger('a b');
equal(obj.counter, 3);
assert.equal(obj.counter, 3);
obj.trigger('c');
equal(obj.counter, 4);
assert.equal(obj.counter, 4);
obj.off({
'a c': increment
});
obj.trigger('a b c');
equal(obj.counter, 5);
assert.equal(obj.counter, 5);
});
test("binding and trigger with event maps context", 2, function() {
QUnit.test("binding and trigger with event maps context", function(assert) {
assert.expect(2);
var obj = { counter: 0 };
var context = {};
_.extend(obj, Backbone.Events);
obj.on({
a: function() {
strictEqual(this, context, 'defaults `context` to `callback` param');
assert.strictEqual(this, context, 'defaults `context` to `callback` param');
}
}, context).trigger('a');
@@ -112,20 +115,22 @@
}, this, context).trigger('a');
});
test("listenTo and stopListening", 1, function() {
QUnit.test("listenTo and stopListening", function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenTo(b, 'all', function(){ ok(true); });
a.listenTo(b, 'all', function(){ assert.ok(true); });
b.trigger('anything');
a.listenTo(b, 'all', function(){ ok(false); });
a.listenTo(b, 'all', function(){ assert.ok(false); });
a.stopListening();
b.trigger('anything');
});
test("listenTo and stopListening with event maps", 4, function() {
QUnit.test("listenTo and stopListening with event maps", function(assert) {
assert.expect(4);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var cb = function(){ ok(true); };
var cb = function(){ assert.ok(true); };
a.listenTo(b, {event: cb});
b.trigger('event');
a.listenTo(b, {event2: cb});
@@ -136,10 +141,11 @@
b.trigger('event event2');
});
test("stopListening with omitted args", 2, function () {
QUnit.test("stopListening with omitted args", function(assert) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var cb = function () { ok(true); };
var cb = function () { assert.ok(true); };
a.listenTo(b, 'event', cb);
b.on('event', cb);
a.listenTo(b, 'event2', cb);
@@ -152,7 +158,8 @@
b.trigger('event2');
});
test("listenToOnce", 2, function() {
QUnit.test("listenToOnce", function(assert) {
assert.expect(2);
// Same as the previous test, but we use once rather than having to explicitly unbind
var obj = { counterA: 0, counterB: 0 };
_.extend(obj, Backbone.Events);
@@ -161,172 +168,186 @@
obj.listenToOnce(obj, 'event', incrA);
obj.listenToOnce(obj, 'event', incrB);
obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.');
equal(obj.counterB, 1, 'counterB should have only been incremented once.');
assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.');
assert.equal(obj.counterB, 1, 'counterB should have only been incremented once.');
});
test("listenToOnce and stopListening", 1, function() {
QUnit.test("listenToOnce and stopListening", function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, 'all', function() { ok(true); });
a.listenToOnce(b, 'all', function() { assert.ok(true); });
b.trigger('anything');
b.trigger('anything');
a.listenToOnce(b, 'all', function() { ok(false); });
a.listenToOnce(b, 'all', function() { assert.ok(false); });
a.stopListening();
b.trigger('anything');
});
test("listenTo, listenToOnce and stopListening", 1, function() {
QUnit.test("listenTo, listenToOnce and stopListening", function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, 'all', function() { ok(true); });
a.listenToOnce(b, 'all', function() { assert.ok(true); });
b.trigger('anything');
b.trigger('anything');
a.listenTo(b, 'all', function() { ok(false); });
a.listenTo(b, 'all', function() { assert.ok(false); });
a.stopListening();
b.trigger('anything');
});
test("listenTo and stopListening with event maps", 1, function() {
QUnit.test("listenTo and stopListening with event maps", function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenTo(b, {change: function(){ ok(true); }});
a.listenTo(b, {change: function(){ assert.ok(true); }});
b.trigger('change');
a.listenTo(b, {change: function(){ ok(false); }});
a.listenTo(b, {change: function(){ assert.ok(false); }});
a.stopListening();
b.trigger('change');
});
test("listenTo yourself", 1, function(){
QUnit.test("listenTo yourself", function(assert) {
assert.expect(1);
var e = _.extend({}, Backbone.Events);
e.listenTo(e, "foo", function(){ ok(true); });
e.listenTo(e, "foo", function(){ assert.ok(true); });
e.trigger("foo");
});
test("listenTo yourself cleans yourself up with stopListening", 1, function(){
QUnit.test("listenTo yourself cleans yourself up with stopListening", function(assert) {
assert.expect(1);
var e = _.extend({}, Backbone.Events);
e.listenTo(e, "foo", function(){ ok(true); });
e.listenTo(e, "foo", function(){ assert.ok(true); });
e.trigger("foo");
e.stopListening();
e.trigger("foo");
});
test("stopListening cleans up references", 12, function() {
QUnit.test("stopListening cleans up references", function(assert) {
assert.expect(12);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var fn = function() {};
b.on('event', fn);
a.listenTo(b, 'event', fn).stopListening();
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn).stopListening(b);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn).stopListening(b, 'event');
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn).stopListening(b, 'event', fn);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
});
test("stopListening cleans up references from listenToOnce", 12, function() {
QUnit.test("stopListening cleans up references from listenToOnce", function(assert) {
assert.expect(12);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var fn = function() {};
b.on('event', fn);
a.listenToOnce(b, 'event', fn).stopListening();
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenToOnce(b, 'event', fn).stopListening(b);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenToOnce(b, 'event', fn).stopListening(b, 'event');
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenToOnce(b, 'event', fn).stopListening(b, 'event', fn);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._events.event), 1);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
});
test("listenTo and off cleaning up references", 8, function() {
QUnit.test("listenTo and off cleaning up references", function(assert) {
assert.expect(8);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var fn = function() {};
a.listenTo(b, 'event', fn);
b.off();
equal(_.size(a._listeningTo), 0);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn);
b.off('event');
equal(_.size(a._listeningTo), 0);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn);
b.off(null, fn);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn);
b.off(null, null, a);
equal(_.size(a._listeningTo), 0);
equal(_.size(b._listeners), 0);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
});
test("listenTo and stopListening cleaning up references", 2, function() {
QUnit.test("listenTo and stopListening cleaning up references", function(assert) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenTo(b, 'all', function(){ ok(true); });
a.listenTo(b, 'all', function(){ assert.ok(true); });
b.trigger('anything');
a.listenTo(b, 'other', function(){ ok(false); });
a.listenTo(b, 'other', function(){ assert.ok(false); });
a.stopListening(b, 'other');
a.stopListening(b, 'all');
equal(_.size(a._listeningTo), 0);
assert.equal(_.size(a._listeningTo), 0);
});
test("listenToOnce without context cleans up references after the event has fired", 2, function() {
QUnit.test("listenToOnce without context cleans up references after the event has fired", function(assert) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, 'all', function(){ ok(true); });
a.listenToOnce(b, 'all', function(){ assert.ok(true); });
b.trigger('anything');
equal(_.size(a._listeningTo), 0);
assert.equal(_.size(a._listeningTo), 0);
});
test("listenToOnce with event maps cleans up references", 2, function() {
QUnit.test("listenToOnce with event maps cleans up references", function(assert) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, {
one: function() { ok(true); },
two: function() { ok(false); }
one: function() { assert.ok(true); },
two: function() { assert.ok(false); }
});
b.trigger('one');
equal(_.size(a._listeningTo), 1);
assert.equal(_.size(a._listeningTo), 1);
});
test("listenToOnce with event maps binds the correct `this`", 1, function() {
QUnit.test("listenToOnce with event maps binds the correct `this`", function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, {
one: function() { ok(this === a); },
two: function() { ok(false); }
one: function() { assert.ok(this === a); },
two: function() { assert.ok(false); }
});
b.trigger('one');
});
test("listenTo with empty callback doesn't throw an error", 1, function(){
QUnit.test("listenTo with empty callback doesn't throw an error", function(assert) {
assert.expect(1);
var e = _.extend({}, Backbone.Events);
e.listenTo(e, "foo", null);
e.trigger("foo");
ok(true);
assert.ok(true);
});
test("trigger all for each event", 3, function() {
QUnit.test("trigger all for each event", function(assert) {
assert.expect(3);
var a, b, obj = { counter: 0 };
_.extend(obj, Backbone.Events);
obj.on('all', function(event) {
@@ -335,12 +356,13 @@
if (event == 'b') b = true;
})
.trigger('a b');
ok(a);
ok(b);
equal(obj.counter, 2);
assert.ok(a);
assert.ok(b);
assert.equal(obj.counter, 2);
});
test("on, then unbind all functions", 1, function() {
QUnit.test("on, then unbind all functions", function(assert) {
assert.expect(1);
var obj = { counter: 0 };
_.extend(obj,Backbone.Events);
var callback = function() { obj.counter += 1; };
@@ -348,10 +370,11 @@
obj.trigger('event');
obj.off('event');
obj.trigger('event');
equal(obj.counter, 1, 'counter should have only been incremented once.');
assert.equal(obj.counter, 1, 'counter should have only been incremented once.');
});
test("bind two callbacks, unbind only one", 2, function() {
QUnit.test("bind two callbacks, unbind only one", function(assert) {
assert.expect(2);
var obj = { counterA: 0, counterB: 0 };
_.extend(obj,Backbone.Events);
var callback = function() { obj.counterA += 1; };
@@ -360,11 +383,12 @@
obj.trigger('event');
obj.off('event', callback);
obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.');
equal(obj.counterB, 2, 'counterB should have been incremented twice.');
assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.');
assert.equal(obj.counterB, 2, 'counterB should have been incremented twice.');
});
test("unbind a callback in the midst of it firing", 1, function() {
QUnit.test("unbind a callback in the midst of it firing", function(assert) {
assert.expect(1);
var obj = {counter: 0};
_.extend(obj, Backbone.Events);
var callback = function() {
@@ -375,10 +399,11 @@
obj.trigger('event');
obj.trigger('event');
obj.trigger('event');
equal(obj.counter, 1, 'the callback should have been unbound.');
assert.equal(obj.counter, 1, 'the callback should have been unbound.');
});
test("two binds that unbind themeselves", 2, function() {
QUnit.test("two binds that unbind themeselves", function(assert) {
assert.expect(2);
var obj = { counterA: 0, counterB: 0 };
_.extend(obj,Backbone.Events);
var incrA = function(){ obj.counterA += 1; obj.off('event', incrA); };
@@ -388,16 +413,17 @@
obj.trigger('event');
obj.trigger('event');
obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.');
equal(obj.counterB, 1, 'counterB should have only been incremented once.');
assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.');
assert.equal(obj.counterB, 1, 'counterB should have only been incremented once.');
});
test("bind a callback with a supplied context", 1, function () {
QUnit.test("bind a callback with a supplied context", function(assert) {
assert.expect(1);
var TestClass = function () {
return this;
};
TestClass.prototype.assertTrue = function () {
ok(true, '`this` was bound to the callback');
assert.ok(true, '`this` was bound to the callback');
};
var obj = _.extend({},Backbone.Events);
@@ -405,7 +431,8 @@
obj.trigger('event');
});
test("nested trigger with unbind", 1, function () {
QUnit.test("nested trigger with unbind", function(assert) {
assert.expect(1);
var obj = { counter: 0 };
_.extend(obj, Backbone.Events);
var incr1 = function(){ obj.counter += 1; obj.off('event', incr1); obj.trigger('event'); };
@@ -413,23 +440,25 @@
obj.on('event', incr1);
obj.on('event', incr2);
obj.trigger('event');
equal(obj.counter, 3, 'counter should have been incremented three times');
assert.equal(obj.counter, 3, 'counter should have been incremented three times');
});
test("callback list is not altered during trigger", 2, function () {
QUnit.test("callback list is not altered during trigger", function(assert) {
assert.expect(2);
var counter = 0, obj = _.extend({}, Backbone.Events);
var incr = function(){ counter++; };
var incrOn = function(){ obj.on('event all', incr); };
var incrOff = function(){ obj.off('event all', incr); };
obj.on('event all', incrOn).trigger('event');
equal(counter, 0, 'on does not alter callback list');
assert.equal(counter, 0, 'on does not alter callback list');
obj.off().on('event', incrOff).on('event all', incr).trigger('event');
equal(counter, 2, 'off does not alter callback list');
assert.equal(counter, 2, 'off does not alter callback list');
});
test("#1282 - 'all' callback list is retrieved after each event.", 1, function() {
QUnit.test("#1282 - 'all' callback list is retrieved after each event.", function(assert) {
assert.expect(1);
var counter = 0;
var obj = _.extend({}, Backbone.Events);
var incr = function(){ counter++; };
@@ -437,47 +466,53 @@
obj.on('y', incr).on('all', incr);
})
.trigger('x y');
strictEqual(counter, 2);
assert.strictEqual(counter, 2);
});
test("if no callback is provided, `on` is a noop", 0, function() {
QUnit.test("if no callback is provided, `on` is a noop", function(assert) {
assert.expect(0);
_.extend({}, Backbone.Events).on('test').trigger('test');
});
test("if callback is truthy but not a function, `on` should throw an error just like jQuery", 1, function() {
QUnit.test("if callback is truthy but not a function, `on` should throw an error just like jQuery", function(assert) {
assert.expect(1);
var view = _.extend({}, Backbone.Events).on('test', 'noop');
throws(function() {
assert.throws(function() {
view.trigger('test');
});
});
test("remove all events for a specific context", 4, function() {
QUnit.test("remove all events for a specific context", function(assert) {
assert.expect(4);
var obj = _.extend({}, Backbone.Events);
obj.on('x y all', function() { ok(true); });
obj.on('x y all', function() { ok(false); }, obj);
obj.on('x y all', function() { assert.ok(true); });
obj.on('x y all', function() { assert.ok(false); }, obj);
obj.off(null, null, obj);
obj.trigger('x y');
});
test("remove all events for a specific callback", 4, function() {
QUnit.test("remove all events for a specific callback", function(assert) {
assert.expect(4);
var obj = _.extend({}, Backbone.Events);
var success = function() { ok(true); };
var fail = function() { ok(false); };
var success = function() { assert.ok(true); };
var fail = function() { assert.ok(false); };
obj.on('x y all', success);
obj.on('x y all', fail);
obj.off(null, fail);
obj.trigger('x y');
});
test("#1310 - off does not skip consecutive events", 0, function() {
QUnit.test("#1310 - off does not skip consecutive events", function(assert) {
assert.expect(0);
var obj = _.extend({}, Backbone.Events);
obj.on('event', function() { ok(false); }, obj);
obj.on('event', function() { ok(false); }, obj);
obj.on('event', function() { assert.ok(false); }, obj);
obj.on('event', function() { assert.ok(false); }, obj);
obj.off(null, null, obj);
obj.trigger('event');
});
test("once", 2, function() {
QUnit.test("once", function(assert) {
assert.expect(2);
// Same as the previous test, but we use once rather than having to explicitly unbind
var obj = { counterA: 0, counterB: 0 };
_.extend(obj, Backbone.Events);
@@ -486,12 +521,13 @@
obj.once('event', incrA);
obj.once('event', incrB);
obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.');
equal(obj.counterB, 1, 'counterB should have only been incremented once.');
assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.');
assert.equal(obj.counterB, 1, 'counterB should have only been incremented once.');
});
test("once variant one", 3, function() {
var f = function(){ ok(true); };
QUnit.test("once variant one", function(assert) {
assert.expect(3);
var f = function(){ assert.ok(true); };
var a = _.extend({}, Backbone.Events).once('event', f);
var b = _.extend({}, Backbone.Events).on('event', f);
@@ -502,8 +538,9 @@
b.trigger('event');
});
test("once variant two", 3, function() {
var f = function(){ ok(true); };
QUnit.test("once variant two", function(assert) {
assert.expect(3);
var f = function(){ assert.ok(true); };
var obj = _.extend({}, Backbone.Events);
obj
@@ -513,8 +550,9 @@
.trigger('event');
});
test("once with off", 0, function() {
var f = function(){ ok(true); };
QUnit.test("once with off", function(assert) {
assert.expect(0);
var f = function(){ assert.ok(true); };
var obj = _.extend({}, Backbone.Events);
obj.once('event', f);
@@ -522,7 +560,7 @@
obj.trigger('event');
});
test("once with event maps", function() {
QUnit.test("once with event maps", function(assert) {
var obj = { counter: 0 };
_.extend(obj, Backbone.Events);
@@ -537,94 +575,103 @@
}, obj);
obj.trigger('a');
equal(obj.counter, 1);
assert.equal(obj.counter, 1);
obj.trigger('a b');
equal(obj.counter, 2);
assert.equal(obj.counter, 2);
obj.trigger('c');
equal(obj.counter, 3);
assert.equal(obj.counter, 3);
obj.trigger('a b c');
equal(obj.counter, 3);
assert.equal(obj.counter, 3);
});
test("once with off only by context", 0, function() {
QUnit.test("once with off only by context", function(assert) {
assert.expect(0);
var context = {};
var obj = _.extend({}, Backbone.Events);
obj.once('event', function(){ ok(false); }, context);
obj.once('event', function(){ assert.ok(false); }, context);
obj.off(null, null, context);
obj.trigger('event');
});
test("Backbone object inherits Events", function() {
ok(Backbone.on === Backbone.Events.on);
QUnit.test("Backbone object inherits Events", function(assert) {
assert.ok(Backbone.on === Backbone.Events.on);
});
asyncTest("once with asynchronous events", 1, function() {
var func = _.debounce(function() { ok(true); start(); }, 50);
QUnit.test("once with asynchronous events", function(assert) {
var done = assert.async();
assert.expect(1);
var func = _.debounce(function() { assert.ok(true); done(); }, 50);
var obj = _.extend({}, Backbone.Events).once('async', func);
obj.trigger('async');
obj.trigger('async');
});
test("once with multiple events.", 2, function() {
QUnit.test("once with multiple events.", function(assert) {
assert.expect(2);
var obj = _.extend({}, Backbone.Events);
obj.once('x y', function() { ok(true); });
obj.once('x y', function() { assert.ok(true); });
obj.trigger('x y');
});
test("Off during iteration with once.", 2, function() {
QUnit.test("Off during iteration with once.", function(assert) {
assert.expect(2);
var obj = _.extend({}, Backbone.Events);
var f = function(){ this.off('event', f); };
obj.on('event', f);
obj.once('event', function(){});
obj.on('event', function(){ ok(true); });
obj.on('event', function(){ assert.ok(true); });
obj.trigger('event');
obj.trigger('event');
});
test("`once` on `all` should work as expected", 1, function() {
QUnit.test("`once` on `all` should work as expected", function(assert) {
assert.expect(1);
Backbone.once('all', function() {
ok(true);
assert.ok(true);
Backbone.trigger('all');
});
Backbone.trigger('all');
});
test("once without a callback is a noop", 0, function() {
QUnit.test("once without a callback is a noop", function(assert) {
assert.expect(0);
_.extend({}, Backbone.Events).once('event').trigger('event');
});
test("listenToOnce without a callback is a noop", 0, function() {
QUnit.test("listenToOnce without a callback is a noop", function(assert) {
assert.expect(0);
var obj = _.extend({}, Backbone.Events);
obj.listenToOnce(obj, 'event').trigger('event');
});
test("event functions are chainable", function() {
QUnit.test("event functions are chainable", function(assert) {
var obj = _.extend({}, Backbone.Events);
var obj2 = _.extend({}, Backbone.Events);
var fn = function() {};
equal(obj, obj.trigger('noeventssetyet'));
equal(obj, obj.off('noeventssetyet'));
equal(obj, obj.stopListening('noeventssetyet'));
equal(obj, obj.on('a', fn));
equal(obj, obj.once('c', fn));
equal(obj, obj.trigger('a'));
equal(obj, obj.listenTo(obj2, 'a', fn));
equal(obj, obj.listenToOnce(obj2, 'b', fn));
equal(obj, obj.off('a c'));
equal(obj, obj.stopListening(obj2, 'a'));
equal(obj, obj.stopListening());
assert.equal(obj, obj.trigger('noeventssetyet'));
assert.equal(obj, obj.off('noeventssetyet'));
assert.equal(obj, obj.stopListening('noeventssetyet'));
assert.equal(obj, obj.on('a', fn));
assert.equal(obj, obj.once('c', fn));
assert.equal(obj, obj.trigger('a'));
assert.equal(obj, obj.listenTo(obj2, 'a', fn));
assert.equal(obj, obj.listenToOnce(obj2, 'b', fn));
assert.equal(obj, obj.off('a c'));
assert.equal(obj, obj.stopListening(obj2, 'a'));
assert.equal(obj, obj.stopListening());
});
test("#3448 - listenToOnce with space-separated events", 2, function() {
QUnit.test("#3448 - listenToOnce with space-separated events", function(assert) {
assert.expect(2);
var one = _.extend({}, Backbone.Events);
var two = _.extend({}, Backbone.Events);
var count = 1;
one.listenToOnce(two, 'x y', function(n) { ok(n === count++); });
one.listenToOnce(two, 'x y', function(n) { assert.ok(n === count++); });
two.trigger('x', 1);
two.trigger('x', 1);
two.trigger('y', 2);

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +1,13 @@
(function() {
module("Backbone.noConflict");
QUnit.module("Backbone.noConflict");
test('noConflict', 2, function() {
QUnit.test('noConflict', function(assert) {
assert.expect(2);
var noconflictBackbone = Backbone.noConflict();
equal(window.Backbone, undefined, 'Returned window.Backbone');
assert.equal(window.Backbone, undefined, 'Returned window.Backbone');
window.Backbone = noconflictBackbone;
equal(window.Backbone, noconflictBackbone, 'Backbone is still pointing to the original Backbone');
assert.equal(window.Backbone, noconflictBackbone, 'Backbone is still pointing to the original Backbone');
});
})();

View File

@@ -40,7 +40,7 @@
});
module("Backbone.Router", {
QUnit.module("Backbone.Router", {
setup: function() {
location = new Location('http://example.com');
@@ -176,167 +176,189 @@
});
test("initialize", 1, function() {
equal(router.testing, 101);
QUnit.test("initialize", function(assert) {
assert.expect(1);
assert.equal(router.testing, 101);
});
test("routes (simple)", 4, function() {
QUnit.test("routes (simple)", function(assert) {
assert.expect(4);
location.replace('http://example.com#search/news');
Backbone.history.checkUrl();
equal(router.query, 'news');
equal(router.page, void 0);
equal(lastRoute, 'search');
equal(lastArgs[0], 'news');
assert.equal(router.query, 'news');
assert.equal(router.page, void 0);
assert.equal(lastRoute, 'search');
assert.equal(lastArgs[0], 'news');
});
test("routes (simple, but unicode)", 4, function() {
QUnit.test("routes (simple, but unicode)", function(assert) {
assert.expect(4);
location.replace('http://example.com#search/тест');
Backbone.history.checkUrl();
equal(router.query, "тест");
equal(router.page, void 0);
equal(lastRoute, 'search');
equal(lastArgs[0], "тест");
assert.equal(router.query, "тест");
assert.equal(router.page, void 0);
assert.equal(lastRoute, 'search');
assert.equal(lastArgs[0], "тест");
});
test("routes (two part)", 2, function() {
QUnit.test("routes (two part)", function(assert) {
assert.expect(2);
location.replace('http://example.com#search/nyc/p10');
Backbone.history.checkUrl();
equal(router.query, 'nyc');
equal(router.page, '10');
assert.equal(router.query, 'nyc');
assert.equal(router.page, '10');
});
test("routes via navigate", 2, function() {
QUnit.test("routes via navigate", function(assert) {
assert.expect(2);
Backbone.history.navigate('search/manhattan/p20', {trigger: true});
equal(router.query, 'manhattan');
equal(router.page, '20');
assert.equal(router.query, 'manhattan');
assert.equal(router.page, '20');
});
test("routes via navigate with params", 1, function() {
QUnit.test("routes via navigate with params", function(assert) {
assert.expect(1);
Backbone.history.navigate('query/test?a=b', {trigger: true});
equal(router.queryArgs, 'a=b');
assert.equal(router.queryArgs, 'a=b');
});
test("routes via navigate for backwards-compatibility", 2, function() {
QUnit.test("routes via navigate for backwards-compatibility", function(assert) {
assert.expect(2);
Backbone.history.navigate('search/manhattan/p20', true);
equal(router.query, 'manhattan');
equal(router.page, '20');
assert.equal(router.query, 'manhattan');
assert.equal(router.page, '20');
});
test("reports matched route via nagivate", 1, function() {
ok(Backbone.history.navigate('search/manhattan/p20', true));
QUnit.test("reports matched route via nagivate", function(assert) {
assert.expect(1);
assert.ok(Backbone.history.navigate('search/manhattan/p20', true));
});
test("route precedence via navigate", 6, function(){
QUnit.test("route precedence via navigate", function(assert){
assert.expect(6);
// check both 0.9.x and backwards-compatibility options
_.each([ { trigger: true }, true ], function( options ){
Backbone.history.navigate('contacts', options);
equal(router.contact, 'index');
assert.equal(router.contact, 'index');
Backbone.history.navigate('contacts/new', options);
equal(router.contact, 'new');
assert.equal(router.contact, 'new');
Backbone.history.navigate('contacts/foo', options);
equal(router.contact, 'load');
assert.equal(router.contact, 'load');
});
});
test("loadUrl is not called for identical routes.", 0, function() {
Backbone.history.loadUrl = function(){ ok(false); };
QUnit.test("loadUrl is not called for identical routes.", function(assert) {
assert.expect(0);
Backbone.history.loadUrl = function(){ assert.ok(false); };
location.replace('http://example.com#route');
Backbone.history.navigate('route');
Backbone.history.navigate('/route');
Backbone.history.navigate('/route');
});
test("use implicit callback if none provided", 1, function() {
QUnit.test("use implicit callback if none provided", function(assert) {
assert.expect(1);
router.count = 0;
router.navigate('implicit', {trigger: true});
equal(router.count, 1);
assert.equal(router.count, 1);
});
test("routes via navigate with {replace: true}", 1, function() {
QUnit.test("routes via navigate with {replace: true}", function(assert) {
assert.expect(1);
location.replace('http://example.com#start_here');
Backbone.history.checkUrl();
location.replace = function(href) {
strictEqual(href, new Location('http://example.com#end_here').href);
assert.strictEqual(href, new Location('http://example.com#end_here').href);
};
Backbone.history.navigate('end_here', {replace: true});
});
test("routes (splats)", 1, function() {
QUnit.test("routes (splats)", function(assert) {
assert.expect(1);
location.replace('http://example.com#splat/long-list/of/splatted_99args/end');
Backbone.history.checkUrl();
equal(router.args, 'long-list/of/splatted_99args');
assert.equal(router.args, 'long-list/of/splatted_99args');
});
test("routes (github)", 3, function() {
QUnit.test("routes (github)", function(assert) {
assert.expect(3);
location.replace('http://example.com#backbone/compare/1.0...braddunbar:with/slash');
Backbone.history.checkUrl();
equal(router.repo, 'backbone');
equal(router.from, '1.0');
equal(router.to, 'braddunbar:with/slash');
assert.equal(router.repo, 'backbone');
assert.equal(router.from, '1.0');
assert.equal(router.to, 'braddunbar:with/slash');
});
test("routes (optional)", 2, function() {
QUnit.test("routes (optional)", function(assert) {
assert.expect(2);
location.replace('http://example.com#optional');
Backbone.history.checkUrl();
ok(!router.arg);
assert.ok(!router.arg);
location.replace('http://example.com#optional/thing');
Backbone.history.checkUrl();
equal(router.arg, 'thing');
assert.equal(router.arg, 'thing');
});
test("routes (complex)", 3, function() {
QUnit.test("routes (complex)", function(assert) {
assert.expect(3);
location.replace('http://example.com#one/two/three/complex-part/four/five/six/seven');
Backbone.history.checkUrl();
equal(router.first, 'one/two/three');
equal(router.part, 'part');
equal(router.rest, 'four/five/six/seven');
assert.equal(router.first, 'one/two/three');
assert.equal(router.part, 'part');
assert.equal(router.rest, 'four/five/six/seven');
});
test("routes (query)", 5, function() {
QUnit.test("routes (query)", function(assert) {
assert.expect(5);
location.replace('http://example.com#query/mandel?a=b&c=d');
Backbone.history.checkUrl();
equal(router.entity, 'mandel');
equal(router.queryArgs, 'a=b&c=d');
equal(lastRoute, 'query');
equal(lastArgs[0], 'mandel');
equal(lastArgs[1], 'a=b&c=d');
assert.equal(router.entity, 'mandel');
assert.equal(router.queryArgs, 'a=b&c=d');
assert.equal(lastRoute, 'query');
assert.equal(lastArgs[0], 'mandel');
assert.equal(lastArgs[1], 'a=b&c=d');
});
test("routes (anything)", 1, function() {
QUnit.test("routes (anything)", function(assert) {
assert.expect(1);
location.replace('http://example.com#doesnt-match-a-route');
Backbone.history.checkUrl();
equal(router.anything, 'doesnt-match-a-route');
assert.equal(router.anything, 'doesnt-match-a-route');
});
test("routes (function)", 3, function() {
QUnit.test("routes (function)", function(assert) {
assert.expect(3);
router.on('route', function(name) {
ok(name === '');
assert.ok(name === '');
});
equal(ExternalObject.value, 'unset');
assert.equal(ExternalObject.value, 'unset');
location.replace('http://example.com#function/set');
Backbone.history.checkUrl();
equal(ExternalObject.value, 'set');
assert.equal(ExternalObject.value, 'set');
});
test("Decode named parameters, not splats.", 2, function() {
QUnit.test("Decode named parameters, not splats.", function(assert) {
assert.expect(2);
location.replace('http://example.com#decode/a%2Fb/c%2Fd/e');
Backbone.history.checkUrl();
strictEqual(router.named, 'a/b');
strictEqual(router.path, 'c/d/e');
assert.strictEqual(router.named, 'a/b');
assert.strictEqual(router.path, 'c/d/e');
});
test("fires event when router doesn't have callback on it", 1, function() {
router.on("route:noCallback", function(){ ok(true); });
QUnit.test("fires event when router doesn't have callback on it", function(assert) {
assert.expect(1);
router.on("route:noCallback", function(){ assert.ok(true); });
location.replace('http://example.com#noCallback');
Backbone.history.checkUrl();
});
test("No events are triggered if #execute returns false.", 1, function() {
QUnit.test("No events are triggered if #execute returns false.", function(assert) {
assert.expect(1);
var Router = Backbone.Router.extend({
routes: {
foo: function() {
ok(true);
assert.ok(true);
}
},
@@ -350,92 +372,100 @@
var router = new Router;
router.on('route route:foo', function() {
ok(false);
assert.ok(false);
});
Backbone.history.on('route', function() {
ok(false);
assert.ok(false);
});
location.replace('http://example.com#foo');
Backbone.history.checkUrl();
});
test("#933, #908 - leading slash", 2, function() {
QUnit.test("#933, #908 - leading slash", function(assert) {
assert.expect(2);
location.replace('http://example.com/root/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({root: '/root', hashChange: false, silent: true});
strictEqual(Backbone.history.getFragment(), 'foo');
assert.strictEqual(Backbone.history.getFragment(), 'foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({root: '/root/', hashChange: false, silent: true});
strictEqual(Backbone.history.getFragment(), 'foo');
assert.strictEqual(Backbone.history.getFragment(), 'foo');
});
test("#967 - Route callback gets passed encoded values.", 3, function() {
QUnit.test("#967 - Route callback gets passed encoded values.", function(assert) {
assert.expect(3);
var route = 'has%2Fslash/complex-has%23hash/has%20space';
Backbone.history.navigate(route, {trigger: true});
strictEqual(router.first, 'has/slash');
strictEqual(router.part, 'has#hash');
strictEqual(router.rest, 'has space');
assert.strictEqual(router.first, 'has/slash');
assert.strictEqual(router.part, 'has#hash');
assert.strictEqual(router.rest, 'has space');
});
test("correctly handles URLs with % (#868)", 3, function() {
QUnit.test("correctly handles URLs with % (#868)", function(assert) {
assert.expect(3);
location.replace('http://example.com#search/fat%3A1.5%25');
Backbone.history.checkUrl();
location.replace('http://example.com#search/fat');
Backbone.history.checkUrl();
equal(router.query, 'fat');
equal(router.page, void 0);
equal(lastRoute, 'search');
assert.equal(router.query, 'fat');
assert.equal(router.page, void 0);
assert.equal(lastRoute, 'search');
});
test("#2666 - Hashes with UTF8 in them.", 2, function() {
QUnit.test("#2666 - Hashes with UTF8 in them.", function(assert) {
assert.expect(2);
Backbone.history.navigate('charñ', {trigger: true});
equal(router.charType, 'UTF');
assert.equal(router.charType, 'UTF');
Backbone.history.navigate('char%C3%B1', {trigger: true});
equal(router.charType, 'UTF');
assert.equal(router.charType, 'UTF');
});
test("#1185 - Use pathname when hashChange is not wanted.", 1, function() {
QUnit.test("#1185 - Use pathname when hashChange is not wanted.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/path/name#hash');
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false});
var fragment = Backbone.history.getFragment();
strictEqual(fragment, location.pathname.replace(/^\//, ''));
assert.strictEqual(fragment, location.pathname.replace(/^\//, ''));
});
test("#1206 - Strip leading slash before location.assign.", 1, function() {
QUnit.test("#1206 - Strip leading slash before location.assign.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root/');
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false, root: '/root/'});
location.assign = function(pathname) {
strictEqual(pathname, '/root/fragment');
assert.strictEqual(pathname, '/root/fragment');
};
Backbone.history.navigate('/fragment');
});
test("#1387 - Root fragment without trailing slash.", 1, function() {
QUnit.test("#1387 - Root fragment without trailing slash.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false, root: '/root/', silent: true});
strictEqual(Backbone.history.getFragment(), '');
assert.strictEqual(Backbone.history.getFragment(), '');
});
test("#1366 - History does not prepend root to fragment.", 2, function() {
QUnit.test("#1366 - History does not prepend root to fragment.", function(assert) {
assert.expect(2);
Backbone.history.stop();
location.replace('http://example.com/root/');
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
strictEqual(url, '/root/x');
assert.strictEqual(url, '/root/x');
}
}
});
@@ -445,17 +475,18 @@
hashChange: false
});
Backbone.history.navigate('x');
strictEqual(Backbone.history.fragment, 'x');
assert.strictEqual(Backbone.history.fragment, 'x');
});
test("Normalize root.", 1, function() {
QUnit.test("Normalize root.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
strictEqual(url, '/root/fragment');
assert.strictEqual(url, '/root/fragment');
}
}
});
@@ -467,7 +498,8 @@
Backbone.history.navigate('fragment');
});
test("Normalize root.", 1, function() {
QUnit.test("Normalize root.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root#fragment');
Backbone.history = _.extend(new Backbone.History, {
@@ -475,7 +507,7 @@
history: {
pushState: function(state, title, url) {},
replaceState: function(state, title, url) {
strictEqual(url, '/root/fragment');
assert.strictEqual(url, '/root/fragment');
}
}
});
@@ -485,18 +517,20 @@
});
});
test("Normalize root.", 1, function() {
QUnit.test("Normalize root.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.loadUrl = function() { ok(true); };
Backbone.history.loadUrl = function() { assert.ok(true); };
Backbone.history.start({
pushState: true,
root: '/root'
});
});
test("Normalize root - leading slash.", 1, function() {
QUnit.test("Normalize root - leading slash.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = _.extend(new Backbone.History, {
@@ -507,10 +541,11 @@
}
});
Backbone.history.start({root: 'root'});
strictEqual(Backbone.history.root, '/root/');
assert.strictEqual(Backbone.history.root, '/root/');
});
test("Transition from hashChange to pushState.", 1, function() {
QUnit.test("Transition from hashChange to pushState.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root#x/y');
Backbone.history = _.extend(new Backbone.History, {
@@ -518,7 +553,7 @@
history: {
pushState: function(){},
replaceState: function(state, title, url){
strictEqual(url, '/root/x/y');
assert.strictEqual(url, '/root/x/y');
}
}
});
@@ -528,7 +563,8 @@
});
});
test("#1619: Router: Normalize empty root", 1, function() {
QUnit.test("#1619: Router: Normalize empty root", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/');
Backbone.history = _.extend(new Backbone.History, {
@@ -539,17 +575,18 @@
}
});
Backbone.history.start({root: ''});
strictEqual(Backbone.history.root, '/');
assert.strictEqual(Backbone.history.root, '/');
});
test("#1619: Router: nagivate with empty root", 1, function() {
QUnit.test("#1619: Router: nagivate with empty root", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/');
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
strictEqual(url, '/fragment');
assert.strictEqual(url, '/fragment');
}
}
});
@@ -561,11 +598,12 @@
Backbone.history.navigate('fragment');
});
test("Transition from pushState to hashChange.", 1, function() {
QUnit.test("Transition from pushState to hashChange.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root/x/y?a=b');
location.replace = function(url) {
strictEqual(url, '/root#x/y?a=b');
assert.strictEqual(url, '/root#x/y?a=b');
};
Backbone.history = _.extend(new Backbone.History, {
location: location,
@@ -580,7 +618,8 @@
});
});
test("#1695 - hashChange to pushState with search.", 1, function() {
QUnit.test("#1695 - hashChange to pushState with search.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root#x/y?a=b');
Backbone.history = _.extend(new Backbone.History, {
@@ -588,7 +627,7 @@
history: {
pushState: function(){},
replaceState: function(state, title, url){
strictEqual(url, '/root/x/y?a=b');
assert.strictEqual(url, '/root/x/y?a=b');
}
}
});
@@ -598,46 +637,51 @@
});
});
test("#1746 - Router allows empty route.", 1, function() {
QUnit.test("#1746 - Router allows empty route.", function(assert) {
assert.expect(1);
var Router = Backbone.Router.extend({
routes: {'': 'empty'},
empty: function(){},
route: function(route){
strictEqual(route, '');
assert.strictEqual(route, '');
}
});
new Router;
});
test("#1794 - Trailing space in fragments.", 1, function() {
QUnit.test("#1794 - Trailing space in fragments.", function(assert) {
assert.expect(1);
var history = new Backbone.History;
strictEqual(history.getFragment('fragment '), 'fragment');
assert.strictEqual(history.getFragment('fragment '), 'fragment');
});
test("#1820 - Leading slash and trailing space.", 1, function() {
QUnit.test("#1820 - Leading slash and trailing space.", 1, function(assert) {
var history = new Backbone.History;
strictEqual(history.getFragment('/fragment '), 'fragment');
assert.strictEqual(history.getFragment('/fragment '), 'fragment');
});
test("#1980 - Optional parameters.", 2, function() {
QUnit.test("#1980 - Optional parameters.", function(assert) {
assert.expect(2);
location.replace('http://example.com#named/optional/y');
Backbone.history.checkUrl();
strictEqual(router.z, undefined);
assert.strictEqual(router.z, undefined);
location.replace('http://example.com#named/optional/y123');
Backbone.history.checkUrl();
strictEqual(router.z, '123');
assert.strictEqual(router.z, '123');
});
test("#2062 - Trigger 'route' event on router instance.", 2, function() {
QUnit.test("#2062 - Trigger 'route' event on router instance.", function(assert) {
assert.expect(2);
router.on('route', function(name, args) {
strictEqual(name, 'routeEvent');
deepEqual(args, ['x', null]);
assert.strictEqual(name, 'routeEvent');
assert.deepEqual(args, ['x', null]);
});
location.replace('http://example.com#route-event/x');
Backbone.history.checkUrl();
});
test("#2255 - Extend routes by making routes a function.", 1, function() {
QUnit.test("#2255 - Extend routes by making routes a function.", function(assert) {
assert.expect(1);
var RouterBase = Backbone.Router.extend({
routes: function() {
return {
@@ -657,17 +701,18 @@
});
var router = new RouterExtended();
deepEqual({home: "root", index: "index.html", show: "show", search: "search"}, router.routes);
assert.deepEqual({home: "root", index: "index.html", show: "show", search: "search"}, router.routes);
});
test("#2538 - hashChange to pushState only if both requested.", 0, function() {
QUnit.test("#2538 - hashChange to pushState only if both requested.", function(assert) {
assert.expect(0);
Backbone.history.stop();
location.replace('http://example.com/root?a=b#x/y');
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(){},
replaceState: function(){ ok(false); }
replaceState: function(){ assert.ok(false); }
}
});
Backbone.history.start({
@@ -677,7 +722,8 @@
});
});
test('No hash fallback.', 0, function() {
QUnit.test('No hash fallback.', function(assert) {
assert.expect(0);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
@@ -689,7 +735,7 @@
var Router = Backbone.Router.extend({
routes: {
hash: function() { ok(false); }
hash: function() { assert.ok(false); }
}
});
var router = new Router;
@@ -703,13 +749,14 @@
Backbone.history.checkUrl();
});
test('#2656 - No trailing slash on root.', 1, function() {
QUnit.test('#2656 - No trailing slash on root.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url){
strictEqual(url, '/root');
assert.strictEqual(url, '/root');
}
}
});
@@ -718,13 +765,14 @@
Backbone.history.navigate('');
});
test('#2656 - No trailing slash on root.', 1, function() {
QUnit.test('#2656 - No trailing slash on root.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
strictEqual(url, '/');
assert.strictEqual(url, '/');
}
}
});
@@ -733,13 +781,14 @@
Backbone.history.navigate('');
});
test('#2656 - No trailing slash on root.', 1, function() {
QUnit.test('#2656 - No trailing slash on root.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url){
strictEqual(url, '/root?x=1');
assert.strictEqual(url, '/root?x=1');
}
}
});
@@ -748,20 +797,21 @@
Backbone.history.navigate('?x=1');
});
test('#2765 - Fragment matching sans query/hash.', 2, function() {
QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) {
assert.expect(2);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
strictEqual(url, '/path?query#hash');
assert.strictEqual(url, '/path?query#hash');
}
}
});
var Router = Backbone.Router.extend({
routes: {
path: function() { ok(true); }
path: function() { assert.ok(true); }
}
});
var router = new Router;
@@ -771,11 +821,12 @@
Backbone.history.navigate('path?query#hash', true);
});
test('Do not decode the search params.', function() {
QUnit.test('Do not decode the search params.', function(assert) {
assert.expect(1);
var Router = Backbone.Router.extend({
routes: {
path: function(params){
strictEqual(params, 'x=y%3Fz');
assert.strictEqual(params, 'x=y%3Fz');
}
}
});
@@ -783,14 +834,15 @@
Backbone.history.navigate('path?x=y%3Fz', true);
});
test('Navigate to a hash url.', function() {
QUnit.test('Navigate to a hash url.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({pushState: true});
var Router = Backbone.Router.extend({
routes: {
path: function(params) {
strictEqual(params, 'x=y');
assert.strictEqual(params, 'x=y');
}
}
});
@@ -799,14 +851,15 @@
Backbone.history.checkUrl();
});
test('#navigate to a hash url.', function() {
QUnit.test('#navigate to a hash url.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({pushState: true});
var Router = Backbone.Router.extend({
routes: {
path: function(params) {
strictEqual(params, 'x=y');
assert.strictEqual(params, 'x=y');
}
}
});
@@ -814,14 +867,15 @@
Backbone.history.navigate('path?x=y#hash', true);
});
test('unicode pathname', 1, function() {
QUnit.test('unicode pathname', function(assert) {
assert.expect(1);
location.replace('http://example.com/myyjä');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {
myyjä: function() {
ok(true);
assert.ok(true);
}
}
});
@@ -829,7 +883,8 @@
Backbone.history.start({pushState: true});
});
test('unicode pathname with % in a parameter', 1, function() {
QUnit.test('unicode pathname with % in a parameter', function(assert) {
assert.expect(1);
location.replace('http://example.com/myyjä/foo%20%25%3F%2f%40%25%20bar');
location.pathname = '/myyj%C3%A4/foo%20%25%3F%2f%40%25%20bar';
Backbone.history.stop();
@@ -837,7 +892,7 @@
var Router = Backbone.Router.extend({
routes: {
'myyjä/:query': function(query) {
strictEqual(query, 'foo %?/@% bar');
assert.strictEqual(query, 'foo %?/@% bar');
}
}
});
@@ -845,14 +900,15 @@
Backbone.history.start({pushState: true});
});
test('newline in route', 1, function() {
QUnit.test('newline in route', function(assert) {
assert.expect(1);
location.replace('http://example.com/stuff%0Anonsense?param=foo%0Abar');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {
'stuff\nnonsense': function() {
ok(true);
assert.ok(true);
}
}
});
@@ -860,7 +916,8 @@
Backbone.history.start({pushState: true});
});
test('Router#execute receives callback, args, name.', 3, function() {
QUnit.test('Router#execute receives callback, args, name.', function(assert) {
assert.expect(3);
location.replace('http://example.com#foo/123/bar?x=y');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
@@ -868,20 +925,21 @@
routes: {'foo/:id/bar': 'foo'},
foo: function(){},
execute: function(callback, args, name) {
strictEqual(callback, this.foo);
deepEqual(args, ['123', 'x=y']);
strictEqual(name, 'foo');
assert.strictEqual(callback, this.foo);
assert.deepEqual(args, ['123', 'x=y']);
assert.strictEqual(name, 'foo');
}
});
var router = new Router;
Backbone.history.start();
});
test("pushState to hashChange with only search params.", 1, function() {
QUnit.test("pushState to hashChange with only search params.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com?a=b');
location.replace = function(url) {
strictEqual(url, '/#?a=b');
assert.strictEqual(url, '/#?a=b');
};
Backbone.history = _.extend(new Backbone.History, {
location: location,
@@ -890,37 +948,40 @@
Backbone.history.start({pushState: true});
});
test("#3123 - History#navigate decodes before comparison.", 1, function() {
QUnit.test("#3123 - History#navigate decodes before comparison.", function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/shop/search?keyword=short%20dress');
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(){ ok(false); },
replaceState: function(){ ok(false); }
pushState: function(){ assert.ok(false); },
replaceState: function(){ assert.ok(false); }
}
});
Backbone.history.start({pushState: true});
Backbone.history.navigate('shop/search?keyword=short%20dress', true);
strictEqual(Backbone.history.fragment, 'shop/search?keyword=short dress');
assert.strictEqual(Backbone.history.fragment, 'shop/search?keyword=short dress');
});
test('#3175 - Urls in the params', 1, function() {
QUnit.test('#3175 - Urls in the params', function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com#login?a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db');
Backbone.history = _.extend(new Backbone.History, {location: location});
var router = new Backbone.Router;
router.route('login', function(params) {
strictEqual(params, 'a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db');
assert.strictEqual(params, 'a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db');
});
Backbone.history.start();
});
test('#3358 - pushState to hashChange transition with search params', 1, function() {
QUnit.test('#3358 - pushState to hashChange transition with search params', function(assert) {
assert.expect(1);
Backbone.history.stop();
location.replace('http://example.com/root?foo=bar');
location.replace = function(url) {
strictEqual(url, '/root#?foo=bar');
assert.strictEqual(url, '/root#?foo=bar');
};
Backbone.history = _.extend(new Backbone.History, {
location: location,
@@ -932,14 +993,15 @@
Backbone.history.start({root: '/root', pushState: true});
});
test("Paths that don't match the root should not match no root", 0, function() {
QUnit.test("Paths that don't match the root should not match no root", function(assert) {
assert.expect(0);
location.replace('http://example.com/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {
foo: function(){
ok(false, 'should not match unless root matches');
assert.ok(false, 'should not match unless root matches');
}
}
});
@@ -947,14 +1009,15 @@
Backbone.history.start({root: 'root', pushState: true});
});
test("Paths that don't match the root should not match roots of the same length", 0, function() {
QUnit.test("Paths that don't match the root should not match roots of the same length", function(assert) {
assert.expect(0);
location.replace('http://example.com/xxxx/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {
foo: function(){
ok(false, 'should not match unless root matches');
assert.ok(false, 'should not match unless root matches');
}
}
});
@@ -962,34 +1025,37 @@
Backbone.history.start({root: 'root', pushState: true});
});
test("roots with regex characters", 1, function() {
QUnit.test("roots with regex characters", function(assert) {
assert.expect(1);
location.replace('http://example.com/x+y.z/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {foo: function(){ ok(true); }}
routes: {foo: function(){ assert.ok(true); }}
});
var router = new Router;
Backbone.history.start({root: 'x+y.z', pushState: true});
});
test("roots with unicode characters", 1, function() {
QUnit.test("roots with unicode characters", function(assert) {
assert.expect(1);
location.replace('http://example.com/®ooτ/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {foo: function(){ ok(true); }}
routes: {foo: function(){ assert.ok(true); }}
});
var router = new Router;
Backbone.history.start({root: '®ooτ', pushState: true});
});
test("roots without slash", 1, function() {
QUnit.test("roots without slash", function(assert) {
assert.expect(1);
location.replace('http://example.com/®ooτ');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {'': function(){ ok(true); }}
routes: {'': function(){ assert.ok(true); }}
});
var router = new Router;
Backbone.history.start({root: '®ooτ', pushState: true});

View File

@@ -8,6 +8,8 @@
var pushState = history.pushState;
var replaceState = history.replaceState;
QUnit.config.noglobals = true;
QUnit.testStart(function() {
var env = QUnit.config.current.testEnvironment;

View File

@@ -11,208 +11,226 @@
length : 123
};
module("Backbone.sync", {
QUnit.module("Backbone.sync", {
setup : function() {
beforeEach : function(assert) {
library = new Library;
library.create(attrs, {wait: false});
},
teardown: function() {
afterEach: function(assert) {
Backbone.emulateHTTP = false;
}
});
test("read", 4, function() {
QUnit.test("read", function(assert) {
assert.expect(4);
library.fetch();
equal(this.ajaxSettings.url, '/library');
equal(this.ajaxSettings.type, 'GET');
equal(this.ajaxSettings.dataType, 'json');
ok(_.isEmpty(this.ajaxSettings.data));
assert.equal(this.ajaxSettings.url, '/library');
assert.equal(this.ajaxSettings.type, 'GET');
assert.equal(this.ajaxSettings.dataType, 'json');
assert.ok(_.isEmpty(this.ajaxSettings.data));
});
test("passing data", 3, function() {
QUnit.test("passing data", function(assert) {
assert.expect(3);
library.fetch({data: {a: 'a', one: 1}});
equal(this.ajaxSettings.url, '/library');
equal(this.ajaxSettings.data.a, 'a');
equal(this.ajaxSettings.data.one, 1);
assert.equal(this.ajaxSettings.url, '/library');
assert.equal(this.ajaxSettings.data.a, 'a');
assert.equal(this.ajaxSettings.data.one, 1);
});
test("create", 6, function() {
equal(this.ajaxSettings.url, '/library');
equal(this.ajaxSettings.type, 'POST');
equal(this.ajaxSettings.dataType, 'json');
QUnit.test("create", function(assert) {
assert.expect(6);
assert.equal(this.ajaxSettings.url, '/library');
assert.equal(this.ajaxSettings.type, 'POST');
assert.equal(this.ajaxSettings.dataType, 'json');
var data = JSON.parse(this.ajaxSettings.data);
equal(data.title, 'The Tempest');
equal(data.author, 'Bill Shakespeare');
equal(data.length, 123);
assert.equal(data.title, 'The Tempest');
assert.equal(data.author, 'Bill Shakespeare');
assert.equal(data.length, 123);
});
test("update", 7, function() {
QUnit.test("update", function(assert) {
assert.expect(7);
library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
equal(this.ajaxSettings.url, '/library/1-the-tempest');
equal(this.ajaxSettings.type, 'PUT');
equal(this.ajaxSettings.dataType, 'json');
assert.equal(this.ajaxSettings.url, '/library/1-the-tempest');
assert.equal(this.ajaxSettings.type, 'PUT');
assert.equal(this.ajaxSettings.dataType, 'json');
var data = JSON.parse(this.ajaxSettings.data);
equal(data.id, '1-the-tempest');
equal(data.title, 'The Tempest');
equal(data.author, 'William Shakespeare');
equal(data.length, 123);
assert.equal(data.id, '1-the-tempest');
assert.equal(data.title, 'The Tempest');
assert.equal(data.author, 'William Shakespeare');
assert.equal(data.length, 123);
});
test("update with emulateHTTP and emulateJSON", 7, function() {
QUnit.test("update with emulateHTTP and emulateJSON", function(assert) {
assert.expect(7);
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
emulateHTTP: true,
emulateJSON: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'POST');
equal(this.ajaxSettings.dataType, 'json');
equal(this.ajaxSettings.data._method, 'PUT');
assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
assert.equal(this.ajaxSettings.type, 'POST');
assert.equal(this.ajaxSettings.dataType, 'json');
assert.equal(this.ajaxSettings.data._method, 'PUT');
var data = JSON.parse(this.ajaxSettings.data.model);
equal(data.id, '2-the-tempest');
equal(data.author, 'Tim Shakespeare');
equal(data.length, 123);
assert.equal(data.id, '2-the-tempest');
assert.equal(data.author, 'Tim Shakespeare');
assert.equal(data.length, 123);
});
test("update with just emulateHTTP", 6, function() {
QUnit.test("update with just emulateHTTP", function(assert) {
assert.expect(6);
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
emulateHTTP: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'POST');
equal(this.ajaxSettings.contentType, 'application/json');
assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
assert.equal(this.ajaxSettings.type, 'POST');
assert.equal(this.ajaxSettings.contentType, 'application/json');
var data = JSON.parse(this.ajaxSettings.data);
equal(data.id, '2-the-tempest');
equal(data.author, 'Tim Shakespeare');
equal(data.length, 123);
assert.equal(data.id, '2-the-tempest');
assert.equal(data.author, 'Tim Shakespeare');
assert.equal(data.length, 123);
});
test("update with just emulateJSON", 6, function() {
QUnit.test("update with just emulateJSON", function(assert) {
assert.expect(6);
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
emulateJSON: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'PUT');
equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded');
assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
assert.equal(this.ajaxSettings.type, 'PUT');
assert.equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded');
var data = JSON.parse(this.ajaxSettings.data.model);
equal(data.id, '2-the-tempest');
equal(data.author, 'Tim Shakespeare');
equal(data.length, 123);
assert.equal(data.id, '2-the-tempest');
assert.equal(data.author, 'Tim Shakespeare');
assert.equal(data.length, 123);
});
test("read model", 3, function() {
QUnit.test("read model", function(assert) {
assert.expect(3);
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
library.first().fetch();
equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'GET');
ok(_.isEmpty(this.ajaxSettings.data));
assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
assert.equal(this.ajaxSettings.type, 'GET');
assert.ok(_.isEmpty(this.ajaxSettings.data));
});
test("destroy", 3, function() {
QUnit.test("destroy", function(assert) {
assert.expect(3);
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
library.first().destroy({wait: true});
equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'DELETE');
equal(this.ajaxSettings.data, null);
assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
assert.equal(this.ajaxSettings.type, 'DELETE');
assert.equal(this.ajaxSettings.data, null);
});
test("destroy with emulateHTTP", 3, function() {
QUnit.test("destroy with emulateHTTP", function(assert) {
assert.expect(3);
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
library.first().destroy({
emulateHTTP: true,
emulateJSON: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'POST');
equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}');
assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
assert.equal(this.ajaxSettings.type, 'POST');
assert.equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}');
});
test("urlError", 2, function() {
QUnit.test("urlError", function(assert) {
assert.expect(2);
var model = new Backbone.Model();
throws(function() {
assert.throws(function() {
model.fetch();
});
model.fetch({url: '/one/two'});
equal(this.ajaxSettings.url, '/one/two');
assert.equal(this.ajaxSettings.url, '/one/two');
});
test("#1052 - `options` is optional.", 0, function() {
QUnit.test("#1052 - `options` is optional.", function(assert) {
assert.expect(0);
var model = new Backbone.Model();
model.url = '/test';
Backbone.sync('create', model);
});
test("Backbone.ajax", 1, function() {
QUnit.test("Backbone.ajax", function(assert) {
assert.expect(1);
Backbone.ajax = function(settings){
strictEqual(settings.url, '/test');
assert.strictEqual(settings.url, '/test');
};
var model = new Backbone.Model();
model.url = '/test';
Backbone.sync('create', model);
});
test("Call provided error callback on error.", 1, function() {
QUnit.test("Call provided error callback on error.", function(assert) {
assert.expect(1);
var model = new Backbone.Model;
model.url = '/test';
Backbone.sync('read', model, {
error: function() { ok(true); }
error: function() { assert.ok(true); }
});
this.ajaxSettings.error();
});
test('Use Backbone.emulateHTTP as default.', 2, function() {
QUnit.test('Use Backbone.emulateHTTP as default.', function(assert) {
assert.expect(2);
var model = new Backbone.Model;
model.url = '/test';
Backbone.emulateHTTP = true;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateHTTP, true);
assert.strictEqual(this.ajaxSettings.emulateHTTP, true);
Backbone.emulateHTTP = false;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateHTTP, false);
assert.strictEqual(this.ajaxSettings.emulateHTTP, false);
});
test('Use Backbone.emulateJSON as default.', 2, function() {
QUnit.test('Use Backbone.emulateJSON as default.', function(assert) {
assert.expect(2);
var model = new Backbone.Model;
model.url = '/test';
Backbone.emulateJSON = true;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateJSON, true);
assert.strictEqual(this.ajaxSettings.emulateJSON, true);
Backbone.emulateJSON = false;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateJSON, false);
assert.strictEqual(this.ajaxSettings.emulateJSON, false);
});
test("#1756 - Call user provided beforeSend function.", 4, function() {
QUnit.test("#1756 - Call user provided beforeSend function.", function(assert) {
assert.expect(4);
Backbone.emulateHTTP = true;
var model = new Backbone.Model;
model.url = '/test';
var xhr = {
setRequestHeader: function(header, value) {
strictEqual(header, 'X-HTTP-Method-Override');
strictEqual(value, 'DELETE');
assert.strictEqual(header, 'X-HTTP-Method-Override');
assert.strictEqual(value, 'DELETE');
}
};
model.sync('delete', model, {
beforeSend: function(_xhr) {
ok(_xhr === xhr);
assert.ok(_xhr === xhr);
return false;
}
});
strictEqual(this.ajaxSettings.beforeSend(xhr), false);
assert.strictEqual(this.ajaxSettings.beforeSend(xhr), false);
});
test('#2928 - Pass along `textStatus` and `errorThrown`.', 2, function() {
QUnit.test('#2928 - Pass along `textStatus` and `errorThrown`.', function(assert) {
assert.expect(2);
var model = new Backbone.Model;
model.url = '/test';
model.on('error', function(model, xhr, options) {
strictEqual(options.textStatus, 'textStatus');
strictEqual(options.errorThrown, 'errorThrown');
assert.strictEqual(options.textStatus, 'textStatus');
assert.strictEqual(options.errorThrown, 'errorThrown');
});
model.fetch();
this.ajaxSettings.error({}, 'textStatus', 'errorThrown');

View File

@@ -2,9 +2,9 @@
var view;
module("Backbone.View", {
QUnit.module("Backbone.View", {
setup: function() {
beforeEach: function(assert) {
$('#qunit-fixture').append(
'<div id="testElement"><h1>Test</h1></div>'
);
@@ -18,46 +18,52 @@
});
test("constructor", 3, function() {
equal(view.el.id, 'test-view');
equal(view.el.className, 'test-view');
equal(view.el.other, void 0);
QUnit.test("constructor", function(assert) {
assert.expect(3);
assert.equal(view.el.id, 'test-view');
assert.equal(view.el.className, 'test-view');
assert.equal(view.el.other, void 0);
});
test("$", 2, function() {
QUnit.test("$", function(assert) {
assert.expect(2);
var view = new Backbone.View;
view.setElement('<p><a><b>test</b></a></p>');
var result = view.$('a b');
strictEqual(result[0].innerHTML, 'test');
ok(result.length === +result.length);
assert.strictEqual(result[0].innerHTML, 'test');
assert.ok(result.length === +result.length);
});
test("$el", 3, function() {
QUnit.test("$el", function(assert) {
assert.expect(3);
var view = new Backbone.View;
view.setElement('<p><a><b>test</b></a></p>');
strictEqual(view.el.nodeType, 1);
assert.strictEqual(view.el.nodeType, 1);
ok(view.$el instanceof Backbone.$);
strictEqual(view.$el[0], view.el);
assert.ok(view.$el instanceof Backbone.$);
assert.strictEqual(view.$el[0], view.el);
});
test("initialize", 1, function() {
QUnit.test("initialize", function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
initialize: function() {
this.one = 1;
}
});
strictEqual(new View().one, 1);
assert.strictEqual(new View().one, 1);
});
test("render", 1, function() {
QUnit.test("render", function(assert) {
assert.expect(1);
var view = new Backbone.View;
equal(view.render(), view, '#render returns the view instance');
assert.equal(view.render(), view, '#render returns the view instance');
});
test("delegateEvents", 6, function() {
QUnit.test("delegateEvents", function(assert) {
assert.expect(6);
var counter1 = 0, counter2 = 0;
var view = new Backbone.View({el: '#testElement'});
@@ -68,33 +74,35 @@
view.delegateEvents(events);
view.$('h1').trigger('click');
equal(counter1, 1);
equal(counter2, 1);
assert.equal(counter1, 1);
assert.equal(counter2, 1);
view.$('h1').trigger('click');
equal(counter1, 2);
equal(counter2, 2);
assert.equal(counter1, 2);
assert.equal(counter2, 2);
view.delegateEvents(events);
view.$('h1').trigger('click');
equal(counter1, 3);
equal(counter2, 3);
assert.equal(counter1, 3);
assert.equal(counter2, 3);
});
test("delegate", 3, function() {
QUnit.test("delegate", function(assert) {
assert.expect(3);
var view = new Backbone.View({el: '#testElement'});
view.delegate('click', 'h1', function() {
ok(true);
assert.ok(true);
});
view.delegate('click', function() {
ok(true);
assert.ok(true);
});
view.$('h1').trigger('click');
equal(view.delegate(), view, '#delegate returns the view instance');
assert.equal(view.delegate(), view, '#delegate returns the view instance');
});
test("delegateEvents allows functions for callbacks", 3, function() {
QUnit.test("delegateEvents allows functions for callbacks", function(assert) {
assert.expect(3);
var view = new Backbone.View({el: '<p></p>'});
view.counter = 0;
@@ -106,24 +114,26 @@
view.delegateEvents(events);
view.$el.trigger('click');
equal(view.counter, 1);
assert.equal(view.counter, 1);
view.$el.trigger('click');
equal(view.counter, 2);
assert.equal(view.counter, 2);
view.delegateEvents(events);
view.$el.trigger('click');
equal(view.counter, 3);
assert.equal(view.counter, 3);
});
test("delegateEvents ignore undefined methods", 0, function() {
QUnit.test("delegateEvents ignore undefined methods", function(assert) {
assert.expect(0);
var view = new Backbone.View({el: '<p></p>'});
view.delegateEvents({'click': 'undefinedMethod'});
view.$el.trigger('click');
});
test("undelegateEvents", 7, function() {
QUnit.test("undelegateEvents", function(assert) {
assert.expect(7);
var counter1 = 0, counter2 = 0;
var view = new Backbone.View({el: '#testElement'});
@@ -134,107 +144,116 @@
view.delegateEvents(events);
view.$('h1').trigger('click');
equal(counter1, 1);
equal(counter2, 1);
assert.equal(counter1, 1);
assert.equal(counter2, 1);
view.undelegateEvents();
view.$('h1').trigger('click');
equal(counter1, 1);
equal(counter2, 2);
assert.equal(counter1, 1);
assert.equal(counter2, 2);
view.delegateEvents(events);
view.$('h1').trigger('click');
equal(counter1, 2);
equal(counter2, 3);
assert.equal(counter1, 2);
assert.equal(counter2, 3);
equal(view.undelegateEvents(), view, '#undelegateEvents returns the view instance');
assert.equal(view.undelegateEvents(), view, '#undelegateEvents returns the view instance');
});
test("undelegate", 1, function() {
QUnit.test("undelegate", function(assert) {
assert.expect(1);
view = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { ok(false); });
view.delegate('click', 'h1', function() { ok(false); });
view.delegate('click', function() { assert.ok(false); });
view.delegate('click', 'h1', function() { assert.ok(false); });
view.undelegate('click');
view.$('h1').trigger('click');
view.$el.trigger('click');
equal(view.undelegate(), view, '#undelegate returns the view instance');
assert.equal(view.undelegate(), view, '#undelegate returns the view instance');
});
test("undelegate with passed handler", 1, function() {
QUnit.test("undelegate with passed handler", function(assert) {
assert.expect(1);
view = new Backbone.View({el: '#testElement'});
var listener = function() { ok(false); };
var listener = function() { assert.ok(false); };
view.delegate('click', listener);
view.delegate('click', function() { ok(true); });
view.delegate('click', function() { assert.ok(true); });
view.undelegate('click', listener);
view.$el.trigger('click');
});
test("undelegate with selector", 2, function() {
QUnit.test("undelegate with selector", function(assert) {
assert.expect(2);
view = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { ok(true); });
view.delegate('click', 'h1', function() { ok(false); });
view.delegate('click', function() { assert.ok(true); });
view.delegate('click', 'h1', function() { assert.ok(false); });
view.undelegate('click', 'h1');
view.$('h1').trigger('click');
view.$el.trigger('click');
});
test("undelegate with handler and selector", 2, function() {
QUnit.test("undelegate with handler and selector", function(assert) {
assert.expect(2);
view = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { ok(true); });
var handler = function(){ ok(false); };
view.delegate('click', function() { assert.ok(true); });
var handler = function(){ assert.ok(false); };
view.delegate('click', 'h1', handler);
view.undelegate('click', 'h1', handler);
view.$('h1').trigger('click');
view.$el.trigger('click');
});
test("tagName can be provided as a string", 1, function() {
QUnit.test("tagName can be provided as a string", function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
tagName: 'span'
});
equal(new View().el.tagName, 'SPAN');
assert.equal(new View().el.tagName, 'SPAN');
});
test("tagName can be provided as a function", 1, function() {
QUnit.test("tagName can be provided as a function", function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
tagName: function() {
return 'p';
}
});
ok(new View().$el.is('p'));
assert.ok(new View().$el.is('p'));
});
test("_ensureElement with DOM node el", 1, function() {
QUnit.test("_ensureElement with DOM node el", function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
el: document.body
});
equal(new View().el, document.body);
assert.equal(new View().el, document.body);
});
test("_ensureElement with string el", 3, function() {
QUnit.test("_ensureElement with string el", function(assert) {
assert.expect(3);
var View = Backbone.View.extend({
el: "body"
});
strictEqual(new View().el, document.body);
assert.strictEqual(new View().el, document.body);
View = Backbone.View.extend({
el: "#testElement > h1"
});
strictEqual(new View().el, $("#testElement > h1").get(0));
assert.strictEqual(new View().el, $("#testElement > h1").get(0));
View = Backbone.View.extend({
el: "#nonexistent"
});
ok(!new View().el);
assert.ok(!new View().el);
});
test("with className and id functions", 2, function() {
QUnit.test("with className and id functions", function(assert) {
assert.expect(2);
var View = Backbone.View.extend({
className: function() {
return 'className';
@@ -244,11 +263,12 @@
}
});
strictEqual(new View().el.className, 'className');
strictEqual(new View().el.id, 'id');
assert.strictEqual(new View().el.className, 'className');
assert.strictEqual(new View().el.id, 'id');
});
test("with attributes", 2, function() {
QUnit.test("with attributes", function(assert) {
assert.expect(2);
var View = Backbone.View.extend({
attributes: {
id: 'id',
@@ -256,21 +276,41 @@
}
});
strictEqual(new View().el.className, 'class');
strictEqual(new View().el.id, 'id');
assert.strictEqual(new View().el.className, 'class');
assert.strictEqual(new View().el.id, 'id');
});
test("with attributes as a function", 1, function() {
QUnit.test("with attributes as a function", function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
attributes: function() {
return {'class': 'dynamic'};
}
});
strictEqual(new View().el.className, 'dynamic');
assert.strictEqual(new View().el.className, 'dynamic');
});
test("multiple views per element", 3, function() {
QUnit.test("should default to className/id properties", function(assert) {
assert.expect(4);
var View = Backbone.View.extend({
className: 'backboneClass',
id: 'backboneId',
attributes: {
'class': 'attributeClass',
'id': 'attributeId'
}
});
var view = new View;
assert.strictEqual(view.el.className, 'backboneClass');
assert.strictEqual(view.el.id, 'backboneId');
assert.strictEqual(view.$el.attr('class'), 'backboneClass');
assert.strictEqual(view.$el.attr('id'), 'backboneId');
});
QUnit.test("multiple views per element", function(assert) {
assert.expect(3);
var count = 0;
var $el = $('<p></p>');
@@ -285,22 +325,23 @@
var view1 = new View;
$el.trigger("click");
equal(1, count);
assert.equal(1, count);
var view2 = new View;
$el.trigger("click");
equal(3, count);
assert.equal(3, count);
view1.delegateEvents();
$el.trigger("click");
equal(5, count);
assert.equal(5, count);
});
test("custom events", 2, function() {
QUnit.test("custom events", function(assert) {
assert.expect(2);
var View = Backbone.View.extend({
el: $('body'),
events: {
"fake$event": function() { ok(true); }
"fake$event": function() { assert.ok(true); }
}
});
@@ -311,24 +352,26 @@
$('body').trigger('fake$event');
});
test("#1048 - setElement uses provided object.", 2, function() {
QUnit.test("#1048 - setElement uses provided object.", function(assert) {
assert.expect(2);
var $el = $('body');
var view = new Backbone.View({el: $el});
ok(view.$el === $el);
assert.ok(view.$el === $el);
view.setElement($el = $($el));
ok(view.$el === $el);
assert.ok(view.$el === $el);
});
test("#986 - Undelegate before changing element.", 1, function() {
QUnit.test("#986 - Undelegate before changing element.", function(assert) {
assert.expect(1);
var button1 = $('<button></button>');
var button2 = $('<button></button>');
var View = Backbone.View.extend({
events: {
click: function(e) {
ok(view.el === e.target);
assert.ok(view.el === e.target);
}
}
});
@@ -340,23 +383,25 @@
button2.trigger('click');
});
test("#1172 - Clone attributes object", 2, function() {
QUnit.test("#1172 - Clone attributes object", function(assert) {
assert.expect(2);
var View = Backbone.View.extend({
attributes: {foo: 'bar'}
});
var view1 = new View({id: 'foo'});
strictEqual(view1.el.id, 'foo');
assert.strictEqual(view1.el.id, 'foo');
var view2 = new View();
ok(!view2.el.id);
assert.ok(!view2.el.id);
});
test("views stopListening", 0, function() {
QUnit.test("views stopListening", function(assert) {
assert.expect(0);
var View = Backbone.View.extend({
initialize: function() {
this.listenTo(this.model, 'all x', function(){ ok(false); });
this.listenTo(this.collection, 'all x', function(){ ok(false); });
this.listenTo(this.model, 'all x', function(){ assert.ok(false); });
this.listenTo(this.collection, 'all x', function(){ assert.ok(false); });
}
});
@@ -370,7 +415,8 @@
view.collection.trigger('x');
});
test("Provide function for el.", 2, function() {
QUnit.test("Provide function for el.", function(assert) {
assert.expect(2);
var View = Backbone.View.extend({
el: function() {
return "<p><a></a></p>";
@@ -378,11 +424,12 @@
});
var view = new View;
ok(view.$el.is('p'));
ok(view.$el.has('a'));
assert.ok(view.$el.is('p'));
assert.ok(view.$el.has('a'));
});
test("events passed in options", 1, function() {
QUnit.test("events passed in options", function(assert) {
assert.expect(1);
var counter = 0;
var View = Backbone.View.extend({
@@ -399,32 +446,34 @@
});
view.$('h1').trigger('click').trigger('click');
equal(counter, 2);
assert.equal(counter, 2);
});
test("remove", 2, function() {
QUnit.test("remove", function(assert) {
assert.expect(2);
var view = new Backbone.View;
document.body.appendChild(view.el);
view.delegate('click', function() { ok(false); });
view.listenTo(view, 'all x', function() { ok(false); });
view.delegate('click', function() { assert.ok(false); });
view.listenTo(view, 'all x', function() { assert.ok(false); });
equal(view.remove(), view, '#remove returns the view instance');
assert.equal(view.remove(), view, '#remove returns the view instance');
view.$el.trigger('click');
view.trigger('x');
// In IE8 and below, parentNode still exists but is not document.body.
notEqual(view.el.parentNode, document.body);
assert.notEqual(view.el.parentNode, document.body);
});
test("setElement", 3, function() {
QUnit.test("setElement", function(assert) {
assert.expect(3);
var view = new Backbone.View({
events: {
click: function() { ok(false); }
click: function() { assert.ok(false); }
}
});
view.events = {
click: function() { ok(true); }
click: function() { assert.ok(true); }
};
var oldEl = view.el;
var $oldEl = view.$el;
@@ -434,8 +483,8 @@
$oldEl.click();
view.$el.click();
notEqual(oldEl, view.el);
notEqual($oldEl, view.$el);
assert.notEqual(oldEl, view.el);
assert.notEqual($oldEl, view.$el);
});
})();