mirror of
https://github.com/jashkenas/backbone.git
synced 2026-01-22 21:38:00 -05:00
Merge pull request #940 from braddunbar/test-refactor
Refactor some tests.
This commit is contained in:
@@ -6,8 +6,12 @@ $(document).ready(function() {
|
||||
module("Backbone.Collection", {
|
||||
|
||||
setup: function() {
|
||||
Backbone.sync = function() {
|
||||
lastRequest = _.toArray(arguments);
|
||||
Backbone.sync = function(method, model, options) {
|
||||
lastRequest = {
|
||||
method: method,
|
||||
model: model,
|
||||
options: options
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
@@ -319,18 +323,18 @@ $(document).ready(function() {
|
||||
|
||||
test("Collection: fetch", function() {
|
||||
col.fetch();
|
||||
equal(lastRequest[0], 'read');
|
||||
equal(lastRequest[1], col);
|
||||
equal(lastRequest[2].parse, true);
|
||||
equal(lastRequest.method, 'read');
|
||||
equal(lastRequest.model, col);
|
||||
equal(lastRequest.options.parse, true);
|
||||
|
||||
col.fetch({parse: false});
|
||||
equal(lastRequest[2].parse, false);
|
||||
equal(lastRequest.options.parse, false);
|
||||
});
|
||||
|
||||
test("Collection: create", function() {
|
||||
var model = col.create({label: 'f'}, {wait: true});
|
||||
equal(lastRequest[0], 'create');
|
||||
equal(lastRequest[1], model);
|
||||
equal(lastRequest.method, 'create');
|
||||
equal(lastRequest.model, model);
|
||||
equal(model.get('label'), 'f');
|
||||
equal(model.collection, col);
|
||||
});
|
||||
|
||||
@@ -11,8 +11,12 @@ $(document).ready(function() {
|
||||
module("Backbone.Model", {
|
||||
|
||||
setup: function() {
|
||||
Backbone.sync = function() {
|
||||
lastRequest = _.toArray(arguments);
|
||||
Backbone.sync = function(method, model, options) {
|
||||
lastRequest = {
|
||||
method: method,
|
||||
model: model,
|
||||
options: options
|
||||
};
|
||||
sync.apply(this, arguments);
|
||||
};
|
||||
$.ajax = function(params) { ajaxParams = params; };
|
||||
@@ -323,7 +327,7 @@ $(document).ready(function() {
|
||||
var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
|
||||
model.on('change', function () {
|
||||
model.save();
|
||||
ok(_.isEqual(lastRequest[1], model));
|
||||
ok(_.isEqual(lastRequest.model, model));
|
||||
});
|
||||
model.set({lastName: 'Hicks'});
|
||||
});
|
||||
@@ -357,8 +361,8 @@ $(document).ready(function() {
|
||||
|
||||
test("Model: save", function() {
|
||||
doc.save({title : "Henry V"});
|
||||
equal(lastRequest[0], 'update');
|
||||
ok(_.isEqual(lastRequest[1], doc));
|
||||
equal(lastRequest.method, 'update');
|
||||
ok(_.isEqual(lastRequest.model, doc));
|
||||
});
|
||||
|
||||
test("Model: save in positional style", function() {
|
||||
@@ -372,14 +376,14 @@ $(document).ready(function() {
|
||||
|
||||
test("Model: fetch", function() {
|
||||
doc.fetch();
|
||||
equal(lastRequest[0], 'read');
|
||||
ok(_.isEqual(lastRequest[1], doc));
|
||||
equal(lastRequest.method, 'read');
|
||||
ok(_.isEqual(lastRequest.model, doc));
|
||||
});
|
||||
|
||||
test("Model: destroy", function() {
|
||||
doc.destroy();
|
||||
equal(lastRequest[0], 'delete');
|
||||
ok(_.isEqual(lastRequest[1], doc));
|
||||
equal(lastRequest.method, 'delete');
|
||||
ok(_.isEqual(lastRequest.model, doc));
|
||||
});
|
||||
|
||||
test("Model: non-persisted destroy", function() {
|
||||
@@ -598,7 +602,7 @@ $(document).ready(function() {
|
||||
test("save with `wait` succeeds without `validate`", function() {
|
||||
var model = new Backbone.Model();
|
||||
model.save({x: 1}, {wait: true});
|
||||
ok(lastRequest[1] === model);
|
||||
ok(lastRequest.model === model);
|
||||
});
|
||||
|
||||
test("`hasChanged` for falsey keys", function() {
|
||||
@@ -623,7 +627,7 @@ $(document).ready(function() {
|
||||
deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
|
||||
equal(model.get('x'), 1);
|
||||
equal(changed, 0);
|
||||
lastRequest[2].success({});
|
||||
lastRequest.options.success({});
|
||||
equal(model.get('x'), 3);
|
||||
equal(changed, 1);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
$(document).ready(function() {
|
||||
$(document).ready(function(jQuery) {
|
||||
|
||||
// a mock object that looks sufficiently jQuery-ish
|
||||
var myLib = function() {
|
||||
@@ -11,19 +11,20 @@ $(document).ready(function() {
|
||||
|
||||
var viewAttrs = { id: 'test-setdomlibrary', className: 'test-setdomlibrary' }
|
||||
|
||||
module("Backbone.setDomLibrary");
|
||||
module("Backbone.setDomLibrary", {
|
||||
|
||||
teardown: function() {
|
||||
Backbone.setDomLibrary(jQuery);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
test('Changing jQuery library to custom library', function() {
|
||||
Backbone.setDomLibrary(myLib);
|
||||
var view = new Backbone.View(viewAttrs);
|
||||
|
||||
ok(view.$el.hasClass('test-setdomlibrary') === 'spam');
|
||||
});
|
||||
|
||||
test('Changing jQuery library back to global jQuery', function() {
|
||||
Backbone.setDomLibrary(jQuery);
|
||||
var view = new Backbone.View(viewAttrs);
|
||||
|
||||
ok(view.$el.hasClass('test-setdomlibrary'));
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user