Skip to content

Commit

Permalink
Tests for state object adjustments when adding/removing a model from …
Browse files Browse the repository at this point in the history
…the collection
  • Loading branch information
alanrubin committed Dec 30, 2012
1 parent 8b8bf1b commit f44b5fd
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/client-pageable.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ $(document).ready(function () {
strictEqual(col.at(0).get("name"), "a");
strictEqual(col.at(1).get("name"), "c");
strictEqual(col.at(2).get("name"), "b");
strictEqual(col.state.totalRecords, 4);
strictEqual(col.state.totalPages, 2);

var e = new Backbone.Model({name: "e"});
col.fullCollection.push(e);
Expand All @@ -151,6 +153,8 @@ $(document).ready(function () {
strictEqual(col.at(1).get("name"), "c");
strictEqual(col.at(2).get("name"), "b");
strictEqual(col.indexOf(e.cid), -1);
strictEqual(col.state.totalRecords, 5);
strictEqual(col.state.totalPages, 2);

var f = new Backbone.Model({name: "f"});
col.fullCollection.unshift(f);
Expand All @@ -159,6 +163,8 @@ $(document).ready(function () {
strictEqual(col.at(0).get("name"), "f");
strictEqual(col.at(1).get("name"), "a");
strictEqual(col.at(2).get("name"), "c");
strictEqual(col.state.totalRecords, 6);
strictEqual(col.state.totalPages, 2);
});

test("remove", 15, function () {
Expand All @@ -172,6 +178,9 @@ $(document).ready(function () {
mode: "client"
});

strictEqual(col.state.totalRecords, 3);
strictEqual(col.state.totalPages, 3);

var onRemove = function () {
ok(true);
};
Expand All @@ -181,18 +190,61 @@ $(document).ready(function () {
col.remove(col.at(0));
strictEqual(col.size(), 1);
strictEqual(col.at(0).get("name"), "c");
strictEqual(col.state.totalRecords, 2);
strictEqual(col.state.totalPages, 2);
strictEqual(col.fullCollection.size(), 2);
strictEqual(col.fullCollection.at(0).get("name"), "c");
strictEqual(col.fullCollection.at(1).get("name"), "b");

col.fullCollection.remove(col.fullCollection.at(1));
strictEqual(col.size(), 1);
strictEqual(col.at(0).get("name"), "c");
strictEqual(col.state.totalRecords, 1);
strictEqual(col.state.totalPages, 1);
strictEqual(col.fullCollection.size(), 1);

col.fullCollection.remove(col.fullCollection.at(0));
strictEqual(col.size(), 0);
strictEqual(col.fullCollection.size(), 0);
strictEqual(col.state.totalRecords, 0);
strictEqual(col.state.totalPages, 1);
});

test("remove from last page", 16, function () {

var mods = models.slice();

var col = new Backbone.PageableCollection(mods, {
state: {
pageSize: 1
},
mode: "client"
});

var onRemove = function () {
ok(true);
};
col.on("remove", onRemove);
col.fullCollection.on("remove", onRemove);

col.getPage(3);
strictEqual(col.state.currentPage, 3);

col.remove(col.at(0));
strictEqual(col.size(), 1);
strictEqual(col.at(0).get("name"), "c");
strictEqual(col.state.currentPage, 2);

col.fullCollection.remove(col.fullCollection.at(1));
strictEqual(col.size(), 1);
strictEqual(col.at(0).get("name"), "a");
strictEqual(col.state.currentPage, 1);
strictEqual(col.fullCollection.size(), 1);

col.fullCollection.remove(col.fullCollection.at(0));
strictEqual(col.size(), 0);
strictEqual(col.fullCollection.size(), 0);
strictEqual(col.state.currentPage, 1);
});

test("change", 6, function () {
Expand Down

0 comments on commit f44b5fd

Please sign in to comment.