Skip to content

Commit 943b660

Browse files
committed
upgrade Backbone tests to 0.9.2
1 parent be46388 commit 943b660

File tree

3 files changed

+447
-200
lines changed

3 files changed

+447
-200
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ user.bind('change:name.first', function(){ ... });
3030

3131
## Usage
3232

33-
1. Download the latest version [here](https://github.com/afeld/backbone-nested/tags), and add `backbone-nested.js` to your HTML `<head>`, **after** `backbone.js` is included ([tested](http://afeld.github.com/backbone-nested/test/) against [jQuery](http://jquery.com/) v1.7.1, [Underscore](http://documentcloud.github.com/underscore/) v1.3.1 and [Backbone](http://documentcloud.github.com/backbone/) v0.9.1).
33+
1. Download the latest version [here](https://github.com/afeld/backbone-nested/tags), and add `backbone-nested.js` to your HTML `<head>`, **after** `backbone.js` is included ([tested](http://afeld.github.com/backbone-nested/test/) against [jQuery](http://jquery.com/) v1.7.1, [Underscore](http://documentcloud.github.com/underscore/) v1.3.1 and [Backbone](http://documentcloud.github.com/backbone/) v0.9.2).
3434

3535
```html
3636
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

test/model.js

Lines changed: 166 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// yanked from Backbone 0.9.1 test suite
1+
// yanked from Backbone 0.9.2 test suite
22
$(document).ready(function() {
33

44
// test Backbone.NestedModel instead of Backbone.Model - reset at end of function
@@ -18,29 +18,31 @@ $(document).ready(function() {
1818
var klass = Backbone.Collection.extend({
1919
url : function() { return '/collection'; }
2020
});
21-
22-
var attrs, doc, collection;
21+
var doc, collection;
2322

2423
module("Backbone.Model", {
2524

2625
setup: function() {
27-
Backbone.sync = function() {
28-
lastRequest = _.toArray(arguments);
29-
sync.apply(this, arguments);
30-
};
31-
$.ajax = function(params) { ajaxParams = params; };
32-
urlRoot = Backbone.Model.prototype.urlRoot;
33-
Backbone.Model.prototype.urlRoot = '/';
34-
35-
attrs = {
26+
doc = new proxy({
3627
id : '1-the-tempest',
3728
title : "The Tempest",
3829
author : "Bill Shakespeare",
3930
length : 123
40-
};
41-
doc = new proxy(attrs);
31+
});
4232
collection = new klass();
4333
collection.add(doc);
34+
35+
Backbone.sync = function(method, model, options) {
36+
lastRequest = {
37+
method: method,
38+
model: model,
39+
options: options
40+
};
41+
sync.apply(this, arguments);
42+
};
43+
$.ajax = function(params) { ajaxParams = params; };
44+
urlRoot = Backbone.Model.prototype.urlRoot;
45+
Backbone.Model.prototype.urlRoot = '/';
4446
},
4547

4648
teardown: function() {
@@ -51,7 +53,6 @@ $(document).ready(function() {
5153

5254
});
5355

54-
5556
test("Model: initialize", function() {
5657
var Model = Backbone.Model.extend({
5758
initialize: function() {
@@ -86,11 +87,11 @@ $(document).ready(function() {
8687
});
8788

8889
test("Model: url", function() {
90+
doc.urlRoot = null;
8991
equal(doc.url(), '/collection/1-the-tempest');
9092
doc.collection.url = '/collection/';
9193
equal(doc.url(), '/collection/1-the-tempest');
9294
doc.collection = null;
93-
doc.urlRoot = null;
9495
raises(function() { doc.url(); });
9596
doc.collection = collection;
9697
});
@@ -119,9 +120,8 @@ $(document).ready(function() {
119120
});
120121

121122
test("Model: clone", function() {
122-
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
123-
a = new Backbone.Model(attrs);
124-
b = a.clone();
123+
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
124+
var b = a.clone();
125125
equal(a.get('foo'), 1);
126126
equal(a.get('bar'), 2);
127127
equal(a.get('baz'), 3);
@@ -134,14 +134,11 @@ $(document).ready(function() {
134134
});
135135

136136
test("Model: isNew", function() {
137-
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
138-
a = new Backbone.Model(attrs);
137+
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
139138
ok(a.isNew(), "it should be new");
140-
attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
141-
a = new Backbone.Model(attrs);
139+
a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 });
142140
ok(!a.isNew(), "any defined ID is legal, negative or positive");
143-
attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 };
144-
a = new Backbone.Model(attrs);
141+
a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 });
145142
ok(!a.isNew(), "any defined ID is legal, including zero");
146143
ok( new Backbone.Model({ }).isNew(), "is true when there is no id");
147144
ok(!new Backbone.Model({ 'id': 2 }).isNew(), "is false for a positive integer");
@@ -166,8 +163,7 @@ $(document).ready(function() {
166163
});
167164

168165
test("Model: has", function() {
169-
attrs = {};
170-
a = new Backbone.Model(attrs);
166+
var a = new Backbone.Model();
171167
equal(a.has("name"), false);
172168
_([true, "Truth!", 1, false, '', 0]).each(function(value) {
173169
a.set({'name': value});
@@ -183,8 +179,7 @@ $(document).ready(function() {
183179

184180
test("Model: set and unset", function() {
185181
expect(8);
186-
attrs = {id: 'id', foo: 1, bar: 2, baz: 3};
187-
a = new Backbone.Model(attrs);
182+
var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
188183
var changeCount = 0;
189184
a.on("change:foo", function() { changeCount += 1; });
190185
a.set({'foo': 2});
@@ -330,7 +325,7 @@ $(document).ready(function() {
330325
var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
331326
model.on('change', function () {
332327
model.save();
333-
ok(_.isEqual(lastRequest[1], model));
328+
ok(_.isEqual(lastRequest.model, model));
334329
});
335330
model.set({lastName: 'Hicks'});
336331
});
@@ -364,8 +359,8 @@ $(document).ready(function() {
364359

365360
test("Model: save", function() {
366361
doc.save({title : "Henry V"});
367-
equal(lastRequest[0], 'update');
368-
ok(_.isEqual(lastRequest[1], doc));
362+
equal(lastRequest.method, 'update');
363+
ok(_.isEqual(lastRequest.model, doc));
369364
});
370365

371366
test("Model: save in positional style", function() {
@@ -377,21 +372,25 @@ $(document).ready(function() {
377372
equal(model.get('title'), 'Twelfth Night');
378373
});
379374

375+
376+
380377
test("Model: fetch", function() {
381378
doc.fetch();
382-
equal(lastRequest[0], 'read');
383-
ok(_.isEqual(lastRequest[1], doc));
379+
equal(lastRequest.method, 'read');
380+
ok(_.isEqual(lastRequest.model, doc));
384381
});
385382

386383
test("Model: destroy", function() {
387384
doc.destroy();
388-
equal(lastRequest[0], 'delete');
389-
ok(_.isEqual(lastRequest[1], doc));
385+
equal(lastRequest.method, 'delete');
386+
ok(_.isEqual(lastRequest.model, doc));
387+
388+
var newModel = new Backbone.Model;
389+
equal(newModel.destroy(), false);
390390
});
391391

392392
test("Model: non-persisted destroy", function() {
393-
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
394-
a = new Backbone.Model(attrs);
393+
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
395394
a.sync = function() { throw "should not be called"; };
396395
a.destroy();
397396
ok(true, "non-persisted model should not call sync");
@@ -520,8 +519,8 @@ $(document).ready(function() {
520519
}
521520
});
522521

523-
a = new A();
524-
b = new B({a: a});
522+
var a = new A();
523+
var b = new B({a: a});
525524
a.set({state: 'hello'});
526525
});
527526

@@ -594,18 +593,10 @@ $(document).ready(function() {
594593
ok(!model.hasChanged());
595594
});
596595

597-
test("set/hasChanged object prototype props", function() {
598-
var model = new Backbone.Model();
599-
ok(!model.hasChanged('toString'));
600-
model.set({toString: undefined});
601-
model.unset('toString', {silent: true});
602-
ok(model.hasChanged());
603-
});
604-
605596
test("save with `wait` succeeds without `validate`", function() {
606597
var model = new Backbone.Model();
607598
model.save({x: 1}, {wait: true});
608-
ok(lastRequest[1] === model);
599+
ok(lastRequest.model === model);
609600
});
610601

611602
test("`hasChanged` for falsey keys", function() {
@@ -630,20 +621,44 @@ $(document).ready(function() {
630621
deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
631622
equal(model.get('x'), 1);
632623
equal(changed, 0);
633-
lastRequest[2].success({});
624+
lastRequest.options.success({});
625+
equal(model.get('x'), 3);
626+
equal(changed, 1);
627+
});
628+
629+
test("`save` with `wait` results in correct attributes if success is called during sync", function() {
630+
var changed = 0;
631+
var model = new Backbone.Model({x: 1, y: 2});
632+
model.sync = function(method, model, options) {
633+
options.success();
634+
};
635+
model.on("change:x", function() { changed++; });
636+
model.save({x: 3}, {wait: true});
634637
equal(model.get('x'), 3);
635638
equal(changed, 1);
636639
});
637640

638-
test("nested `set` during `'change:attr'`", 1, function() {
641+
test("save with wait validates attributes", 1, function() {
642+
var model = new Backbone.Model();
643+
model.validate = function() { ok(true); };
644+
model.save({x: 1}, {wait: true});
645+
});
646+
647+
test("nested `set` during `'change:attr'`", function() {
648+
var events = [];
639649
var model = new Backbone.Model();
640-
model.on('change:x', function() { ok(true); });
641-
model.on('change:y', function() {
642-
model.set({x: true});
643-
// only fires once
644-
model.set({x: true});
650+
model.on('all', function(event) { events.push(event); });
651+
model.on('change', function() {
652+
model.set({z: true}, {silent:true});
653+
});
654+
model.on('change:x', function() {
655+
model.set({y: true});
645656
});
646-
model.set({y: true});
657+
model.set({x: true});
658+
deepEqual(events, ['change:y', 'change:x', 'change']);
659+
events = [];
660+
model.change();
661+
deepEqual(events, ['change:z', 'change']);
647662
});
648663

649664
test("nested `change` only fires once", 1, function() {
@@ -661,27 +676,118 @@ $(document).ready(function() {
661676
model.change();
662677
});
663678

664-
test("nested `set` suring `'change'`", 3, function() {
679+
test("nested `set` during `'change'`", 6, function() {
665680
var count = 0;
666681
var model = new Backbone.Model();
667682
model.on('change', function() {
668683
switch(count++) {
669684
case 0:
670685
deepEqual(this.changedAttributes(), {x: true});
686+
equal(model.previous('x'), undefined);
671687
model.set({y: true});
672688
break;
673689
case 1:
674-
deepEqual(this.changedAttributes(), {x: true, y: true});
690+
deepEqual(this.changedAttributes(), {y: true});
691+
equal(model.previous('x'), true);
675692
model.set({z: true});
676693
break;
677694
case 2:
678-
deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
695+
deepEqual(this.changedAttributes(), {z: true});
696+
equal(model.previous('y'), true);
697+
break;
698+
default:
699+
ok(false);
700+
}
701+
});
702+
model.set({x: true});
703+
});
704+
705+
test("nested `'change'` with silent", 3, function() {
706+
var count = 0;
707+
var model = new Backbone.Model();
708+
model.on('change:y', function() { ok(true); });
709+
model.on('change', function() {
710+
switch(count++) {
711+
case 0:
712+
deepEqual(this.changedAttributes(), {x: true});
713+
model.set({y: true}, {silent: true});
714+
break;
715+
case 1:
716+
deepEqual(this.changedAttributes(), {y: true, z: true});
679717
break;
680718
default:
681719
ok(false);
682720
}
683721
});
684722
model.set({x: true});
723+
model.set({z: true});
724+
});
725+
726+
test("nested `'change:attr'` with silent", 1, function() {
727+
var model = new Backbone.Model();
728+
model.on('change:y', function(){ ok(true); });
729+
model.on('change', function() {
730+
model.set({y: true}, {silent: true});
731+
model.set({z: true});
732+
});
733+
model.set({x: true});
734+
});
735+
736+
test("multiple nested changes with silent", 1, function() {
737+
var model = new Backbone.Model();
738+
model.on('change:x', function() {
739+
model.set({y: 1}, {silent: true});
740+
model.set({y: 2});
741+
});
742+
model.on('change:y', function(model, val) {
743+
equal(val, 2);
744+
});
745+
model.set({x: true});
746+
model.change();
747+
});
748+
749+
test("multiple nested changes with silent", function() {
750+
var changes = [];
751+
var model = new Backbone.Model();
752+
model.on('change:b', function(model, val) { changes.push(val); });
753+
model.on('change', function() {
754+
model.set({b: 1});
755+
model.set({b: 2}, {silent: true});
756+
});
757+
model.set({b: 0});
758+
deepEqual(changes, [0, 1, 1]);
759+
model.change();
760+
deepEqual(changes, [0, 1, 1, 2, 1]);
761+
});
762+
763+
test("nested set multiple times", 1, function() {
764+
var model = new Backbone.Model();
765+
model.on('change:b', function() {
766+
ok(true);
767+
});
768+
model.on('change:a', function() {
769+
model.set({b: true});
770+
model.set({b: true});
771+
});
772+
model.set({a: true});
773+
});
774+
775+
test("Backbone.wrapError triggers `'error'`", 12, function() {
776+
var resp = {};
777+
var options = {};
778+
var model = new Backbone.Model();
779+
model.on('error', error);
780+
var callback = Backbone.wrapError(null, model, options);
781+
callback(model, resp);
782+
callback(resp);
783+
callback = Backbone.wrapError(error, model, options);
784+
callback(model, resp);
785+
callback(resp);
786+
function error(_model, _resp, _options) {
787+
ok(model === _model);
788+
ok(resp === _resp);
789+
ok(options === _options);
790+
}
685791
});
686792

687793

0 commit comments

Comments
 (0)