PageRenderTime 4ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 2ms app.codeStats 0ms

/test/model.js

https://github.com/hlissnake/backbone
JavaScript | 424 lines | 375 code | 46 blank | 3 comment | 8 complexity | ec5a6bcffcf8794e2237d341cf073837 MD5 | raw file
  1$(document).ready(function() {
  2
  3  module("Backbone.Model");
  4
  5  // Variable to catch the last request.
  6  window.lastRequest = null;
  7
  8  window.originalSync = Backbone.sync;
  9
 10  // Stub out Backbone.request...
 11  Backbone.sync = function() {
 12    lastRequest = _.toArray(arguments);
 13  };
 14
 15  var attrs = {
 16    id     : '1-the-tempest',
 17    title  : "The Tempest",
 18    author : "Bill Shakespeare",
 19    length : 123
 20  };
 21
 22  var proxy = Backbone.Model.extend();
 23  var doc = new proxy(attrs);
 24
 25  var klass = Backbone.Collection.extend({
 26    url : function() { return '/collection'; }
 27  });
 28
 29  var collection = new klass();
 30  collection.add(doc);
 31
 32  test("Model: initialize", function() {
 33    var Model = Backbone.Model.extend({
 34      initialize: function() {
 35        this.one = 1;
 36        equals(this.collection, collection);
 37      }
 38    });
 39    var model = new Model({}, {collection: collection});
 40    equals(model.one, 1);
 41    equals(model.collection, collection);
 42  });
 43
 44  test("Model: initialize with attributes and options", function() {
 45    var Model = Backbone.Model.extend({
 46      initialize: function(attributes, options) {
 47        this.one = options.one;
 48      }
 49    });
 50    var model = new Model({}, {one: 1});
 51    equals(model.one, 1);
 52  });
 53
 54  test("Model: url", function() {
 55    equals(doc.url(), '/collection/1-the-tempest');
 56    doc.collection.url = '/collection/';
 57    equals(doc.url(), '/collection/1-the-tempest');
 58    doc.collection = null;
 59    var failed = false;
 60    try {
 61      doc.url();
 62    } catch (e) {
 63      failed = true;
 64    }
 65    equals(failed, true);
 66    doc.collection = collection;
 67  });
 68
 69  test("Model: url when using urlRoot, and uri encoding", function() {
 70    var Model = Backbone.Model.extend({
 71      urlRoot: '/collection'
 72    });
 73    var model = new Model();
 74    equals(model.url(), '/collection');
 75    model.set({id: '+1+'});
 76    equals(model.url(), '/collection/%2B1%2B');
 77  });
 78
 79  test("Model: clone", function() {
 80    attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
 81    a = new Backbone.Model(attrs);
 82    b = a.clone();
 83    equals(a.get('foo'), 1);
 84    equals(a.get('bar'), 2);
 85    equals(a.get('baz'), 3);
 86    equals(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
 87    equals(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
 88    equals(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
 89    a.set({foo : 100});
 90    equals(a.get('foo'), 100);
 91    equals(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
 92  });
 93
 94  test("Model: isNew", function() {
 95    attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
 96    a = new Backbone.Model(attrs);
 97    ok(a.isNew(), "it should be new");
 98    attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
 99    a = new Backbone.Model(attrs);
100    ok(!a.isNew(), "any defined ID is legal, negative or positive");
101    attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 };
102    a = new Backbone.Model(attrs);
103    ok(!a.isNew(), "any defined ID is legal, including zero");
104    ok( new Backbone.Model({          }).isNew(), "is true when there is no id");
105    ok(!new Backbone.Model({ 'id': 2  }).isNew(), "is false for a positive integer");
106    ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
107  });
108
109  test("Model: get", function() {
110    equals(doc.get('title'), 'The Tempest');
111    equals(doc.get('author'), 'Bill Shakespeare');
112  });
113
114  test("Model: escape", function() {
115    equals(doc.escape('title'), 'The Tempest');
116    doc.set({audience: 'Bill & Bob'});
117    equals(doc.escape('audience'), 'Bill & Bob');
118    doc.set({audience: 'Tim > Joan'});
119    equals(doc.escape('audience'), 'Tim > Joan');
120    doc.set({audience: 10101});
121    equals(doc.escape('audience'), '10101');
122    doc.unset('audience');
123    equals(doc.escape('audience'), '');
124  });
125
126  test("Model: has", function() {
127    attrs = {};
128    a = new Backbone.Model(attrs);
129    equals(a.has("name"), false);
130    _([true, "Truth!", 1, false, '', 0]).each(function(value) {
131      a.set({'name': value});
132      equals(a.has("name"), true);
133    });
134    a.unset('name');
135    equals(a.has('name'), false);
136    _([null, undefined]).each(function(value) {
137      a.set({'name': value});
138      equals(a.has("name"), false);
139    });
140  });
141
142  test("Model: set and unset", function() {
143    attrs = {id: 'id', foo: 1, bar: 2, baz: 3};
144    a = new Backbone.Model(attrs);
145    var changeCount = 0;
146    a.bind("change:foo", function() { changeCount += 1; });
147    a.set({'foo': 2});
148    ok(a.get('foo')== 2, "Foo should have changed.");
149    ok(changeCount == 1, "Change count should have incremented.");
150    a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
151    ok(a.get('foo')== 2, "Foo should NOT have changed, still 2");
152    ok(changeCount == 1, "Change count should NOT have incremented.");
153
154    a.unset('foo');
155    ok(a.get('foo')== null, "Foo should have changed");
156    ok(changeCount == 2, "Change count should have incremented for unset.");
157
158    a.unset('id');
159    equals(a.id, undefined, "Unsetting the id should remove the id property.");
160  });
161
162  test("Model: multiple unsets", function() {
163    var i = 0;
164    var counter = function(){ i++; };
165    var model = new Backbone.Model({a: 1});
166    model.bind("change:a", counter);
167    model.set({a: 2});
168    model.unset('a');
169    model.unset('a');
170    equals(i, 2, 'Unset does not fire an event for missing attributes.');
171  });
172
173  test("Model: using a non-default id attribute.", function() {
174    var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
175    var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
176    equals(model.get('id'), 'eye-dee');
177    equals(model.id, 25);
178    model.unset('_id');
179    equals(model.id, undefined);
180  });
181
182  test("Model: set an empty string", function() {
183    var model = new Backbone.Model({name : "Model"});
184    model.set({name : ''});
185    equals(model.get('name'), '');
186  });
187
188  test("Model: clear", function() {
189    var changed;
190    var model = new Backbone.Model({name : "Model"});
191    model.bind("change:name", function(){ changed = true; });
192    model.clear();
193    equals(changed, true);
194    equals(model.get('name'), undefined);
195  });
196
197  test("Model: defaults", function() {
198    var Defaulted = Backbone.Model.extend({
199      defaults: {
200        "one": 1,
201        "two": 2
202      }
203    });
204    var model = new Defaulted({two: null});
205    equals(model.get('one'), 1);
206    equals(model.get('two'), null);
207    Defaulted = Backbone.Model.extend({
208      defaults: function() {
209        return {
210          "one": 3,
211          "two": 4
212        };
213      }
214    });
215    var model = new Defaulted({two: null});
216    equals(model.get('one'), 3);
217    equals(model.get('two'), null);
218  });
219
220  test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
221    var model = new Backbone.Model({name : "Tim", age : 10});
222    equals(model.changedAttributes(), false);
223    model.bind('change', function() {
224      ok(model.hasChanged('name'), 'name changed');
225      ok(!model.hasChanged('age'), 'age did not');
226      ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
227      equals(model.previous('name'), 'Tim');
228      ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
229    });
230    model.set({name : 'Rob'}, {silent : true});
231    equals(model.hasChanged(), true);
232    equals(model.hasChanged('name'), true);
233    model.change();
234    equals(model.get('name'), 'Rob');
235  });
236
237  test("Model: change with options", function() {
238    var value;
239    var model = new Backbone.Model({name: 'Rob'});
240    model.bind('change', function(model, options) {
241      value = options.prefix + model.get('name');
242    });
243    model.set({name: 'Bob'}, {silent: true});
244    model.change({prefix: 'Mr. '});
245    equals(value, 'Mr. Bob');
246    model.set({name: 'Sue'}, {prefix: 'Ms. '});
247    equals(value, 'Ms. Sue');
248  });
249
250  test("Model: change after initialize", function () {
251    var changed = 0;
252    var attrs = {id: 1, label: 'c'};
253    var obj = new Backbone.Model(attrs);
254    obj.bind('change', function() { changed += 1; });
255    obj.set(attrs);
256    equals(changed, 0);
257  });
258
259  test("Model: save within change event", function () {
260    var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
261    model.bind('change', function () {
262      model.save();
263      ok(_.isEqual(lastRequest[1], model));
264    });
265    model.set({lastName: 'Hicks'});
266  });
267
268  test("Model: save", function() {
269    doc.save({title : "Henry V"});
270    equals(lastRequest[0], 'update');
271    ok(_.isEqual(lastRequest[1], doc));
272  });
273
274  test("Model: fetch", function() {
275    doc.fetch();
276    ok(lastRequest[0], 'read');
277    ok(_.isEqual(lastRequest[1], doc));
278  });
279
280  test("Model: destroy", function() {
281    doc.destroy();
282    equals(lastRequest[0], 'delete');
283    ok(_.isEqual(lastRequest[1], doc));
284  });
285
286  test("Model: non-persisted destroy", function() {
287    attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
288    a = new Backbone.Model(attrs);
289    a.sync = function() { throw "should not be called"; };
290    ok(a.destroy(), "non-persisted model should not call sync");
291  });
292
293  test("Model: validate", function() {
294    var lastError;
295    var model = new Backbone.Model();
296    model.validate = function(attrs) {
297      if (attrs.admin) return "Can't change admin status.";
298    };
299    model.bind('error', function(model, error) {
300      lastError = error;
301    });
302    var result = model.set({a: 100});
303    equals(result, model);
304    equals(model.get('a'), 100);
305    equals(lastError, undefined);
306    result = model.set({admin: true}, {silent: true});
307    equals(lastError, undefined);
308    equals(model.get('admin'), true);
309    result = model.set({a: 200, admin: true});
310    equals(result, false);
311    equals(model.get('a'), 100);
312    equals(lastError, "Can't change admin status.");
313  });
314
315  test("Model: validate on unset and clear", function() {
316    var error;
317    var model = new Backbone.Model({name: "One"});
318    model.validate = function(attrs) {
319      if ("name" in attrs) {
320        if (!attrs.name) {
321          error = true;
322          return "No thanks.";
323        }
324      }
325    };
326    model.set({name: "Two"});
327    equals(model.get('name'), 'Two');
328    equals(error, undefined);
329    model.unset('name');
330    equals(error, true);
331    equals(model.get('name'), 'Two');
332    model.clear();
333    equals(model.get('name'), 'Two');
334    delete model.validate;
335    model.clear();
336    equals(model.get('name'), undefined);
337  });
338
339  test("Model: validate with error callback", function() {
340    var lastError, boundError;
341    var model = new Backbone.Model();
342    model.validate = function(attrs) {
343      if (attrs.admin) return "Can't change admin status.";
344    };
345    var callback = function(model, error) {
346      lastError = error;
347    };
348    model.bind('error', function(model, error) {
349      boundError = true;
350    });
351    var result = model.set({a: 100}, {error: callback});
352    equals(result, model);
353    equals(model.get('a'), 100);
354    equals(lastError, undefined);
355    equals(boundError, undefined);
356    result = model.set({a: 200, admin: true}, {error: callback});
357    equals(result, false);
358    equals(model.get('a'), 100);
359    equals(lastError, "Can't change admin status.");
360    equals(boundError, undefined);
361  });
362
363  test("Model: defaults always extend attrs (#459)", function() {
364    var Defaulted = Backbone.Model.extend({
365      defaults: {one: 1},
366      initialize : function(attrs, opts) {
367        equals(attrs.one, 1);
368      }
369    });
370    var providedattrs = new Defaulted({});
371    var emptyattrs = new Defaulted();
372  });
373
374  test("Model: Inherit class properties", function() {
375    var Parent = Backbone.Model.extend({
376      instancePropSame: function() {},
377      instancePropDiff: function() {}
378    }, {
379      classProp: function() {}
380    });
381    var Child = Parent.extend({
382      instancePropDiff: function() {}
383    });
384
385    var adult = new Parent;
386    var kid   = new Child;
387
388    equals(Child.classProp, Parent.classProp);
389    notEqual(Child.classProp, undefined);
390
391    equals(kid.instancePropSame, adult.instancePropSame);
392    notEqual(kid.instancePropSame, undefined);
393
394    notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
395    notEqual(Child.prototype.instancePropDiff, undefined);
396  });
397
398  test("Model: Nested change events don't clobber previous attributes", function() {
399    var A = Backbone.Model.extend({
400      initialize: function() {
401        this.bind("change:state", function(a, newState) {
402          equals(a.previous('state'), undefined);
403          equals(newState, 'hello');
404          // Fire a nested change event.
405          this.set({ other: "whatever" });
406        });
407      }
408    });
409
410    var B = Backbone.Model.extend({
411      initialize: function() {
412        this.get("a").bind("change:state", function(a, newState) {
413          equals(a.previous('state'), undefined);
414          equals(newState, 'hello');
415        });
416      }
417    });
418
419    a = new A();
420    b = new B({a: a});
421    a.set({state: 'hello'});
422  });
423
424});