3 module( 'jquery.tablesorter.test.js' );
6 mw
.config
.set('wgMonthNames', window
.wgMonthNames
= ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']);
7 mw
.config
.set('wgMonthNamesShort', window
.wgMonthNamesShort
= ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
9 test( '-- Initial check', function() {
11 ok( $.tablesorter
, '$.tablesorter defined' );
15 * Create an HTML table from an array of row arrays containing text strings.
16 * First row will be header row. No fancy rowspan/colspan stuff.
18 * @param {String[]} header
19 * @param {String[][]} data
22 var tableCreate = function( header
, data
) {
23 var $table
= $('<table class="sortable"><thead></thead><tbody></tbody></table>'),
24 $thead
= $table
.find('thead'),
25 $tbody
= $table
.find('tbody');
27 $.each(header
, function(i
, str
) {
29 $th
.text(str
).appendTo($tr
);
33 for (var i
= 0; i
< data
.length
; i
++) {
35 $.each(data
[i
], function(j
, str
) {
37 $td
.text(str
).appendTo($tr
);
45 * Extract text from table.
47 * @param {jQuery} $table
50 var tableExtract = function( $table
) {
52 $table
.find('tbody').find('tr').each(function(i
, tr
) {
54 $(tr
).find('td,th').each(function(i
, td
) {
55 row
.push($(td
).text());
63 * Run a table test by building a table with the given data,
64 * running some callback on it, then checking the results.
66 * @param {String} msg text to pass on to qunit for the comparison
67 * @param {String[]} header cols to make the table
68 * @param {String[][]} data rows/cols to make the table
69 * @param {String[][]} expected rows/cols to compare against at end
70 * @param {function($table)} callback something to do with the table before we compare
72 var tableTest = function( msg
, header
, data
, expected
, callback
) {
73 test( msg
, function() {
76 var $table
= tableCreate( header
, data
);
77 //$('body').append($table);
79 // Give caller a chance to set up sorting and manipulate the table.
82 // Table sorting is done synchronously; if it ever needs to change back
83 // to asynchronous, we'll need a timeout or a callback here.
84 var extracted
= tableExtract( $table
);
85 deepEqual( extracted
, expected
, msg
)
89 var reversed = function(arr
) {
90 var arr2
= arr
.slice(0);
95 // Sample data set: some planets!
96 var header
= ['Planet', 'Radius (km)'],
97 mercury
= ['Mercury', '2439.7'],
98 venus
= ['Venus', '6051.8'],
99 earth
= ['Earth', '6371.0'],
100 mars
= ['Mars', '3390.0'],
101 jupiter
= ['Jupiter', '69911'],
102 saturn
= ['Saturn', '58232'];
105 var planets
= [mercury
, venus
, earth
, mars
, jupiter
, saturn
];
106 var ascendingName
= [earth
, jupiter
, mars
, mercury
, saturn
, venus
];
107 var ascendingRadius
= [mercury
, mars
, venus
, earth
, saturn
, jupiter
];
110 'Basic planet table: ascending by name',
115 $table
.tablesorter();
116 $table
.find('.headerSort:eq(0)').click();
120 'Basic planet table: ascending by name a second time',
125 $table
.tablesorter();
126 $table
.find('.headerSort:eq(0)').click();
130 'Basic planet table: descending by name',
133 reversed(ascendingName
),
135 $table
.tablesorter();
136 $table
.find('.headerSort:eq(0)').click().click();
140 'Basic planet table: ascending radius',
145 $table
.tablesorter();
146 $table
.find('.headerSort:eq(1)').click();
150 'Basic planet table: descending radius',
153 reversed(ascendingRadius
),
155 $table
.tablesorter();
156 $table
.find('.headerSort:eq(1)').click().click();
163 'Bug 28775: German-style short numeric dates',
166 // German-style dates are day-month-year
174 // Sorted by ascending date
182 // @fixme reset it at end or change module to allow us to override it
183 mw
.config
.set('wgDefaultDateFormat', window
.wgDefaultDateFormat
= 'dmy');
184 $table
.tablesorter();
185 $table
.find('.headerSort:eq(0)').click();
189 'Bug 28775: American-style short numeric dates',
192 // American-style dates are month-day-year
200 // Sorted by ascending date
208 // @fixme reset it at end or change module to allow us to override it
209 mw
.config
.set('wgDefaultDateFormat', window
.wgDefaultDateFormat
= 'mdy');
210 $table
.tablesorter();
211 $table
.find('.headerSort:eq(0)').click();
216 // Some randomly generated fake IPs
227 // Sort order should go octet by octet
238 'Bug 17141: IPv4 address sorting',
243 $table
.tablesorter();
244 $table
.find('.headerSort:eq(0)').click();
248 'Bug 17141: IPv4 address sorting (reverse)',
251 reversed(ipv4Sorted
),
253 $table
.tablesorter();
254 $table
.find('.headerSort:eq(0)').click().click();