1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
describe('rowCallback Option', function() {
dt.libs({
js: ['jquery', 'datatables'],
css: ['datatables']
});
describe('Check the defaults', function() {
dt.html('basic');
it('Default should not be true', function() {
$('#example').DataTable();
expect($.fn.dataTable.defaults.fnRowCallback).not.toBe(true);
expect($.fn.dataTable.defaults.rowCallback).not.toBe(true);
});
dt.html('basic');
it('Expected arguments passed', function() {
let called = false;
$('#example').DataTable({
rowCallback: function() {
if (called === false) {
called = true;
expect(arguments.length).toBe(5);
expect(arguments[0] instanceof HTMLElement).toBe(true);
expect(Array.isArray(arguments[1])).toBe(true);
expect(typeof arguments[2]).toBe('number');
expect(typeof arguments[3]).toBe('number');
expect(typeof arguments[4]).toBe('number');
}
}
});
expect(called).toBe(true);
});
});
describe('Functional tests', function() {
dt.html('basic');
it('rowCallback called once for each drawn row', function() {
test = 0;
$('#example').DataTable({
pageLength: 15,
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
test++;
}
});
expect(test).toBe(15);
});
dt.html('basic');
it('rowCallback allows us to alter row information', function() {
$('#example').DataTable({
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
$(row).addClass('unit_test');
}
});
expect($('#example tbody tr:eq(1)').hasClass('unit_test')).toBe(true);
expect($('#example .unit_test').length).toBe(10);
});
dt.html('basic');
it('Data array has length matching columns', function() {
$('#example').DataTable({
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
expect(data.length).toBe(6);
}
});
});
dt.html('basic');
it('Data has expected value', function() {
let called = false;
$('#example').DataTable({
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
if (!called) {
expect(data[0]).toBe('Airi Satou');
called = true;
}
}
});
});
dt.html('basic');
it('Check the displayNum argument', function() {
let count = 0;
$('#example').DataTable({
displayStart: 10,
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
expect(displayNum).toBe(count++);
}
});
});
dt.html('basic');
it('Check the displayIndex argument', function() {
let count = 10;
$('#example').DataTable({
displayStart: 10,
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
expect(displayIndex).toBe(count++);
}
});
});
dt.html('basic');
it('Check the displayIndex argument', function() {
let called = false;
let count = 0;
let expected = [12, 8, 19, 56, 23];
$('#example').DataTable({
displayStart: 10,
pageLength: 5,
rowCallback: function(row, data, displayNum, displayIndex, dataIndex) {
if (!called) {
expect(dataIndex).toBe(expected[count++]);
}
}
});
});
});
});
|