Skip to content

Commit

Permalink
Merge pull request jashkenas#1093 from afeld/test-cleanup2
Browse files Browse the repository at this point in the history
clean up tests to use fresh object instances for each run and not set globals
  • Loading branch information
jashkenas committed Mar 12, 2012
2 parents b9c6ee9 + f499690 commit 6d85811
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 76 deletions.
52 changes: 28 additions & 24 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ $(document).ready(function() {
var lastRequest = null;
var sync = Backbone.sync;

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

module("Backbone.Collection", {

setup: function() {
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,
Expand All @@ -21,14 +31,6 @@ $(document).ready(function() {

});

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

test("Collection: new and sort", function() {
equal(col.first(), a, "a should be first");
equal(col.last(), d, "d should be last");
Expand Down Expand Up @@ -77,15 +79,16 @@ $(document).ready(function() {
});

test("Collection: at", function() {
equal(col.at(2), b);
equal(col.at(2), c);
});

test("Collection: pluck", function() {
equal(col.pluck('label').join(' '), 'd c b a');
equal(col.pluck('label').join(' '), 'a b c d');
});

test("Collection: add", function() {
var added = opts = secondAdded = null;
var added, opts, secondAdded;
added = opts = secondAdded = null;
e = new Backbone.Model({id: 10, label : 'e'});
otherCol.add(e);
otherCol.bind('add', function() {
Expand Down Expand Up @@ -206,18 +209,19 @@ $(document).ready(function() {
});

test("Collection: remove", function() {
var removed = otherRemoved = null;
var removed = null;
var otherRemoved = null;
col.bind('remove', function(model, col, options) {
removed = model.get('label');
equal(options.index, 4);
equal(options.index, 3);
});
otherCol.bind('remove', function(model, col, options) {
otherRemoved = true;
});
col.remove(e);
equal(removed, 'e');
equal(col.length, 4);
equal(col.first(), d);
col.remove(d);
equal(removed, 'd');
equal(col.length, 3);
equal(col.first(), a);
equal(otherRemoved, null);
});

Expand Down Expand Up @@ -378,14 +382,14 @@ $(document).ready(function() {
});

test("Collection: toJSON", function() {
equal(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
});

test("Collection: Underscore methods", function() {
equal(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
equal(col.any(function(model){ return model.id === 100; }), false);
equal(col.any(function(model){ return model.id === 0; }), true);
equal(col.indexOf(b), 2);
equal(col.indexOf(b), 1);
equal(col.size(), 4);
equal(col.rest().length, 3);
ok(!_.include(col.rest()), a);
Expand All @@ -398,7 +402,7 @@ $(document).ready(function() {
.filter(function(o){ return o.id % 2 === 0; })
.map(function(o){ return o.id * 2; })
.value(),
[0, 4]);
[4, 0]);
});

test("Collection: reset", function() {
Expand All @@ -412,12 +416,12 @@ $(document).ready(function() {
col.reset(models);
equal(resetCount, 2);
equal(col.length, 4);
equal(col.last(), a);
equal(col.last(), d);
col.reset(_.map(models, function(m){ return m.attributes; }));
equal(resetCount, 3);
equal(col.length, 4);
ok(col.last() !== a);
ok(_.isEqual(col.last().attributes, a.attributes));
ok(col.last() !== d);
ok(_.isEqual(col.last().attributes, d.attributes));
});

test("Collection: trigger custom events on models", function() {
Expand Down
55 changes: 23 additions & 32 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ $(document).ready(function() {
var ajax = $.ajax;
var urlRoot = null;

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

module("Backbone.Model", {

setup: function() {
doc = new proxy({
id : '1-the-tempest',
title : "The Tempest",
author : "Bill Shakespeare",
length : 123
});
collection = new klass();
collection.add(doc);

Backbone.sync = function(method, model, options) {
lastRequest = {
method: method,
Expand All @@ -32,23 +47,6 @@ $(document).ready(function() {

});

var attrs = {
id : '1-the-tempest',
title : "The Tempest",
author : "Bill Shakespeare",
length : 123
};

var proxy = Backbone.Model.extend();
var doc = new proxy(attrs);

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

var collection = new klass();
collection.add(doc);

test("Model: initialize", function() {
var Model = Backbone.Model.extend({
initialize: function() {
Expand Down Expand Up @@ -116,9 +114,8 @@ $(document).ready(function() {
});

test("Model: clone", function() {
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
a = new Backbone.Model(attrs);
b = a.clone();
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
var b = a.clone();
equal(a.get('foo'), 1);
equal(a.get('bar'), 2);
equal(a.get('baz'), 3);
Expand All @@ -131,14 +128,11 @@ $(document).ready(function() {
});

test("Model: isNew", function() {
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
a = new Backbone.Model(attrs);
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
ok(a.isNew(), "it should be new");
attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
a = new Backbone.Model(attrs);
a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 });
ok(!a.isNew(), "any defined ID is legal, negative or positive");
attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 };
a = new Backbone.Model(attrs);
a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 });
ok(!a.isNew(), "any defined ID is legal, including zero");
ok( new Backbone.Model({ }).isNew(), "is true when there is no id");
ok(!new Backbone.Model({ 'id': 2 }).isNew(), "is false for a positive integer");
Expand All @@ -163,8 +157,7 @@ $(document).ready(function() {
});

test("Model: has", function() {
attrs = {};
a = new Backbone.Model(attrs);
var a = new Backbone.Model();
equal(a.has("name"), false);
_([true, "Truth!", 1, false, '', 0]).each(function(value) {
a.set({'name': value});
Expand All @@ -180,8 +173,7 @@ $(document).ready(function() {

test("Model: set and unset", function() {
expect(8);
attrs = {id: 'id', foo: 1, bar: 2, baz: 3};
a = new Backbone.Model(attrs);
var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
var changeCount = 0;
a.on("change:foo", function() { changeCount += 1; });
a.set({'foo': 2});
Expand Down Expand Up @@ -387,8 +379,7 @@ $(document).ready(function() {
});

test("Model: non-persisted destroy", function() {
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
a = new Backbone.Model(attrs);
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
a.sync = function() { throw "should not be called"; };
a.destroy();
ok(true, "non-persisted model should not call sync");
Expand Down
2 changes: 1 addition & 1 deletion test/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$(document).ready(function() {

var router = null;
var lsatRoute = null;
var lastRoute = null;
var lastArgs = [];

function onRoute(router, route, args) {
Expand Down
29 changes: 16 additions & 13 deletions test/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ $(document).ready(function() {
var ajax = $.ajax
var lastRequest = null;

var Library = Backbone.Collection.extend({
url : function() { return '/library'; }
});
var library;

var attrs = {
title : "The Tempest",
author : "Bill Shakespeare",
length : 123
};

module("Backbone.sync", {

setup : function() {
library = new Library();
$.ajax = function(obj) {
lastRequest = obj;
};
library.create(attrs, {wait: false});
},

teardown: function() {
Expand All @@ -17,18 +30,6 @@ $(document).ready(function() {

});

var Library = Backbone.Collection.extend({
url : function() { return '/library'; }
});

var library = new Library();

var attrs = {
title : "The Tempest",
author : "Bill Shakespeare",
length : 123
};

test("sync: read", function() {
library.fetch();
equal(lastRequest.url, '/library');
Expand All @@ -45,7 +46,6 @@ $(document).ready(function() {
});

test("sync: create", function() {
library.create(attrs, {wait: false});
equal(lastRequest.url, '/library');
equal(lastRequest.type, 'POST');
equal(lastRequest.dataType, 'json');
Expand Down Expand Up @@ -108,20 +108,23 @@ $(document).ready(function() {
});

test("sync: read model", function() {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
library.first().fetch();
equal(lastRequest.url, '/library/2-the-tempest');
equal(lastRequest.type, 'GET');
ok(_.isEmpty(lastRequest.data));
});

test("sync: destroy", function() {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
library.first().destroy({wait: true});
equal(lastRequest.url, '/library/2-the-tempest');
equal(lastRequest.type, 'DELETE');
equal(lastRequest.data, null);
});

test("sync: destroy with emulateHTTP", function() {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
Backbone.emulateHTTP = Backbone.emulateJSON = true;
library.first().destroy();
equal(lastRequest.url, '/library/2-the-tempest');
Expand Down
20 changes: 14 additions & 6 deletions test/view.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
$(document).ready(function() {

module("Backbone.View");
var view;

module("Backbone.View", {

setup: function() {
view = new Backbone.View({
id : 'test-view',
className : 'test-view'
});
}

var view = new Backbone.View({
id : 'test-view',
className : 'test-view'
});

test("View: constructor", function() {
Expand Down Expand Up @@ -38,7 +44,8 @@ $(document).ready(function() {
});

test("View: delegateEvents", function() {
var counter = counter2 = 0;
var counter = 0;
var counter2 = 0;
view.setElement(document.body);
view.increment = function(){ counter++; };
view.$el.bind('click', function(){ counter2++; });
Expand Down Expand Up @@ -71,7 +78,8 @@ $(document).ready(function() {
});

test("View: undelegateEvents", function() {
var counter = counter2 = 0;
var counter = 0;
var counter2 = 0;
view.setElement(document.body);
view.increment = function(){ counter++; };
$(view.el).unbind('click');
Expand Down

0 comments on commit 6d85811

Please sign in to comment.