assets.lib.SlickGrid.tests.dataview.dataview.js Maven / Gradle / Ivy
The newest version!
(function($) {
module("basic");
function assertEmpty(dv) {
same(0, dv.getLength(), ".rows is initialized to an empty array");
same(dv.getItems().length, 0, "getItems().length");
same(undefined, dv.getIdxById("id"), "getIdxById should return undefined if not found");
same(undefined, dv.getRowById("id"), "getRowById should return undefined if not found");
same(undefined, dv.getItemById("id"), "getItemById should return undefined if not found");
same(undefined, dv.getItemByIdx(0), "getItemByIdx should return undefined if not found");
}
function assertConsistency(dv,idProperty) {
idProperty = idProperty || "id";
var items = dv.getItems(),
filteredOut = 0,
row,
id;
for (var i=0; i= args; });
same(dv.getLength(), 3, "nothing is filtered out");
assertConsistency(dv);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[0]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
same(args.previous, 3, "previous arg");
same(args.current, 1, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
same(args.pageSize, 0, "pageSize arg");
same(args.pageNum, 0, "pageNum arg");
same(args.totalRows, 1, "totalRows arg");
count++;
});
dv.setFilterArgs(2);
dv.refresh();
equal(count, 3, "events fired");
same(dv.getItems().length, 3, "original data is still there");
same(dv.getLength(), 1, "rows are filtered");
assertConsistency(dv);
});
test("re-applied on sort", function() {
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.setFilter(function(o) { return o.val === 1; });
same(dv.getLength(), 1, "one row is remaining");
dv.onRowsChanged.subscribe(function() { ok(false, "onRowsChanged called") });
dv.onRowCountChanged.subscribe(function() { ok(false, "onRowCountChanged called") });
dv.onPagingInfoChanged.subscribe(function() { ok(false, "onPagingInfoChanged called") });
dv.sort(function(x,y) { return x.val-y.val; }, false);
same(dv.getItems().length, 3, "original data is still there");
same(dv.getLength(), 1, "rows are filtered");
assertConsistency(dv);
});
test("all", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(false, "onRowsChanged called");
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
same(args.previous, 3, "previous arg");
same(args.current, 0, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
same(args.pageSize, 0, "pageSize arg");
same(args.pageNum, 0, "pageNum arg");
same(args.totalRows, 0, "totalRows arg");
count++;
});
dv.setFilter(function(o) { return false; });
equal(count, 2, "events fired");
same(dv.getItems().length, 3, "original data is still there");
same(dv.getLength(), 0, "rows are filtered");
assertConsistency(dv);
});
test("all then none", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.setFilterArgs(false);
dv.setFilter(function(o, args) { return args; });
same(dv.getLength(), 0, "all rows are filtered out");
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[0,1,2]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
same(args.previous, 0, "previous arg");
same(args.current, 3, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
same(args.pageSize, 0, "pageSize arg");
same(args.pageNum, 0, "pageNum arg");
same(args.totalRows, 3, "totalRows arg");
count++;
});
dv.setFilterArgs(true);
dv.refresh();
equal(count, 3, "events fired");
same(dv.getItems().length, 3, "original data is still there");
same(dv.getLength(), 3, "all rows are back");
assertConsistency(dv);
});
test("inlining replaces absolute returns", function() {
var dv = new Slick.Data.DataView({ inlineFilters: true });
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.setFilter(function(o) {
if (o.val === 1) { return true; }
else if (o.val === 4) { return true }
return false});
same(dv.getLength(), 1, "one row is remaining");
dv.onRowsChanged.subscribe(function() { ok(false, "onRowsChanged called") });
dv.onRowCountChanged.subscribe(function() { ok(false, "onRowCountChanged called") });
dv.onPagingInfoChanged.subscribe(function() { ok(false, "onPagingInfoChanged called") });
same(dv.getItems().length, 3, "original data is still there");
same(dv.getLength(), 1, "rows are filtered");
assertConsistency(dv);
});
test("inlining replaces evaluated returns", function() {
var dv = new Slick.Data.DataView({ inlineFilters: true });
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.setFilter(function(o) {
if (o.val === 0) { return o.id === 2; }
else if (o.val === 1) { return o.id === 2 }
return o.val === 2});
same(dv.getLength(), 1, "one row is remaining");
dv.onRowsChanged.subscribe(function() { ok(false, "onRowsChanged called") });
dv.onRowCountChanged.subscribe(function() { ok(false, "onRowCountChanged called") });
dv.onPagingInfoChanged.subscribe(function() { ok(false, "onPagingInfoChanged called") });
same(dv.getItems().length, 3, "original data is still there");
same(dv.getLength(), 1, "rows are filtered");
assertConsistency(dv);
});
module("updateItem");
test("basic", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[1]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(false, "onRowCountChanged called");
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(false, "onPagingInfoChanged called");
});
dv.updateItem(1,{id:1,val:1337});
equal(count, 1, "events fired");
same(dv.getItem(1), {id:1,val:1337}, "item updated");
assertConsistency(dv);
});
test("updating an item not passing the filter", function() {
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2},{id:3,val:1337}]);
dv.setFilter(function(o) { return o["val"] !== 1337; });
dv.onRowsChanged.subscribe(function(e,args) {
ok(false, "onRowsChanged called");
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(false, "onRowCountChanged called");
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(false, "onPagingInfoChanged called");
});
dv.updateItem(3,{id:3,val:1337});
same(dv.getItems()[3], {id:3,val:1337}, "item updated");
assertConsistency(dv);
});
test("updating an item to pass the filter", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2},{id:3,val:1337}]);
dv.setFilter(function(o) { return o["val"] !== 1337; });
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[3]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 4, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
same(args.pageSize, 0, "pageSize arg");
same(args.pageNum, 0, "pageNum arg");
same(args.totalRows, 4, "totalRows arg");
count++;
});
dv.updateItem(3,{id:3,val:3});
equal(count, 3, "events fired");
same(dv.getItems()[3], {id:3,val:3}, "item updated");
assertConsistency(dv);
});
test("updating an item to not pass the filter", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2},{id:3,val:3}]);
dv.setFilter(function(o) { return o["val"] !== 1337; });
dv.onRowsChanged.subscribe(function(e,args) {
console.log(args);
ok(false, "onRowsChanged called");
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 4, "previous arg");
equal(args.current, 3, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
same(args.pageSize, 0, "pageSize arg");
same(args.pageNum, 0, "pageNum arg");
same(args.totalRows, 3, "totalRows arg");
count++;
});
dv.updateItem(3,{id:3,val:1337});
equal(count, 2, "events fired");
same(dv.getItems()[3], {id:3,val:1337}, "item updated");
assertConsistency(dv);
});
module("addItem");
test("must have id", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
try {
dv.addItem({val:1337});
ok(false, "exception thrown");
}
catch (ex) {}
});
test("must have id (custom)", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{uid:0,val:0},{uid:1,val:1},{uid:2,val:2}], "uid");
try {
dv.addItem({id:3,val:1337});
ok(false, "exception thrown");
}
catch (ex) {}
});
test("basic", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[3]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 4, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 4, "totalRows arg");
count++;
});
dv.addItem({id:3,val:1337});
equal(count, 3, "events fired");
same(dv.getItems()[3], {id:3,val:1337}, "item updated");
same(dv.getItem(3), {id:3,val:1337}, "item updated");
assertConsistency(dv);
});
test("add an item not passing the filter", function() {
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.setFilter(function(o) { return o["val"] !== 1337; });
dv.onRowsChanged.subscribe(function(e,args) {
ok(false, "onRowsChanged called");
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(false, "onRowCountChanged called");
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(false, "onPagingInfoChanged called");
});
dv.addItem({id:3,val:1337});
same(dv.getItems()[3], {id:3,val:1337}, "item updated");
assertConsistency(dv);
});
module("insertItem");
test("must have id", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
try {
dv.insertItem(0,{val:1337});
ok(false, "exception thrown");
}
catch (ex) {}
});
test("must have id (custom)", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{uid:0,val:0},{uid:1,val:1},{uid:2,val:2}], "uid");
try {
dv.insertItem(0,{id:3,val:1337});
ok(false, "exception thrown");
}
catch (ex) {}
});
test("insert at the beginning", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[0,1,2,3]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 4, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 4, "totalRows arg");
count++;
});
dv.insertItem(0, {id:3,val:1337});
equal(count, 3, "events fired");
same(dv.getItem(0), {id:3,val:1337}, "item updated");
equal(dv.getItems().length, 4, "items updated");
equal(dv.getLength(), 4, "rows updated");
assertConsistency(dv);
});
test("insert in the middle", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[2,3]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 4, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 4, "totalRows arg");
count++;
});
dv.insertItem(2,{id:3,val:1337});
equal(count, 3, "events fired");
same(dv.getItem(2), {id:3,val:1337}, "item updated");
equal(dv.getItems().length, 4, "items updated");
equal(dv.getLength(), 4, "rows updated");
assertConsistency(dv);
});
test("insert at the end", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[3]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 4, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 4, "totalRows arg");
count++;
});
dv.insertItem(3,{id:3,val:1337});
equal(count, 3, "events fired");
same(dv.getItem(3), {id:3,val:1337}, "item updated");
equal(dv.getItems().length, 4, "items updated");
equal(dv.getLength(), 4, "rows updated");
assertConsistency(dv);
});
module("deleteItem");
test("must have id", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
try {
dv.deleteItem(-1);
ok(false, "exception thrown");
}
catch (ex) {}
try {
dv.deleteItem(undefined);
ok(false, "exception thrown");
}
catch (ex) {}
try {
dv.deleteItem(null);
ok(false, "exception thrown");
}
catch (ex) {}
try {
dv.deleteItem(3);
ok(false, "exception thrown");
}
catch (ex) {}
});
test("must have id (custom)", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{uid:0,id:-1,val:0},{uid:1,id:3,val:1},{uid:2,id:null,val:2}], "uid");
try {
dv.deleteItem(-1);
ok(false, "exception thrown");
}
catch (ex) {}
try {
dv.deleteItem(undefined);
ok(false, "exception thrown");
}
catch (ex) {}
try {
dv.deleteItem(null);
ok(false, "exception thrown");
}
catch (ex) {}
try {
dv.deleteItem(3);
ok(false, "exception thrown");
}
catch (ex) {}
});
test("delete at the beginning", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:05,val:0},{id:15,val:1},{id:25,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[0,1]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 2, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 2, "totalRows arg");
count++;
});
dv.deleteItem(05);
equal(count, 3, "events fired");
equal(dv.getItems().length, 2, "items updated");
equal(dv.getLength(), 2, "rows updated");
assertConsistency(dv);
});
test("delete in the middle", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:05,val:0},{id:15,val:1},{id:25,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(true, "onRowsChanged called");
same(args, {rows:[1]}, "args");
count++;
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 2, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 2, "totalRows arg");
count++;
});
dv.deleteItem(15);
equal(count, 3, "events fired");
equal(dv.getItems().length, 2, "items updated");
equal(dv.getLength(), 2, "rows updated");
assertConsistency(dv);
});
test("delete at the end", function() {
var count = 0;
var dv = new Slick.Data.DataView();
dv.setItems([{id:05,val:0},{id:15,val:1},{id:25,val:2}]);
dv.onRowsChanged.subscribe(function(e,args) {
ok(false, "onRowsChanged called");
});
dv.onRowCountChanged.subscribe(function(e,args) {
ok(true, "onRowCountChanged called");
equal(args.previous, 3, "previous arg");
equal(args.current, 2, "current arg");
count++;
});
dv.onPagingInfoChanged.subscribe(function(e,args) {
ok(true, "onPagingInfoChanged called");
equal(args.pageSize, 0, "pageSize arg");
equal(args.pageNum, 0, "pageNum arg");
equal(args.totalRows, 2, "totalRows arg");
count++;
});
dv.deleteItem(25);
equal(count, 2, "events fired");
equal(dv.getItems().length, 2, "items updated");
equal(dv.getLength(), 2, "rows updated");
assertConsistency(dv);
});
// TODO: paging
// TODO: combination
})(jQuery);