Skip to content

Commit

Permalink
Consolidate test boilerplate; use consistent names.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Aug 31, 2012
1 parent 65ed49b commit 8ef1730
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 119 deletions.
39 changes: 10 additions & 29 deletions test/collection.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
$(document).ready(function() {

var lastRequest = null;
var sync = Backbone.sync;
var ajaxParams;
var ajax = Backbone.ajax;

var a, b, c, d, e, col, otherCol;

module("Backbone.Collection", {
module("Backbone.Collection", _.extend(new Environment, {

setup: function() {
Environment.prototype.setup.apply(this, arguments);

a = new Backbone.Model({id: 3, label: 'a'});
b = new Backbone.Model({id: 2, label: 'b'});
c = new Backbone.Model({id: 1, label: 'c'});
d = new Backbone.Model({id: 0, label: 'd'});
e = null;
col = new Backbone.Collection([a,b,c,d]);
otherCol = new Backbone.Collection();

Backbone.sync = function(method, model, options) {
lastRequest = {
method: method,
model: model,
options: options
};
sync.apply(this, arguments);
};

Backbone.ajax = function(params) { ajaxParams = params; };
},

teardown: function() {
Backbone.sync = sync;
Backbone.ajax = ajax;
}

});
}));

test("Collection: new and sort", 7, function() {
equal(col.first(), a, "a should be first");
Expand Down Expand Up @@ -380,20 +361,20 @@ $(document).ready(function() {
var collection = new Backbone.Collection;
collection.url = '/test';
collection.fetch();
equal(lastRequest.method, 'read');
equal(lastRequest.model, collection);
equal(lastRequest.options.parse, true);
equal(this.syncArgs.method, 'read');
equal(this.syncArgs.model, collection);
equal(this.syncArgs.options.parse, true);

collection.fetch({parse: false});
equal(lastRequest.options.parse, false);
equal(this.syncArgs.options.parse, false);
});

test("Collection: create", 4, function() {
var collection = new Backbone.Collection;
collection.url = '/test';
var model = collection.create({label: 'f'}, {wait: true});
equal(lastRequest.method, 'create');
equal(lastRequest.model, model);
equal(this.syncArgs.method, 'create');
equal(this.syncArgs.model, model);
equal(model.get('label'), 'f');
equal(model.collection, collection);
});
Expand Down
39 changes: 39 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(function() {

var Environment = this.Environment = function(){};

_.extend(Environment.prototype, {

ajax: Backbone.ajax,

sync: Backbone.sync,

setup: function() {
var env = this;

// Capture ajax settings for comparison.
Backbone.ajax = function(settings) {
env.ajaxSettings = settings;
};

// Capture the arguments to Backbone.sync for comparison.
Backbone.sync = function(method, model, options) {
env.syncArgs = {
method: method,
model: model,
options: options
};
env.sync.apply(this, arguments);
};
},

teardown: function() {
this.syncArgs = null;
this.ajaxSettings = null;
Backbone.sync = this.sync;
Backbone.ajax = this.ajax;
}

});

})();
58 changes: 20 additions & 38 deletions test/model.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
$(document).ready(function() {

// Variable to catch the last request.
var lastRequest = null;
// Variable to catch ajax params.
var ajaxParams = null;
var sync = Backbone.sync;
var ajax = Backbone.ajax;
var urlRoot = null;

var proxy = Backbone.Model.extend();
var klass = Backbone.Collection.extend({
url : function() { return '/collection'; }
});
var doc, collection;

module("Backbone.Model", {
module("Backbone.Model", _.extend(new Environment, {

setup: function() {
Environment.prototype.setup.apply(this, arguments);
doc = new proxy({
id : '1-the-tempest',
title : "The Tempest",
Expand All @@ -25,27 +18,9 @@ $(document).ready(function() {
});
collection = new klass();
collection.add(doc);

Backbone.sync = function(method, model, options) {
lastRequest = {
method: method,
model: model,
options: options
};
sync.apply(this, arguments);
};
Backbone.ajax = function(params) { ajaxParams = params; };
urlRoot = Backbone.Model.prototype.urlRoot;
Backbone.Model.prototype.urlRoot = '/';
},

teardown: function() {
Backbone.sync = sync;
Backbone.ajax = ajax;
Backbone.Model.prototype.urlRoot = urlRoot;
}

});
}));

test("Model: initialize", 3, function() {
var Model = Backbone.Model.extend({
Expand Down Expand Up @@ -334,10 +309,12 @@ $(document).ready(function() {
});

test("Model: save within change event", 1, function () {
var env = this;
var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
model.url = '/test';
model.on('change', function () {
model.save();
ok(_.isEqual(lastRequest.model, model));
ok(_.isEqual(env.syncArgs.model, model));
});
model.set({lastName: 'Hicks'});
});
Expand Down Expand Up @@ -371,8 +348,8 @@ $(document).ready(function() {

test("Model: save", 2, function() {
doc.save({title : "Henry V"});
equal(lastRequest.method, 'update');
ok(_.isEqual(lastRequest.model, doc));
equal(this.syncArgs.method, 'update');
ok(_.isEqual(this.syncArgs.model, doc));
});

test("Model: save in positional style", 1, function() {
Expand All @@ -388,14 +365,14 @@ $(document).ready(function() {

test("Model: fetch", 2, function() {
doc.fetch();
equal(lastRequest.method, 'read');
ok(_.isEqual(lastRequest.model, doc));
equal(this.syncArgs.method, 'read');
ok(_.isEqual(this.syncArgs.model, doc));
});

test("Model: destroy", 3, function() {
doc.destroy();
equal(lastRequest.method, 'delete');
ok(_.isEqual(lastRequest.model, doc));
equal(this.syncArgs.method, 'delete');
ok(_.isEqual(this.syncArgs.model, doc));

var newModel = new Backbone.Model;
equal(newModel.destroy(), false);
Expand Down Expand Up @@ -595,8 +572,9 @@ $(document).ready(function() {

test("save with `wait` succeeds without `validate`", 1, function() {
var model = new Backbone.Model();
model.url = '/test';
model.save({x: 1}, {wait: true});
ok(lastRequest.model === model);
ok(this.syncArgs.model === model);
});

test("`hasChanged` for falsey keys", 2, function() {
Expand All @@ -616,18 +594,20 @@ $(document).ready(function() {
test("`save` with `wait` sends correct attributes", 5, function() {
var changed = 0;
var model = new Backbone.Model({x: 1, y: 2});
model.url = '/test';
model.on('change:x', function() { changed++; });
model.save({x: 3}, {wait: true});
deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
equal(model.get('x'), 1);
equal(changed, 0);
lastRequest.options.success({});
this.syncArgs.options.success({});
equal(model.get('x'), 3);
equal(changed, 1);
});

test("a failed `save` with `wait` doesn't leave attributes behind", 1, function() {
var model = new Backbone.Model;
model.url = '/test';
model.save({x: 1}, {wait: true});
equal(model.get('x'), void 0);
});
Expand All @@ -644,6 +624,7 @@ $(document).ready(function() {

test("save with wait validates attributes", 1, function() {
var model = new Backbone.Model();
model.url = '/test';
model.validate = function() { ok(true); };
model.save({x: 1}, {wait: true});
});
Expand Down Expand Up @@ -813,6 +794,7 @@ $(document).ready(function() {

test("#1412 - Trigger 'sync' event.", 3, function() {
var model = new Backbone.Model({id: 1});
model.url = '/test';
model.on('sync', function(){ ok(true); });
Backbone.ajax = function(settings){ settings.success(); };
model.fetch();
Expand Down
Loading

0 comments on commit 8ef1730

Please sign in to comment.