summaryrefslogtreecommitdiff
path: root/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js
blob: 7ecdc4b152b4bee75fd9743f082fd8940f9be395 (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
( function () {

var config = {
	wgMonthNames: ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	wgMonthNamesShort: ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	wgDefaultDateFormat: 'dmy',
	wgContentLanguage: 'en'
};

module( 'jquery.tablesorter', QUnit.newMwEnvironment( config ) );

test( '-- Initial check', function() {
	expect(1);
	ok( $.tablesorter, '$.tablesorter defined' );
});

/**
 * Create an HTML table from an array of row arrays containing text strings.
 * First row will be header row. No fancy rowspan/colspan stuff.
 *
 * @param {String[]} header
 * @param {String[][]} data
 * @return jQuery
 */
var tableCreate = function( header, data ) {
	var $table = $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
		$thead = $table.find( 'thead' ),
		$tbody = $table.find( 'tbody' ),
		$tr = $( '<tr>' );

	$.each( header, function( i, str ) {
		var $th = $( '<th>' );
		$th.text( str ).appendTo( $tr );
	});
	$tr.appendTo( $thead );

	for (var i = 0; i < data.length; i++) {
		$tr = $( '<tr>' );
		$.each( data[i], function( j, str ) {
			var $td = $( '<td>' );
			$td.text( str ).appendTo( $tr );
		});
		$tr.appendTo( $tbody );
	}
	return $table;
};

/**
 * Extract text from table.
 *
 * @param {jQuery} $table
 * @return String[][]
 */
var tableExtract = function( $table ) {
	var data = [];

	$table.find( 'tbody' ).find( 'tr' ).each( function( i, tr ) {
		var row = [];
		$( tr ).find( 'td,th' ).each( function( i, td ) {
			row.push( $( td ).text() );
		});
		data.push( row );
	});
	return data;
};

/**
 * Run a table test by building a table with the given data,
 * running some callback on it, then checking the results.
 *
 * @param {String} msg text to pass on to qunit for the comparison
 * @param {String[]} header cols to make the table
 * @param {String[][]} data rows/cols to make the table
 * @param {String[][]} expected rows/cols to compare against at end
 * @param {function($table)} callback something to do with the table before we compare
 */
var tableTest = function( msg, header, data, expected, callback ) {
	test( msg, function() {
		expect(1);

		var $table = tableCreate( header, data );

		// Give caller a chance to set up sorting and manipulate the table.
		callback( $table );

		// Table sorting is done synchronously; if it ever needs to change back
		// to asynchronous, we'll need a timeout or a callback here.
		var extracted = tableExtract( $table );
		deepEqual( extracted, expected, msg );
	});
};

var reversed = function(arr) {
	var arr2 = arr.slice(0);
	arr2.reverse();
	return arr2;
};

// Sample data set using planets named and their radius
var header  = [ 'Planet' , 'Radius (km)'],
	mercury = [ 'Mercury', '2439.7' ],
	venus   = [ 'Venus'  , '6051.8' ],
	earth   = [ 'Earth'  , '6371.0' ],
	mars    = [ 'Mars'   , '3390.0' ],
	jupiter = [ 'Jupiter',  '69911' ],
	saturn  = [ 'Saturn' ,  '58232' ];

// Initial data set
var planets         = [mercury, venus, earth, mars, jupiter, saturn];
var ascendingName   = [earth, jupiter, mars, mercury, saturn, venus];
var ascendingRadius = [mercury, mars, venus, earth, saturn, jupiter];

tableTest(
	'Basic planet table: ascending by name',
	header,
	planets,
	ascendingName,
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);
tableTest(
	'Basic planet table: ascending by name a second time',
	header,
	planets,
	ascendingName,
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);
tableTest(
	'Basic planet table: descending by name',
	header,
	planets,
	reversed(ascendingName),
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click().click();
	}
);
tableTest(
	'Basic planet table: ascending radius',
	header,
	planets,
	ascendingRadius,
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(1)' ).click();
	}
);
tableTest(
	'Basic planet table: descending radius',
	header,
	planets,
	reversed(ascendingRadius),
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(1)' ).click().click();
	}
);


