more tests for collection, 0 is a valid id.

This commit is contained in:
Jeremy Ashkenas
2010-10-06 13:18:32 -04:00
parent 16149c7c37
commit 6009b8d1ab
2 changed files with 44 additions and 19 deletions

View File

@@ -2,18 +2,45 @@ $(document).ready(function() {
module("Backbone collections");
test("collections: simple", function() {
a = new Backbone.Model({label: 'a'});
b = new Backbone.Model({label: 'b'});
c = new Backbone.Model({label: 'c'});
d = new Backbone.Model({label: 'd'});
col = new Backbone.Collection([a,b,c,d]);
var a = new Backbone.Model({id: 4, label: 'a'});
var b = new Backbone.Model({id: 3, label: 'b'});
var c = new Backbone.Model({id: 2, label: 'c'});
var d = new Backbone.Model({id: 1, label: 'd'});
var col = new Backbone.Collection([a,b,c,d]);
test("collections: simple and sorted", function() {
equals(col.first(), a, "a should be first");
equals(col.last(), d, "d should be last");
col.comparator = function(model) { return model.id; };
col.sort();
equals(col.first(), d, "d should be first");
equals(col.last(), a, "a should be last");
equals(col.length, 4);
});
test("collections: sorted", function() {
test("collections: get, getByCid", function() {
equals(col.get(1), d);
equals(col.get(3), b);
equals(col.getByCid(col.first().cid), col.first());
});
test("collections: getIds, getCids", function() {
equals(col.getIds().sort().join(' '), '1 2 3 4');
equals(col.getCids().sort().join(' '), 'c1 c2 c3 c4');
});
test("collections: at", function() {
equals(col.at(2), b);
});
test("collections: add", function() {
var added = null;
col.bind('add', function(model){ added = model.get('label'); });
var e = new Backbone.Model({id: 0, label : 'e'});
col.add(e);
equals(added, 'e');
equals(col.length, 5);
equals(col.first(), e);
});
});