Built motion from commit 1038d87.|0.0.141
[motion.git] / public / bower_components / lodash / vendor / backbone / test / model.js
1 (function() {
2
3   var ProxyModel = Backbone.Model.extend();
4   var Klass = Backbone.Collection.extend({
5     url: function() { return '/collection'; }
6   });
7   var doc, collection;
8
9   QUnit.module('Backbone.Model', {
10
11     beforeEach: function(assert) {
12       doc = new ProxyModel({
13         id: '1-the-tempest',
14         title: 'The Tempest',
15         author: 'Bill Shakespeare',
16         length: 123
17       });
18       collection = new Klass();
19       collection.add(doc);
20     }
21
22   });
23
24   QUnit.test('initialize', function(assert) {
25     assert.expect(3);
26     var Model = Backbone.Model.extend({
27       initialize: function() {
28         this.one = 1;
29         assert.equal(this.collection, collection);
30       }
31     });
32     var model = new Model({}, {collection: collection});
33     assert.equal(model.one, 1);
34     assert.equal(model.collection, collection);
35   });
36
37   QUnit.test('initialize with attributes and options', function(assert) {
38     assert.expect(1);
39     var Model = Backbone.Model.extend({
40       initialize: function(attributes, options) {
41         this.one = options.one;
42       }
43     });
44     var model = new Model({}, {one: 1});
45     assert.equal(model.one, 1);
46   });
47
48   QUnit.test('initialize with parsed attributes', function(assert) {
49     assert.expect(1);
50     var Model = Backbone.Model.extend({
51       parse: function(attrs) {
52         attrs.value += 1;
53         return attrs;
54       }
55     });
56     var model = new Model({value: 1}, {parse: true});
57     assert.equal(model.get('value'), 2);
58   });
59
60   QUnit.test('initialize with defaults', function(assert) {
61     assert.expect(2);
62     var Model = Backbone.Model.extend({
63       defaults: {
64         firstName: 'Unknown',
65         lastName: 'Unknown'
66       }
67     });
68     var model = new Model({'firstName': 'John'});
69     assert.equal(model.get('firstName'), 'John');
70     assert.equal(model.get('lastName'), 'Unknown');
71   });
72
73   QUnit.test('parse can return null', function(assert) {
74     assert.expect(1);
75     var Model = Backbone.Model.extend({
76       parse: function(attrs) {
77         attrs.value += 1;
78         return null;
79       }
80     });
81     var model = new Model({value: 1}, {parse: true});
82     assert.equal(JSON.stringify(model.toJSON()), '{}');
83   });
84
85   QUnit.test('url', function(assert) {
86     assert.expect(3);
87     doc.urlRoot = null;
88     assert.equal(doc.url(), '/collection/1-the-tempest');
89     doc.collection.url = '/collection/';
90     assert.equal(doc.url(), '/collection/1-the-tempest');
91     doc.collection = null;
92     assert.raises(function() { doc.url(); });
93     doc.collection = collection;
94   });
95
96   QUnit.test('url when using urlRoot, and uri encoding', function(assert) {
97     assert.expect(2);
98     var Model = Backbone.Model.extend({
99       urlRoot: '/collection'
100     });
101     var model = new Model();
102     assert.equal(model.url(), '/collection');
103     model.set({id: '+1+'});
104     assert.equal(model.url(), '/collection/%2B1%2B');
105   });
106
107   QUnit.test('url when using urlRoot as a function to determine urlRoot at runtime', function(assert) {
108     assert.expect(2);
109     var Model = Backbone.Model.extend({
110       urlRoot: function() {
111         return '/nested/' + this.get('parentId') + '/collection';
112       }
113     });
114
115     var model = new Model({parentId: 1});
116     assert.equal(model.url(), '/nested/1/collection');
117     model.set({id: 2});
118     assert.equal(model.url(), '/nested/1/collection/2');
119   });
120
121   QUnit.test('underscore methods', function(assert) {
122     assert.expect(5);
123     var model = new Backbone.Model({foo: 'a', bar: 'b', baz: 'c'});
124     var model2 = model.clone();
125     assert.deepEqual(model.keys(), ['foo', 'bar', 'baz']);
126     assert.deepEqual(model.values(), ['a', 'b', 'c']);
127     assert.deepEqual(model.invert(), {a: 'foo', b: 'bar', c: 'baz'});
128     assert.deepEqual(model.pick('foo', 'baz'), {foo: 'a', baz: 'c'});
129     assert.deepEqual(model.omit('foo', 'bar'), {baz: 'c'});
130   });
131
132   QUnit.test('chain', function(assert) {
133     var model = new Backbone.Model({a: 0, b: 1, c: 2});
134     assert.deepEqual(model.chain().pick('a', 'b', 'c').values().compact().value(), [1, 2]);
135   });
136
137   QUnit.test('clone', function(assert) {
138     assert.expect(10);
139     var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
140     var b = a.clone();
141     assert.equal(a.get('foo'), 1);
142     assert.equal(a.get('bar'), 2);
143     assert.equal(a.get('baz'), 3);
144     assert.equal(b.get('foo'), a.get('foo'), 'Foo should be the same on the clone.');
145     assert.equal(b.get('bar'), a.get('bar'), 'Bar should be the same on the clone.');
146     assert.equal(b.get('baz'), a.get('baz'), 'Baz should be the same on the clone.');
147     a.set({foo: 100});
148     assert.equal(a.get('foo'), 100);
149     assert.equal(b.get('foo'), 1, 'Changing a parent attribute does not change the clone.');
150
151     var foo = new Backbone.Model({p: 1});
152     var bar = new Backbone.Model({p: 2});
153     bar.set(foo.clone().attributes, {unset: true});
154     assert.equal(foo.get('p'), 1);
155     assert.equal(bar.get('p'), undefined);
156   });
157
158   QUnit.test('isNew', function(assert) {
159     assert.expect(6);
160     var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
161     assert.ok(a.isNew(), 'it should be new');
162     a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: -5});
163     assert.ok(!a.isNew(), 'any defined ID is legal, negative or positive');
164     a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: 0});
165     assert.ok(!a.isNew(), 'any defined ID is legal, including zero');
166     assert.ok(new Backbone.Model().isNew(), 'is true when there is no id');
167     assert.ok(!new Backbone.Model({id: 2}).isNew(), 'is false for a positive integer');
168     assert.ok(!new Backbone.Model({id: -5}).isNew(), 'is false for a negative integer');
169   });
170
171   QUnit.test('get', function(assert) {
172     assert.expect(2);
173     assert.equal(doc.get('title'), 'The Tempest');
174     assert.equal(doc.get('author'), 'Bill Shakespeare');
175   });
176
177   QUnit.test('escape', function(assert) {
178     assert.expect(5);
179     assert.equal(doc.escape('title'), 'The Tempest');
180     doc.set({audience: 'Bill & Bob'});
181     assert.equal(doc.escape('audience'), 'Bill & Bob');
182     doc.set({audience: 'Tim > Joan'});
183     assert.equal(doc.escape('audience'), 'Tim > Joan');
184     doc.set({audience: 10101});
185     assert.equal(doc.escape('audience'), '10101');
186     doc.unset('audience');
187     assert.equal(doc.escape('audience'), '');
188   });
189
190   QUnit.test('has', function(assert) {
191     assert.expect(10);
192     var model = new Backbone.Model();
193
194     assert.strictEqual(model.has('name'), false);
195
196     model.set({
197       '0': 0,
198       '1': 1,
199       'true': true,
200       'false': false,
201       'empty': '',
202       'name': 'name',
203       'null': null,
204       'undefined': undefined
205     });
206
207     assert.strictEqual(model.has('0'), true);
208     assert.strictEqual(model.has('1'), true);
209     assert.strictEqual(model.has('true'), true);
210     assert.strictEqual(model.has('false'), true);
211     assert.strictEqual(model.has('empty'), true);
212     assert.strictEqual(model.has('name'), true);
213
214     model.unset('name');
215
216     assert.strictEqual(model.has('name'), false);
217     assert.strictEqual(model.has('null'), false);
218     assert.strictEqual(model.has('undefined'), false);
219   });
220
221   QUnit.test('matches', function(assert) {
222     assert.expect(4);
223     var model = new Backbone.Model();
224
225     assert.strictEqual(model.matches({name: 'Jonas', cool: true}), false);
226
227     model.set({name: 'Jonas', cool: true});
228
229     assert.strictEqual(model.matches({name: 'Jonas'}), true);
230     assert.strictEqual(model.matches({name: 'Jonas', cool: true}), true);
231     assert.strictEqual(model.matches({name: 'Jonas', cool: false}), false);
232   });
233
234   QUnit.test('matches with predicate', function(assert) {
235     var model = new Backbone.Model({a: 0});
236
237     assert.strictEqual(model.matches(function(attr) {
238       return attr.a > 1 && attr.b != null;
239     }), false);
240
241     model.set({a: 3, b: true});
242
243     assert.strictEqual(model.matches(function(attr) {
244       return attr.a > 1 && attr.b != null;
245     }), true);
246   });
247
248   QUnit.test('set and unset', function(assert) {
249     assert.expect(8);
250     var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
251     var changeCount = 0;
252     a.on('change:foo', function() { changeCount += 1; });
253     a.set({foo: 2});
254     assert.equal(a.get('foo'), 2, 'Foo should have changed.');
255     assert.equal(changeCount, 1, 'Change count should have incremented.');
256     // set with value that is not new shouldn't fire change event
257     a.set({foo: 2});
258     assert.equal(a.get('foo'), 2, 'Foo should NOT have changed, still 2');
259     assert.equal(changeCount, 1, 'Change count should NOT have incremented.');
260
261     a.validate = function(attrs) {
262       assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting');
263     };
264     a.unset('foo', {validate: true});
265     assert.equal(a.get('foo'), void 0, 'Foo should have changed');
266     delete a.validate;
267     assert.equal(changeCount, 2, 'Change count should have incremented for unset.');
268
269     a.unset('id');
270     assert.equal(a.id, undefined, 'Unsetting the id should remove the id property.');
271   });
272
273   QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) {
274     var attr = 0, main = 0, error = 0;
275     var Model = Backbone.Model.extend({
276       validate: function(attrs) {
277         if (attrs.x > 1) {
278           error++;
279           return 'this is an error';
280         }
281       }
282     });
283     var model = new Model({x: 0});
284     model.on('change:x', function() { attr++; });
285     model.on('change', function() { main++; });
286     model.set({x: 2}, {validate: true});
287     model.set({x: 1}, {validate: true});
288     assert.deepEqual([attr, main, error], [1, 1, 1]);
289   });
290
291   QUnit.test('set triggers changes in the correct order', function(assert) {
292     var value = null;
293     var model = new Backbone.Model;
294     model.on('last', function(){ value = 'last'; });
295     model.on('first', function(){ value = 'first'; });
296     model.trigger('first');
297     model.trigger('last');
298     assert.equal(value, 'last');
299   });
300
301   QUnit.test('set falsy values in the correct order', function(assert) {
302     assert.expect(2);
303     var model = new Backbone.Model({result: 'result'});
304     model.on('change', function() {
305       assert.equal(model.changed.result, void 0);
306       assert.equal(model.previous('result'), false);
307     });
308     model.set({result: void 0}, {silent: true});
309     model.set({result: null}, {silent: true});
310     model.set({result: false}, {silent: true});
311     model.set({result: void 0});
312   });
313
314   QUnit.test('nested set triggers with the correct options', function(assert) {
315     var model = new Backbone.Model();
316     var o1 = {};
317     var o2 = {};
318     var o3 = {};
319     model.on('change', function(__, options) {
320       switch (model.get('a')) {
321         case 1:
322           assert.equal(options, o1);
323           return model.set('a', 2, o2);
324         case 2:
325           assert.equal(options, o2);
326           return model.set('a', 3, o3);
327         case 3:
328           assert.equal(options, o3);
329       }
330     });
331     model.set('a', 1, o1);
332   });
333
334   QUnit.test('multiple unsets', function(assert) {
335     assert.expect(1);
336     var i = 0;
337     var counter = function(){ i++; };
338     var model = new Backbone.Model({a: 1});
339     model.on('change:a', counter);
340     model.set({a: 2});
341     model.unset('a');
342     model.unset('a');
343     assert.equal(i, 2, 'Unset does not fire an event for missing attributes.');
344   });
345
346   QUnit.test('unset and changedAttributes', function(assert) {
347     assert.expect(1);
348     var model = new Backbone.Model({a: 1});
349     model.on('change', function() {
350       assert.ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
351     });
352     model.unset('a');
353   });
354
355   QUnit.test('using a non-default id attribute.', function(assert) {
356     assert.expect(5);
357     var MongoModel = Backbone.Model.extend({idAttribute: '_id'});
358     var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
359     assert.equal(model.get('id'), 'eye-dee');
360     assert.equal(model.id, 25);
361     assert.equal(model.isNew(), false);
362     model.unset('_id');
363     assert.equal(model.id, undefined);
364     assert.equal(model.isNew(), true);
365   });
366
367   QUnit.test('setting an alternative cid prefix', function(assert) {
368     assert.expect(4);
369     var Model = Backbone.Model.extend({
370       cidPrefix: 'm'
371     });
372     var model = new Model();
373
374     assert.equal(model.cid.charAt(0), 'm');
375
376     model = new Backbone.Model();
377     assert.equal(model.cid.charAt(0), 'c');
378
379     var Collection = Backbone.Collection.extend({
380       model: Model
381     });
382     var col = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]);
383
384     assert.equal(col.get('c6').cid.charAt(0), 'm');
385     col.set([{id: 'c6', value: 'test'}], {
386       merge: true,
387       add: true,
388       remove: false
389     });
390     assert.ok(col.get('c6').has('value'));
391   });
392
393   QUnit.test('set an empty string', function(assert) {
394     assert.expect(1);
395     var model = new Backbone.Model({name: 'Model'});
396     model.set({name: ''});
397     assert.equal(model.get('name'), '');
398   });
399
400   QUnit.test('setting an object', function(assert) {
401     assert.expect(1);
402     var model = new Backbone.Model({
403       custom: {foo: 1}
404     });
405     model.on('change', function() {
406       assert.ok(1);
407     });
408     model.set({
409       custom: {foo: 1} // no change should be fired
410     });
411     model.set({
412       custom: {foo: 2} // change event should be fired
413     });
414   });
415
416   QUnit.test('clear', function(assert) {
417     assert.expect(3);
418     var changed;
419     var model = new Backbone.Model({id: 1, name: 'Model'});
420     model.on('change:name', function(){ changed = true; });
421     model.on('change', function() {
422       var changedAttrs = model.changedAttributes();
423       assert.ok('name' in changedAttrs);
424     });
425     model.clear();
426     assert.equal(changed, true);
427     assert.equal(model.get('name'), undefined);
428   });
429
430   QUnit.test('defaults', function(assert) {
431     assert.expect(4);
432     var Defaulted = Backbone.Model.extend({
433       defaults: {
434         one: 1,
435         two: 2
436       }
437     });
438     var model = new Defaulted({two: undefined});
439     assert.equal(model.get('one'), 1);
440     assert.equal(model.get('two'), 2);
441     Defaulted = Backbone.Model.extend({
442       defaults: function() {
443         return {
444           one: 3,
445           two: 4
446         };
447       }
448     });
449     model = new Defaulted({two: undefined});
450     assert.equal(model.get('one'), 3);
451     assert.equal(model.get('two'), 4);
452   });
453
454   QUnit.test('change, hasChanged, changedAttributes, previous, previousAttributes', function(assert) {
455     assert.expect(9);
456     var model = new Backbone.Model({name: 'Tim', age: 10});
457     assert.deepEqual(model.changedAttributes(), false);
458     model.on('change', function() {
459       assert.ok(model.hasChanged('name'), 'name changed');
460       assert.ok(!model.hasChanged('age'), 'age did not');
461       assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAttributes returns the changed attrs');
462       assert.equal(model.previous('name'), 'Tim');
463       assert.ok(_.isEqual(model.previousAttributes(), {name: 'Tim', age: 10}), 'previousAttributes is correct');
464     });
465     assert.equal(model.hasChanged(), false);
466     assert.equal(model.hasChanged(undefined), false);
467     model.set({name: 'Rob'});
468     assert.equal(model.get('name'), 'Rob');
469   });
470
471   QUnit.test('changedAttributes', function(assert) {
472     assert.expect(3);
473     var model = new Backbone.Model({a: 'a', b: 'b'});
474     assert.deepEqual(model.changedAttributes(), false);
475     assert.equal(model.changedAttributes({a: 'a'}), false);
476     assert.equal(model.changedAttributes({a: 'b'}).a, 'b');
477   });
478
479   QUnit.test('change with options', function(assert) {
480     assert.expect(2);
481     var value;
482     var model = new Backbone.Model({name: 'Rob'});
483     model.on('change', function(m, options) {
484       value = options.prefix + m.get('name');
485     });
486     model.set({name: 'Bob'}, {prefix: 'Mr. '});
487     assert.equal(value, 'Mr. Bob');
488     model.set({name: 'Sue'}, {prefix: 'Ms. '});
489     assert.equal(value, 'Ms. Sue');
490   });
491
492   QUnit.test('change after initialize', function(assert) {
493     assert.expect(1);
494     var changed = 0;
495     var attrs = {id: 1, label: 'c'};
496     var obj = new Backbone.Model(attrs);
497     obj.on('change', function() { changed += 1; });
498     obj.set(attrs);
499     assert.equal(changed, 0);
500   });
501
502   QUnit.test('save within change event', function(assert) {
503     assert.expect(1);
504     var env = this;
505     var model = new Backbone.Model({firstName: 'Taylor', lastName: 'Swift'});
506     model.url = '/test';
507     model.on('change', function() {
508       model.save();
509       assert.ok(_.isEqual(env.syncArgs.model, model));
510     });
511     model.set({lastName: 'Hicks'});
512   });
513
514   QUnit.test('validate after save', function(assert) {
515     assert.expect(2);
516     var lastError, model = new Backbone.Model();
517     model.validate = function(attrs) {
518       if (attrs.admin) return "Can't change admin status.";
519     };
520     model.sync = function(method, m, options) {
521       options.success.call(this, {admin: true});
522     };
523     model.on('invalid', function(m, error) {
524       lastError = error;
525     });
526     model.save(null);
527
528     assert.equal(lastError, "Can't change admin status.");
529     assert.equal(model.validationError, "Can't change admin status.");
530   });
531
532   QUnit.test('save', function(assert) {
533     assert.expect(2);
534     doc.save({title: 'Henry V'});
535     assert.equal(this.syncArgs.method, 'update');
536     assert.ok(_.isEqual(this.syncArgs.model, doc));
537   });
538
539   QUnit.test('save, fetch, destroy triggers error event when an error occurs', function(assert) {
540     assert.expect(3);
541     var model = new Backbone.Model();
542     model.on('error', function() {
543       assert.ok(true);
544     });
545     model.sync = function(method, m, options) {
546       options.error();
547     };
548     model.save({data: 2, id: 1});
549     model.fetch();
550     model.destroy();
551   });
552
553   QUnit.test('#3283 - save, fetch, destroy calls success with context', function(assert) {
554     assert.expect(3);
555     var model = new Backbone.Model();
556     var obj = {};
557     var options = {
558       context: obj,
559       success: function() {
560         assert.equal(this, obj);
561       }
562     };
563     model.sync = function(method, m, opts) {
564       opts.success.call(opts.context);
565     };
566     model.save({data: 2, id: 1}, options);
567     model.fetch(options);
568     model.destroy(options);
569   });
570
571   QUnit.test('#3283 - save, fetch, destroy calls error with context', function(assert) {
572     assert.expect(3);
573     var model = new Backbone.Model();
574     var obj = {};
575     var options = {
576       context: obj,
577       error: function() {
578         assert.equal(this, obj);
579       }
580     };
581     model.sync = function(method, m, opts) {
582       opts.error.call(opts.context);
583     };
584     model.save({data: 2, id: 1}, options);
585     model.fetch(options);
586     model.destroy(options);
587   });
588
589   QUnit.test('#3470 - save and fetch with parse false', function(assert) {
590     assert.expect(2);
591     var i = 0;
592     var model = new Backbone.Model();
593     model.parse = function() {
594       assert.ok(false);
595     };
596     model.sync = function(method, m, options) {
597       options.success({i: ++i});
598     };
599     model.fetch({parse: false});
600     assert.equal(model.get('i'), i);
601     model.save(null, {parse: false});
602     assert.equal(model.get('i'), i);
603   });
604
605   QUnit.test('save with PATCH', function(assert) {
606     doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
607     doc.save();
608     assert.equal(this.syncArgs.method, 'update');
609     assert.equal(this.syncArgs.options.attrs, undefined);
610
611     doc.save({b: 2, d: 4}, {patch: true});
612     assert.equal(this.syncArgs.method, 'patch');
613     assert.equal(_.size(this.syncArgs.options.attrs), 2);
614     assert.equal(this.syncArgs.options.attrs.d, 4);
615     assert.equal(this.syncArgs.options.attrs.a, undefined);
616     assert.equal(this.ajaxSettings.data, '{"b":2,"d":4}');
617   });
618
619   QUnit.test('save with PATCH and different attrs', function(assert) {
620     doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}});
621     assert.equal(this.syncArgs.options.attrs.D, 3);
622     assert.equal(this.syncArgs.options.attrs.d, undefined);
623     assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}');
624     assert.deepEqual(doc.attributes, {b: 2, d: 4});
625   });
626
627   QUnit.test('save in positional style', function(assert) {
628     assert.expect(1);
629     var model = new Backbone.Model();
630     model.sync = function(method, m, options) {
631       options.success();
632     };
633     model.save('title', 'Twelfth Night');
634     assert.equal(model.get('title'), 'Twelfth Night');
635   });
636
637   QUnit.test('save with non-object success response', function(assert) {
638     assert.expect(2);
639     var model = new Backbone.Model();
640     model.sync = function(method, m, options) {
641       options.success('', options);
642       options.success(null, options);
643     };
644     model.save({testing: 'empty'}, {
645       success: function(m) {
646         assert.deepEqual(m.attributes, {testing: 'empty'});
647       }
648     });
649   });
650
651   QUnit.test('save with wait and supplied id', function(assert) {
652     var Model = Backbone.Model.extend({
653       urlRoot: '/collection'
654     });
655     var model = new Model();
656     model.save({id: 42}, {wait: true});
657     assert.equal(this.ajaxSettings.url, '/collection/42');
658   });
659
660   QUnit.test('save will pass extra options to success callback', function(assert) {
661     assert.expect(1);
662     var SpecialSyncModel = Backbone.Model.extend({
663       sync: function(method, m, options) {
664         _.extend(options, {specialSync: true});
665         return Backbone.Model.prototype.sync.call(this, method, m, options);
666       },
667       urlRoot: '/test'
668     });
669
670     var model = new SpecialSyncModel();
671
672     var onSuccess = function(m, response, options) {
673       assert.ok(options.specialSync, 'Options were passed correctly to callback');
674     };
675
676     model.save(null, {success: onSuccess});
677     this.ajaxSettings.success();
678   });
679
680   QUnit.test('fetch', function(assert) {
681     assert.expect(2);
682     doc.fetch();
683     assert.equal(this.syncArgs.method, 'read');
684     assert.ok(_.isEqual(this.syncArgs.model, doc));
685   });
686
687   QUnit.test('fetch will pass extra options to success callback', function(assert) {
688     assert.expect(1);
689     var SpecialSyncModel = Backbone.Model.extend({
690       sync: function(method, m, options) {
691         _.extend(options, {specialSync: true});
692         return Backbone.Model.prototype.sync.call(this, method, m, options);
693       },
694       urlRoot: '/test'
695     });
696
697     var model = new SpecialSyncModel();
698
699     var onSuccess = function(m, response, options) {
700       assert.ok(options.specialSync, 'Options were passed correctly to callback');
701     };
702
703     model.fetch({success: onSuccess});
704     this.ajaxSettings.success();
705   });
706
707   QUnit.test('destroy', function(assert) {
708     assert.expect(3);
709     doc.destroy();
710     assert.equal(this.syncArgs.method, 'delete');
711     assert.ok(_.isEqual(this.syncArgs.model, doc));
712
713     var newModel = new Backbone.Model;
714     assert.equal(newModel.destroy(), false);
715   });
716
717   QUnit.test('destroy will pass extra options to success callback', function(assert) {
718     assert.expect(1);
719     var SpecialSyncModel = Backbone.Model.extend({
720       sync: function(method, m, options) {
721         _.extend(options, {specialSync: true});
722         return Backbone.Model.prototype.sync.call(this, method, m, options);
723       },
724       urlRoot: '/test'
725     });
726
727     var model = new SpecialSyncModel({id: 'id'});
728
729     var onSuccess = function(m, response, options) {
730       assert.ok(options.specialSync, 'Options were passed correctly to callback');
731     };
732
733     model.destroy({success: onSuccess});
734     this.ajaxSettings.success();
735   });
736
737   QUnit.test('non-persisted destroy', function(assert) {
738     assert.expect(1);
739     var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
740     a.sync = function() { throw 'should not be called'; };
741     a.destroy();
742     assert.ok(true, 'non-persisted model should not call sync');
743   });
744
745   QUnit.test('validate', function(assert) {
746     var lastError;
747     var model = new Backbone.Model();
748     model.validate = function(attrs) {
749       if (attrs.admin !== this.get('admin')) return "Can't change admin status.";
750     };
751     model.on('invalid', function(m, error) {
752       lastError = error;
753     });
754     var result = model.set({a: 100});
755     assert.equal(result, model);
756     assert.equal(model.get('a'), 100);
757     assert.equal(lastError, undefined);
758     result = model.set({admin: true});
759     assert.equal(model.get('admin'), true);
760     result = model.set({a: 200, admin: false}, {validate: true});
761     assert.equal(lastError, "Can't change admin status.");
762     assert.equal(result, false);
763     assert.equal(model.get('a'), 100);
764   });
765
766   QUnit.test('validate on unset and clear', function(assert) {
767     assert.expect(6);
768     var error;
769     var model = new Backbone.Model({name: 'One'});
770     model.validate = function(attrs) {
771       if (!attrs.name) {
772         error = true;
773         return 'No thanks.';
774       }
775     };
776     model.set({name: 'Two'});
777     assert.equal(model.get('name'), 'Two');
778     assert.equal(error, undefined);
779     model.unset('name', {validate: true});
780     assert.equal(error, true);
781     assert.equal(model.get('name'), 'Two');
782     model.clear({validate: true});
783     assert.equal(model.get('name'), 'Two');
784     delete model.validate;
785     model.clear();
786     assert.equal(model.get('name'), undefined);
787   });
788
789   QUnit.test('validate with error callback', function(assert) {
790     assert.expect(8);
791     var lastError, boundError;
792     var model = new Backbone.Model();
793     model.validate = function(attrs) {
794       if (attrs.admin) return "Can't change admin status.";
795     };
796     model.on('invalid', function(m, error) {
797       boundError = true;
798     });
799     var result = model.set({a: 100}, {validate: true});
800     assert.equal(result, model);
801     assert.equal(model.get('a'), 100);
802     assert.equal(model.validationError, null);
803     assert.equal(boundError, undefined);
804     result = model.set({a: 200, admin: true}, {validate: true});
805     assert.equal(result, false);
806     assert.equal(model.get('a'), 100);
807     assert.equal(model.validationError, "Can't change admin status.");
808     assert.equal(boundError, true);
809   });
810
811   QUnit.test('defaults always extend attrs (#459)', function(assert) {
812     assert.expect(2);
813     var Defaulted = Backbone.Model.extend({
814       defaults: {one: 1},
815       initialize: function(attrs, opts) {
816         assert.equal(this.attributes.one, 1);
817       }
818     });
819     var providedattrs = new Defaulted({});
820     var emptyattrs = new Defaulted();
821   });
822
823   QUnit.test('Inherit class properties', function(assert) {
824     assert.expect(6);
825     var Parent = Backbone.Model.extend({
826       instancePropSame: function() {},
827       instancePropDiff: function() {}
828     }, {
829       classProp: function() {}
830     });
831     var Child = Parent.extend({
832       instancePropDiff: function() {}
833     });
834
835     var adult = new Parent;
836     var kid   = new Child;
837
838     assert.equal(Child.classProp, Parent.classProp);
839     assert.notEqual(Child.classProp, undefined);
840
841     assert.equal(kid.instancePropSame, adult.instancePropSame);
842     assert.notEqual(kid.instancePropSame, undefined);
843
844     assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
845     assert.notEqual(Child.prototype.instancePropDiff, undefined);
846   });
847
848   QUnit.test("Nested change events don't clobber previous attributes", function(assert) {
849     assert.expect(4);
850     new Backbone.Model()
851     .on('change:state', function(m, newState) {
852       assert.equal(m.previous('state'), undefined);
853       assert.equal(newState, 'hello');
854       // Fire a nested change event.
855       m.set({other: 'whatever'});
856     })
857     .on('change:state', function(m, newState) {
858       assert.equal(m.previous('state'), undefined);
859       assert.equal(newState, 'hello');
860     })
861     .set({state: 'hello'});
862   });
863
864   QUnit.test('hasChanged/set should use same comparison', function(assert) {
865     assert.expect(2);
866     var changed = 0, model = new Backbone.Model({a: null});
867     model.on('change', function() {
868       assert.ok(this.hasChanged('a'));
869     })
870     .on('change:a', function() {
871       changed++;
872     })
873     .set({a: undefined});
874     assert.equal(changed, 1);
875   });
876
877   QUnit.test('#582, #425, change:attribute callbacks should fire after all changes have occurred', function(assert) {
878     assert.expect(9);
879     var model = new Backbone.Model;
880
881     var assertion = function() {
882       assert.equal(model.get('a'), 'a');
883       assert.equal(model.get('b'), 'b');
884       assert.equal(model.get('c'), 'c');
885     };
886
887     model.on('change:a', assertion);
888     model.on('change:b', assertion);
889     model.on('change:c', assertion);
890
891     model.set({a: 'a', b: 'b', c: 'c'});
892   });
893
894   QUnit.test('#871, set with attributes property', function(assert) {
895     assert.expect(1);
896     var model = new Backbone.Model();
897     model.set({attributes: true});
898     assert.ok(model.has('attributes'));
899   });
900
901   QUnit.test('set value regardless of equality/change', function(assert) {
902     assert.expect(1);
903     var model = new Backbone.Model({x: []});
904     var a = [];
905     model.set({x: a});
906     assert.ok(model.get('x') === a);
907   });
908
909   QUnit.test('set same value does not trigger change', function(assert) {
910     assert.expect(0);
911     var model = new Backbone.Model({x: 1});
912     model.on('change change:x', function() { assert.ok(false); });
913     model.set({x: 1});
914     model.set({x: 1});
915   });
916
917   QUnit.test('unset does not fire a change for undefined attributes', function(assert) {
918     assert.expect(0);
919     var model = new Backbone.Model({x: undefined});
920     model.on('change:x', function(){ assert.ok(false); });
921     model.unset('x');
922   });
923
924   QUnit.test('set: undefined values', function(assert) {
925     assert.expect(1);
926     var model = new Backbone.Model({x: undefined});
927     assert.ok('x' in model.attributes);
928   });
929
930   QUnit.test('hasChanged works outside of change events, and true within', function(assert) {
931     assert.expect(6);
932     var model = new Backbone.Model({x: 1});
933     model.on('change:x', function() {
934       assert.ok(model.hasChanged('x'));
935       assert.equal(model.get('x'), 1);
936     });
937     model.set({x: 2}, {silent: true});
938     assert.ok(model.hasChanged());
939     assert.equal(model.hasChanged('x'), true);
940     model.set({x: 1});
941     assert.ok(model.hasChanged());
942     assert.equal(model.hasChanged('x'), true);
943   });
944
945   QUnit.test('hasChanged gets cleared on the following set', function(assert) {
946     assert.expect(4);
947     var model = new Backbone.Model;
948     model.set({x: 1});
949     assert.ok(model.hasChanged());
950     model.set({x: 1});
951     assert.ok(!model.hasChanged());
952     model.set({x: 2});
953     assert.ok(model.hasChanged());
954     model.set({});
955     assert.ok(!model.hasChanged());
956   });
957
958   QUnit.test('save with `wait` succeeds without `validate`', function(assert) {
959     assert.expect(1);
960     var model = new Backbone.Model();
961     model.url = '/test';
962     model.save({x: 1}, {wait: true});
963     assert.ok(this.syncArgs.model === model);
964   });
965
966   QUnit.test("save without `wait` doesn't set invalid attributes", function(assert) {
967     var model = new Backbone.Model();
968     model.validate = function() { return 1; };
969     model.save({a: 1});
970     assert.equal(model.get('a'), void 0);
971   });
972
973   QUnit.test("save doesn't validate twice", function(assert) {
974     var model = new Backbone.Model();
975     var times = 0;
976     model.sync = function() {};
977     model.validate = function() { ++times; };
978     model.save({});
979     assert.equal(times, 1);
980   });
981
982   QUnit.test('`hasChanged` for falsey keys', function(assert) {
983     assert.expect(2);
984     var model = new Backbone.Model();
985     model.set({x: true}, {silent: true});
986     assert.ok(!model.hasChanged(0));
987     assert.ok(!model.hasChanged(''));
988   });
989
990   QUnit.test('`previous` for falsey keys', function(assert) {
991     assert.expect(2);
992     var model = new Backbone.Model({0: true, '': true});
993     model.set({0: false, '': false}, {silent: true});
994     assert.equal(model.previous(0), true);
995     assert.equal(model.previous(''), true);
996   });
997
998   QUnit.test('`save` with `wait` sends correct attributes', function(assert) {
999     assert.expect(5);
1000     var changed = 0;
1001     var model = new Backbone.Model({x: 1, y: 2});
1002     model.url = '/test';
1003     model.on('change:x', function() { changed++; });
1004     model.save({x: 3}, {wait: true});
1005     assert.deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
1006     assert.equal(model.get('x'), 1);
1007     assert.equal(changed, 0);
1008     this.syncArgs.options.success({});
1009     assert.equal(model.get('x'), 3);
1010     assert.equal(changed, 1);
1011   });
1012
1013   QUnit.test("a failed `save` with `wait` doesn't leave attributes behind", function(assert) {
1014     assert.expect(1);
1015     var model = new Backbone.Model;
1016     model.url = '/test';
1017     model.save({x: 1}, {wait: true});
1018     assert.equal(model.get('x'), void 0);
1019   });
1020
1021   QUnit.test('#1030 - `save` with `wait` results in correct attributes if success is called during sync', function(assert) {
1022     assert.expect(2);
1023     var model = new Backbone.Model({x: 1, y: 2});
1024     model.sync = function(method, m, options) {
1025       options.success();
1026     };
1027     model.on('change:x', function() { assert.ok(true); });
1028     model.save({x: 3}, {wait: true});
1029     assert.equal(model.get('x'), 3);
1030   });
1031
1032   QUnit.test('save with wait validates attributes', function(assert) {
1033     var model = new Backbone.Model();
1034     model.url = '/test';
1035     model.validate = function() { assert.ok(true); };
1036     model.save({x: 1}, {wait: true});
1037   });
1038
1039   QUnit.test('save turns on parse flag', function(assert) {
1040     var Model = Backbone.Model.extend({
1041       sync: function(method, m, options) { assert.ok(options.parse); }
1042     });
1043     new Model().save();
1044   });
1045
1046   QUnit.test("nested `set` during `'change:attr'`", function(assert) {
1047     assert.expect(2);
1048     var events = [];
1049     var model = new Backbone.Model();
1050     model.on('all', function(event) { events.push(event); });
1051     model.on('change', function() {
1052       model.set({z: true}, {silent: true});
1053     });
1054     model.on('change:x', function() {
1055       model.set({y: true});
1056     });
1057     model.set({x: true});
1058     assert.deepEqual(events, ['change:y', 'change:x', 'change']);
1059     events = [];
1060     model.set({z: true});
1061     assert.deepEqual(events, []);
1062   });
1063
1064   QUnit.test('nested `change` only fires once', function(assert) {
1065     assert.expect(1);
1066     var model = new Backbone.Model();
1067     model.on('change', function() {
1068       assert.ok(true);
1069       model.set({x: true});
1070     });
1071     model.set({x: true});
1072   });
1073
1074   QUnit.test("nested `set` during `'change'`", function(assert) {
1075     assert.expect(6);
1076     var count = 0;
1077     var model = new Backbone.Model();
1078     model.on('change', function() {
1079       switch (count++) {
1080         case 0:
1081           assert.deepEqual(this.changedAttributes(), {x: true});
1082           assert.equal(model.previous('x'), undefined);
1083           model.set({y: true});
1084           break;
1085         case 1:
1086           assert.deepEqual(this.changedAttributes(), {x: true, y: true});
1087           assert.equal(model.previous('x'), undefined);
1088           model.set({z: true});
1089           break;
1090         case 2:
1091           assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
1092           assert.equal(model.previous('y'), undefined);
1093           break;
1094         default:
1095           assert.ok(false);
1096       }
1097     });
1098     model.set({x: true});
1099   });
1100
1101   QUnit.test('nested `change` with silent', function(assert) {
1102     assert.expect(3);
1103     var count = 0;
1104     var model = new Backbone.Model();
1105     model.on('change:y', function() { assert.ok(false); });
1106     model.on('change', function() {
1107       switch (count++) {
1108         case 0:
1109           assert.deepEqual(this.changedAttributes(), {x: true});
1110           model.set({y: true}, {silent: true});
1111           model.set({z: true});
1112           break;
1113         case 1:
1114           assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
1115           break;
1116         case 2:
1117           assert.deepEqual(this.changedAttributes(), {z: false});
1118           break;
1119         default:
1120           assert.ok(false);
1121       }
1122     });
1123     model.set({x: true});
1124     model.set({z: false});
1125   });
1126
1127   QUnit.test('nested `change:attr` with silent', function(assert) {
1128     assert.expect(0);
1129     var model = new Backbone.Model();
1130     model.on('change:y', function(){ assert.ok(false); });
1131     model.on('change', function() {
1132       model.set({y: true}, {silent: true});
1133       model.set({z: true});
1134     });
1135     model.set({x: true});
1136   });
1137
1138   QUnit.test('multiple nested changes with silent', function(assert) {
1139     assert.expect(1);
1140     var model = new Backbone.Model();
1141     model.on('change:x', function() {
1142       model.set({y: 1}, {silent: true});
1143       model.set({y: 2});
1144     });
1145     model.on('change:y', function(m, val) {
1146       assert.equal(val, 2);
1147     });
1148     model.set({x: true});
1149   });
1150
1151   QUnit.test('multiple nested changes with silent', function(assert) {
1152     assert.expect(1);
1153     var changes = [];
1154     var model = new Backbone.Model();
1155     model.on('change:b', function(m, val) { changes.push(val); });
1156     model.on('change', function() {
1157       model.set({b: 1});
1158     });
1159     model.set({b: 0});
1160     assert.deepEqual(changes, [0, 1]);
1161   });
1162
1163   QUnit.test('basic silent change semantics', function(assert) {
1164     assert.expect(1);
1165     var model = new Backbone.Model;
1166     model.set({x: 1});
1167     model.on('change', function(){ assert.ok(true); });
1168     model.set({x: 2}, {silent: true});
1169     model.set({x: 1});
1170   });
1171
1172   QUnit.test('nested set multiple times', function(assert) {
1173     assert.expect(1);
1174     var model = new Backbone.Model();
1175     model.on('change:b', function() {
1176       assert.ok(true);
1177     });
1178     model.on('change:a', function() {
1179       model.set({b: true});
1180       model.set({b: true});
1181     });
1182     model.set({a: true});
1183   });
1184
1185   QUnit.test('#1122 - clear does not alter options.', function(assert) {
1186     assert.expect(1);
1187     var model = new Backbone.Model();
1188     var options = {};
1189     model.clear(options);
1190     assert.ok(!options.unset);
1191   });
1192
1193   QUnit.test('#1122 - unset does not alter options.', function(assert) {
1194     assert.expect(1);
1195     var model = new Backbone.Model();
1196     var options = {};
1197     model.unset('x', options);
1198     assert.ok(!options.unset);
1199   });
1200
1201   QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) {
1202     assert.expect(3);
1203     var model = new Backbone.Model();
1204     var opts = {
1205       success: function( m, resp, options ) {
1206         assert.ok(options);
1207       }
1208     };
1209     model.sync = function(method, m, options) {
1210       options.success();
1211     };
1212     model.save({id: 1}, opts);
1213     model.fetch(opts);
1214     model.destroy(opts);
1215   });
1216
1217   QUnit.test("#1412 - Trigger 'sync' event.", function(assert) {
1218     assert.expect(3);
1219     var model = new Backbone.Model({id: 1});
1220     model.sync = function(method, m, options) { options.success(); };
1221     model.on('sync', function(){ assert.ok(true); });
1222     model.fetch();
1223     model.save();
1224     model.destroy();
1225   });
1226
1227   QUnit.test('#1365 - Destroy: New models execute success callback.', function(assert) {
1228     var done = assert.async();
1229     assert.expect(2);
1230     new Backbone.Model()
1231     .on('sync', function() { assert.ok(false); })
1232     .on('destroy', function(){ assert.ok(true); })
1233     .destroy({success: function(){
1234       assert.ok(true);
1235       done();
1236     }});
1237   });
1238
1239   QUnit.test('#1433 - Save: An invalid model cannot be persisted.', function(assert) {
1240     assert.expect(1);
1241     var model = new Backbone.Model;
1242     model.validate = function(){ return 'invalid'; };
1243     model.sync = function(){ assert.ok(false); };
1244     assert.strictEqual(model.save(), false);
1245   });
1246
1247   QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) {
1248     assert.expect(1);
1249     var Model = Backbone.Model.extend({
1250       url: '/test/',
1251       sync: function(method, m, options){ options.success(); },
1252       validate: function(){ return 'invalid'; }
1253     });
1254     var model = new Model({id: 1});
1255     model.on('invalid', function(){ assert.ok(true); });
1256     model.save();
1257   });
1258
1259   QUnit.test('#1545 - `undefined` can be passed to a model constructor without coersion', function(assert) {
1260     var Model = Backbone.Model.extend({
1261       defaults: {one: 1},
1262       initialize: function(attrs, opts) {
1263         assert.equal(attrs, undefined);
1264       }
1265     });
1266     var emptyattrs = new Model();
1267     var undefinedattrs = new Model(undefined);
1268   });
1269
1270   QUnit.test('#1478 - Model `save` does not trigger change on unchanged attributes', function(assert) {
1271     var done = assert.async();
1272     assert.expect(0);
1273     var Model = Backbone.Model.extend({
1274       sync: function(method, m, options) {
1275         setTimeout(function(){
1276           options.success();
1277           done();
1278         }, 0);
1279       }
1280     });
1281     new Model({x: true})
1282     .on('change:x', function(){ assert.ok(false); })
1283     .save(null, {wait: true});
1284   });
1285
1286   QUnit.test('#1664 - Changing from one value, silently to another, back to original triggers a change.', function(assert) {
1287     assert.expect(1);
1288     var model = new Backbone.Model({x: 1});
1289     model.on('change:x', function() { assert.ok(true); });
1290     model.set({x: 2}, {silent: true});
1291     model.set({x: 3}, {silent: true});
1292     model.set({x: 1});
1293   });
1294
1295   QUnit.test('#1664 - multiple silent changes nested inside a change event', function(assert) {
1296     assert.expect(2);
1297     var changes = [];
1298     var model = new Backbone.Model();
1299     model.on('change', function() {
1300       model.set({a: 'c'}, {silent: true});
1301       model.set({b: 2}, {silent: true});
1302       model.unset('c', {silent: true});
1303     });
1304     model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
1305     model.set({a: 'a', b: 1, c: 'item'});
1306     assert.deepEqual(changes, ['a', 1, 'item']);
1307     assert.deepEqual(model.attributes, {a: 'c', b: 2});
1308   });
1309
1310   QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) {
1311     var Model = Backbone.Model.extend({
1312       parse: function() { this.has('a'); } // shouldn't throw an error
1313     });
1314     var model = new Model(null, {parse: true});
1315     assert.expect(0);
1316   });
1317
1318   QUnit.test('silent changes in last `change` event back to original triggers change', function(assert) {
1319     assert.expect(2);
1320     var changes = [];
1321     var model = new Backbone.Model();
1322     model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
1323     model.on('change', function() {
1324       model.set({a: 'c'}, {silent: true});
1325     });
1326     model.set({a: 'a'});
1327     assert.deepEqual(changes, ['a']);
1328     model.set({a: 'a'});
1329     assert.deepEqual(changes, ['a', 'a']);
1330   });
1331
1332   QUnit.test('#1943 change calculations should use _.isEqual', function(assert) {
1333     var model = new Backbone.Model({a: {key: 'value'}});
1334     model.set('a', {key: 'value'}, {silent: true});
1335     assert.equal(model.changedAttributes(), false);
1336   });
1337
1338   QUnit.test('#1964 - final `change` event is always fired, regardless of interim changes', function(assert) {
1339     assert.expect(1);
1340     var model = new Backbone.Model();
1341     model.on('change:property', function() {
1342       model.set('property', 'bar');
1343     });
1344     model.on('change', function() {
1345       assert.ok(true);
1346     });
1347     model.set('property', 'foo');
1348   });
1349
1350   QUnit.test('isValid', function(assert) {
1351     var model = new Backbone.Model({valid: true});
1352     model.validate = function(attrs) {
1353       if (!attrs.valid) return 'invalid';
1354     };
1355     assert.equal(model.isValid(), true);
1356     assert.equal(model.set({valid: false}, {validate: true}), false);
1357     assert.equal(model.isValid(), true);
1358     model.set({valid: false});
1359     assert.equal(model.isValid(), false);
1360     assert.ok(!model.set('valid', false, {validate: true}));
1361   });
1362
1363   QUnit.test('#1179 - isValid returns true in the absence of validate.', function(assert) {
1364     assert.expect(1);
1365     var model = new Backbone.Model();
1366     model.validate = null;
1367     assert.ok(model.isValid());
1368   });
1369
1370   QUnit.test('#1961 - Creating a model with {validate:true} will call validate and use the error callback', function(assert) {
1371     var Model = Backbone.Model.extend({
1372       validate: function(attrs) {
1373         if (attrs.id === 1) return "This shouldn't happen";
1374       }
1375     });
1376     var model = new Model({id: 1}, {validate: true});
1377     assert.equal(model.validationError, "This shouldn't happen");
1378   });
1379
1380   QUnit.test('toJSON receives attrs during save(..., {wait: true})', function(assert) {
1381     assert.expect(1);
1382     var Model = Backbone.Model.extend({
1383       url: '/test',
1384       toJSON: function() {
1385         assert.strictEqual(this.attributes.x, 1);
1386         return _.clone(this.attributes);
1387       }
1388     });
1389     var model = new Model;
1390     model.save({x: 1}, {wait: true});
1391   });
1392
1393   QUnit.test('#2034 - nested set with silent only triggers one change', function(assert) {
1394     assert.expect(1);
1395     var model = new Backbone.Model();
1396     model.on('change', function() {
1397       model.set({b: true}, {silent: true});
1398       assert.ok(true);
1399     });
1400     model.set({a: true});
1401   });
1402
1403   QUnit.test('#3778 - id will only be updated if it is set', function(assert) {
1404     assert.expect(2);
1405     var model = new Backbone.Model({id: 1});
1406     model.id = 2;
1407     model.set({foo: 'bar'});
1408     assert.equal(model.id, 2);
1409     model.set({id: 3});
1410     assert.equal(model.id, 3);
1411   });
1412
1413 })();