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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
describe('rows - row().child()', function() {
dt.libs({
js: ['jquery', 'datatables'],
css: ['datatables']
});
function setupChildRows(hide) {
$('#example tbody').on('click', 'td', function() {
var tr = $(this).closest('tr');
var row = $('#example')
.DataTable()
.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
if (hide) {
row.child.hide();
} else {
row.child.remove();
}
tr.removeClass('shown');
} else {
// Open this row
row.child('TEST ' + row.data()[0]).show();
tr.addClass('shown');
}
});
}
describe('Check the defaults', function() {
dt.html('basic');
it('Ensure its a function', function() {
expect(
typeof $('#example')
.DataTable()
.row().child
).toBe('function');
});
});
describe('Getter behaviour', function() {
dt.html('basic');
let table;
it('Undefined if no child', function() {
table = $('#example').DataTable({
initComplete: setupChildRows(true)
});
expect(table.row(2).child()).toBe(undefined);
});
it('Returns jQuery object if child row', function() {
$('#example tbody tr:eq(2) td:eq(0)').click();
expect(table.row(2).child() instanceof $).toBe(true);
});
it('Returns jQuery object for expected row', function() {
expect(
table
.row(2)
.child()
.text()
).toBe('TEST Ashton Cox');
});
it('Returns jQuery object for expected row if hidden', function() {
table.row(2).child.hide();
expect(
table
.row(2)
.child()
.text()
).toBe('TEST Ashton Cox');
});
it('Returns undefined if child removed', function() {
table.row(2).child.remove();
expect(table.row(2).child()).toBe(undefined);
});
});
describe('showRemove behaviour - show', function() {
dt.html('basic');
let table;
it('Returns API instance', function() {
table = $('#example').DataTable({
initComplete: setupChildRows(true)
});
expect(table.row(2).child(true) instanceof $.fn.dataTable.Api).toBe(true);
});
it('When no created child', function() {
table.row(2).child(true);
expect($('#example tbody tr').length).toBe(10);
});
it('When created child and already open', function() {
$('#example tbody tr:eq(2) td:eq(0)').click();
table.row(2).child(true);
expect($('#example tbody tr').length).toBe(11);
});
it('When created child hidden', function() {
table.row(2).child.hide();
table.row(2).child(true);
expect($('#example tbody tr').length).toBe(11);
});
});
describe('showRemove behaviour - remove', function() {
dt.html('basic');
let table;
it('Returns API instance', function() {
table = $('#example').DataTable({
initComplete: setupChildRows(true)
});
expect(table.row(2).child(false) instanceof $.fn.dataTable.Api).toBe(true);
});
it('When no created child', function() {
table.row(2).child(false);
expect($('#example tbody tr').length).toBe(10);
});
it('When created child and already open', function() {
$('#example tbody tr:eq(2) td:eq(0)').click();
table.row(2).child(false);
expect($('#example tbody tr').length).toBe(10);
});
it('When created child hidden', function() {
$('#example tbody tr:eq(2) td:eq(0)').click();
table.row(2).child.hide();
table.row(2).child(false);
expect($('#example tbody tr').length).toBe(10);
expect(table.row(2).child()).toBe(undefined);
});
});
describe('Set child behaviour', function() {
let table;
dt.html('basic');
it('Returns API instance', function() {
table = $('#example').DataTable();
expect(table.row(2).child('Fred') instanceof $.fn.dataTable.Api).toBe(true);
});
dt.html('basic');
it('Data is string', function() {
table = $('#example').DataTable();
table
.row(2)
.child('Fred')
.child(true);
expect(
table
.row(2)
.child()
.text()
).toBe('Fred');
expect($('#example tbody tr:eq(3) td:eq(0)').text()).toBe('Fred');
});
dt.html('basic');
it('Data is node', function() {
table = $('#example').DataTable();
table
.row(2)
.child('<p>Fred</p>')
.child(true);
expect(
table
.row(2)
.child()
.text()
).toBe('Fred');
expect($('#example tbody tr:eq(3) p').text()).toBe('Fred');
});
dt.html('basic');
it('Data is jQuery', function() {
table = $('#example').DataTable();
table
.row(2)
.child($('<p>Fred</p>'))
.child(true);
expect(
table
.row(2)
.child()
.text()
).toBe('Fred');
expect($('#example tbody tr:eq(3) p').text()).toBe('Fred');
});
dt.html('basic');
it('Data is array', function() {
table = $('#example').DataTable();
table
.row(2)
.child(['First', 'Second', 'Third'])
.child(true);
let child = table.row(2).child();
expect(child.length).toBe(3);
expect($(child[0]).text()).toBe('First');
expect($(child[1]).text()).toBe('Second');
expect($(child[2]).text()).toBe('Third');
expect($('#example tbody tr:eq(3) td:eq(0)').text()).toBe('First');
expect($('#example tbody tr:eq(4) td:eq(0)').text()).toBe('Second');
expect($('#example tbody tr:eq(5) td:eq(0)').text()).toBe('Third');
});
dt.html('basic');
it('Data replaces existing child row if not shown', function() {
table = $('#example').DataTable();
table.row(2).child('Fred');
table.row(2).child('Stan');
expect(
table
.row(2)
.child()
.text()
).toBe('Stan');
table.row(2).child(true);
expect($('#example tbody tr:eq(3) td:eq(0)').text()).toBe('Stan');
});
dt.html('basic');
it('Data replaces existing child row if shown', function() {
table = $('#example').DataTable();
table
.row(2)
.child('Fred')
.child(true);
table.row(2).child('Stan');
expect($('#example tbody tr:eq(3) td:eq(0)').text()).toBe('Stan');
});
dt.html('basic');
it('Data can be different on different rows', function() {
table = $('#example').DataTable();
table
.row(2)
.child('Fred')
.child(true);
table
.row(3)
.child('Stan')
.child(true);
expect($('#example tbody tr:eq(3) td:eq(0)').text()).toBe('Fred');
expect($('#example tbody tr:eq(11) td:eq(0)').text()).toBe('Stan');
});
dt.html('basic');
it('Can set a class', function() {
table = $('#example').DataTable();
table
.row(2)
.child('Fred', 'Stan')
.child(true);
expect($('#example tbody tr:eq(3)').hasClass('Stan')).toBe(true);
expect($('#example tbody tr:eq(3) td:eq(0)').hasClass('Stan')).toBe(true);
});
});
});
|