// Regression tests!
tableTest(
	'Bug 28775: German-style (dmy) short numeric dates',
	['Date'],
	[ // German-style dates are day-month-year
		['11.11.2011'],
		['01.11.2011'],
		['02.10.2011'],
		['03.08.2011'],
		['09.11.2011']
	],
	[ // Sorted by ascending date
		['03.08.2011'],
		['02.10.2011'],
		['01.11.2011'],
		['09.11.2011'],
		['11.11.2011']
	],
	function( $table ) {
		mw.config.set( 'wgDefaultDateFormat', 'dmy' );
		mw.config.set( 'wgContentLanguage', 'de' );

		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

tableTest(
	'Bug 28775: American-style (mdy) short numeric dates',
	['Date'],
	[ // American-style dates are month-day-year
		['11.11.2011'],
		['01.11.2011'],
		['02.10.2011'],
		['03.08.2011'],
		['09.11.2011']
	],
	[ // Sorted by ascending date
		['01.11.2011'],
		['02.10.2011'],
		['03.08.2011'],
		['09.11.2011'],
		['11.11.2011']
	],
	function( $table ) {
		mw.config.set( 'wgDefaultDateFormat', 'mdy' );

		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

var ipv4 = [
	// Some randomly generated fake IPs
	['45.238.27.109'],
	['44.172.9.22'],
	['247.240.82.209'],
	['204.204.132.158'],
	['170.38.91.162'],
	['197.219.164.9'],
	['45.68.154.72'],
	['182.195.149.80']
];
var ipv4Sorted = [
	// Sort order should go octet by octet
	['44.172.9.22'],
	['45.68.154.72'],
	['45.238.27.109'],
	['170.38.91.162'],
	['182.195.149.80'],
	['197.219.164.9'],
	['204.204.132.158'],
	['247.240.82.209']
];

tableTest(
	'Bug 17141: IPv4 address sorting',
	['IP'],
	ipv4,
	ipv4Sorted,
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);
tableTest(
	'Bug 17141: IPv4 address sorting (reverse)',
	['IP'],
	ipv4,
	reversed(ipv4Sorted),
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click().click();
	}
);

var umlautWords = [
	// Some words with Umlauts
	['Günther'],
	['Peter'],
	['Björn'],
	['Bjorn'],
	['Apfel'],
	['Äpfel'],
	['Strasse'],
	['Sträßschen']
];

var umlautWordsSorted = [
	// Some words with Umlauts
	['Äpfel'],
	['Apfel'],
	['Björn'],
	['Bjorn'],
	['Günther'],
	['Peter'],
	['Sträßschen'],
	['Strasse']
];

tableTest(
	'Accented Characters with custom collation',
	['Name'],
	umlautWords,
	umlautWordsSorted,
	function( $table ) {
		mw.config.set( 'tableSorterCollation', {
			'ä': 'ae',
			'ö': 'oe',
			'ß': 'ss',
			'ü':'ue'
		} );

		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

var planetsRowspan = [["Earth","6051.8"], jupiter, ["Mars","6051.8"], mercury, saturn, venus];
var planetsRowspanII = [jupiter, mercury, saturn, ['Venus', '6371.0'], venus, ['Venus', '3390.0']];

tableTest(
	'Basic planet table: same value for multiple rows via rowspan',
	header,
	planets,
	planetsRowspan,
	function( $table ) {
		// Modify the table to have a multiuple-row-spanning cell:
		// - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
		$table.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
		// - Set rowspan for 2nd cell of 3rd row to 3.
		//   This covers the removed cell in the 4th and 5th row.
		$table.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );

		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);
tableTest(
	'Basic planet table: Same value for multiple rows via rowspan II',
	header,
	planets,
	planetsRowspanII,
	function( $table ) {
		// Modify the table to have a multiuple-row-spanning cell:
		// - Remove 1st cell of 4th row, and, 1st cell or 5th row.
		$table.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
		// - Set rowspan for 1st cell of 3rd row to 3.
		//   This covers the removed cell in the 4th and 5th row.
		$table.find( 'tr:eq(2) td:eq(0)' ).prop( 'rowspan', '3' );

		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

var complexMDYDates = [
	// Some words with Umlauts
	['January, 19 2010'],
	['April 21 1991'],
	['04 22 1991'],
	['5.12.1990'],
	['December 12 \'10']
];

var complexMDYSorted = [
	["5.12.1990"],
	["April 21 1991"],
	["04 22 1991"],
	["January, 19 2010"],
	["December 12 '10"]
];

tableTest(
	'Complex date parsing I',
	['date'],
	complexMDYDates,
	complexMDYSorted,
	function( $table ) {
		mw.config.set( 'wgDefaultDateFormat', 'mdy' );

		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

var ascendingNameLegacy = ascendingName.slice(0);
ascendingNameLegacy[4] = ascendingNameLegacy[5];
ascendingNameLegacy.pop();

tableTest(
	'Legacy compat with .sortbottom',
	header,
	planets,
	ascendingNameLegacy,
	function( $table ) {
		$table.find( 'tr:last' ).addClass( 'sortbottom' );
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

/** FIXME: the diff output is not very readeable. */
test( 'bug 32047 - caption must be before thead', function() {
	var $table;
	$table = $(
		'<table class="sortable">' +
		'<caption>CAPTION</caption>' +
		'<tr><th>THEAD</th></tr>' +
		'<tr><td>A</td></tr>' +
		'<tr><td>B</td></tr>' +
		'<tr class="sortbottom"><td>TFOOT</td></tr>' +
		'</table>'
		);
	$table.tablesorter();

	equals(
		$table.children( ).get( 0 ).nodeName,
		'CAPTION',
		'First element after <thead> must be <caption> (bug 32047)'
	);
});

test( 'data-sort-value attribute, when available, should override sorting position', function() {
	var $table, data;

	// Simple example, one without data-sort-value which should be sorted at it's text.
	$table = $(
		'<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
			'<tbody>' +
			'<tr><td>Cheetah</td></tr>' +
			'<tr><td data-sort-value="Apple">Bird</td></tr>' +
			'<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
			'<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
			'<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
		'</tbody></table>'
	);
	$table.tablesorter().find( '.headerSort:eq(0)' ).click();

	data = [];
	$table.find( 'tbody > tr' ).each( function( i, tr ) {
		$( tr ).find( 'td' ).each( function( i, td ) {
			data.push( { data: $( td ).data( 'sort-value' ), text: $( td ).text() } );
		});
	});

	deepEqual( data, [
		{
			"data": "Apple",
			"text": "Bird"
		}, {
			"data": "Bananna",
			"text": "Ferret"
		}, {
			"data": undefined,
			"text": "Cheetah"
		}, {
			"data": "Cherry",
			"text": "Dolphin"
		}, {
			"data": "Drupe",
			"text": "Elephant"
		}
	] );

	// Another example
	$table = $(
		'<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
			'<tbody>' +
			'<tr><td>D</td></tr>' +
			'<tr><td data-sort-value="E">A</td></tr>' +
			'<tr><td>B</td></tr>' +
			'<tr><td>G</td></tr>' +
			'<tr><td data-sort-value="F">C</td></tr>' +
		'</tbody></table>'
	);
	$table.tablesorter().find( '.headerSort:eq(0)' ).click();

	data = [];
	$table.find( 'tbody > tr' ).each( function( i, tr ) {
		$( tr ).find( 'td' ).each( function( i, td ) {
			data.push( { data: $( td ).data( 'sort-value' ), text: $( td ).text() } );
		});
	});

	deepEqual( data, [
		{
			"data": undefined,
			"text": "B"
		}, {
			"data": undefined,
			"text": "D"
		}, {
			"data": "E",
			"text": "A"
		}, {
			"data": "F",
			"text": "C"
		}, {
			"data": undefined,
			"text": "G"
		}
	] );

});

var numbers = [
	[ '12'    ],
	[  '7'    ],
	[ '13,000'],
	[  '9'    ],
	[ '14'    ],
	[  '8.0'  ]
];
var numbersAsc = [
	[  '7'    ],
	[  '8.0'  ],
	[  '9'    ],
	[ '12'    ],
	[ '14'    ],
	[ '13,000']
];

tableTest( 'bug 8115: sort numbers with commas (ascending)',
	['Numbers'], numbers, numbersAsc,
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click();
	}
);

tableTest( 'bug 8115: sort numbers with commas (descending)',
	['Numbers'], numbers, reversed(numbersAsc),
	function( $table ) {
		$table.tablesorter();
		$table.find( '.headerSort:eq(0)' ).click().click();
	}
);
// TODO add numbers sorting tests for bug 8115 with a different language

test( 'bug 32888 - Tables inside a tableheader cell', function() {
	expect(2);

	var $table;
	$table = $(
		'<table class="sortable" id="32888">' +
		'<tr><th>header<table id="32888-2">'+
			'<tr><th>1</th><th>2</th></tr>' +
		'</table></th></tr>' +
		'<tr><td>A</td></tr>' +
		'<tr><td>B</td></tr>' +
		'</table>'
		);
	$table.tablesorter();

	equals(
		$table.find('> thead:eq(0) > tr > th.headerSort').length,
		1,
		'Child tables inside a headercell should not interfere with sortable headers (bug 32888)'
	);
	equals(
		$('#32888-2').find('th.headerSort').length,
		0,
		'The headers of child tables inside a headercell should not be sortable themselves (bug 32888)'
	);
});

})();