Index: trunk/phase3/tests/qunit/index.html |
— | — | @@ -25,6 +25,7 @@ |
26 | 26 | <script src="../../resources/jquery/jquery.makeCollapsible.js"></script> |
27 | 27 | <script src="../../resources/jquery/jquery.mwPrototypes.js"></script> |
28 | 28 | <script src="../../resources/jquery/jquery.placeholder.js"></script> |
| 29 | + <script src="../../resources/jquery/jquery.tablesorter.js"></script> |
29 | 30 | <script src="../../resources/mediawiki/mediawiki.util.js"></script> |
30 | 31 | |
31 | 32 | <!-- MW: user.options --> |
— | — | @@ -53,6 +54,7 @@ |
54 | 55 | <script src="suites/resources/jquery/jquery.autoEllipsis.js"></script> |
55 | 56 | <script src="suites/resources/jquery/jquery.colorUtil.js"></script> |
56 | 57 | <script src="suites/resources/jquery/jquery.tabIndex.js"></script> |
| 58 | + <script src="suites/resources/jquery/jquery.tablesorter.test.js"></script> |
57 | 59 | <script src="suites/resources/mediawiki/mediawiki.Title.js"></script> |
58 | 60 | |
59 | 61 | <!-- TestSwarm: If a test swarm is running this, |
Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js |
— | — | @@ -0,0 +1,166 @@ |
| 2 | +(function() { |
| 3 | + |
| 4 | +module( 'jquery.tablesorter.test.js' ); |
| 5 | + |
| 6 | +// setup hack |
| 7 | +mw.config.set('wgMonthNames', ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']); |
| 8 | +mw.config.set('wgMonthNamesShort', ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']); |
| 9 | + |
| 10 | +test( '-- Initial check', function() { |
| 11 | + expect(1); |
| 12 | + ok( $.tablesorter, '$.tablesorter defined' ); |
| 13 | +}); |
| 14 | + |
| 15 | +/** |
| 16 | + * Create an HTML table from an array of row arrays containing text strings. |
| 17 | + * First row will be header row. No fancy rowspan/colspan stuff. |
| 18 | + * |
| 19 | + * @param {String[]} header |
| 20 | + * @param {String[][]} data |
| 21 | + * @return jQuery |
| 22 | + */ |
| 23 | +var tableCreate = function( header, data ) { |
| 24 | + var $table = $('<table class="sortable"><thead></thead><tbody></tbody></table>'), |
| 25 | + $thead = $table.find('thead'), |
| 26 | + $tbody = $table.find('tbody'); |
| 27 | + var $tr = $('<tr>'); |
| 28 | + $.each(header, function(i, str) { |
| 29 | + var $th = $('<th>'); |
| 30 | + $th.text(str).appendTo($tr); |
| 31 | + }) |
| 32 | + $tr.appendTo($thead); |
| 33 | + |
| 34 | + for (var i = 0; i < data.length; i++) { |
| 35 | + $tr = $('<tr>'); |
| 36 | + $.each(data[i], function(j, str) { |
| 37 | + var $td = $('<td>'); |
| 38 | + $td.text(str).appendTo($tr); |
| 39 | + }) |
| 40 | + $tr.appendTo($tbody); |
| 41 | + } |
| 42 | + return $table; |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * Extract text from table. |
| 47 | + * |
| 48 | + * @param {jQuery} $table |
| 49 | + * @return String[][] |
| 50 | + */ |
| 51 | +var tableExtract = function( $table ) { |
| 52 | + var data = []; |
| 53 | + $table.find('tbody').find('tr').each(function(i, tr) { |
| 54 | + var row = []; |
| 55 | + $(tr).find('td,th').each(function(i, td) { |
| 56 | + row.push($(td).text()); |
| 57 | + }) |
| 58 | + data.push(row); |
| 59 | + }); |
| 60 | + return data; |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Run a table test by building a table with the given data, |
| 65 | + * running some callback on it, then checking the results. |
| 66 | + * |
| 67 | + * @param {String} msg text to pass on to qunit for the comparison |
| 68 | + * @param {String[]} header cols to make the table |
| 69 | + * @param {String[][]} data rows/cols to make the table |
| 70 | + * @param {String[][]} expected rows/cols to compare against at end |
| 71 | + * @param {function($table)} callback something to do with the table before we compare |
| 72 | + */ |
| 73 | +var tableTest = function( msg, header, data, expected, callback ) { |
| 74 | + test( msg, function() { |
| 75 | + expect(1); |
| 76 | + |
| 77 | + var $table = tableCreate( header, data ); |
| 78 | + //$('body').append($table); |
| 79 | + |
| 80 | + // Give caller a chance to set up sorting and manipulate the table. |
| 81 | + callback( $table ); |
| 82 | + |
| 83 | + // Table sorting is done asynchronously in the event loop by running |
| 84 | + // it through a setTimeout({},0); we need to do the same so we can |
| 85 | + // check our results after. |
| 86 | + stop( 1000 ); // timeout in 1s |
| 87 | + setTimeout(function() { |
| 88 | + start(); // continue the async tests... |
| 89 | + |
| 90 | + var extracted = tableExtract( $table ); |
| 91 | + deepEqual( extracted, expected, msg ) |
| 92 | + }, 150); |
| 93 | + }); |
| 94 | +}; |
| 95 | + |
| 96 | +var reversed = function(arr) { |
| 97 | + var arr2 = arr.slice(0); |
| 98 | + arr2.reverse(); |
| 99 | + return arr2; |
| 100 | +} |
| 101 | + |
| 102 | +// Sample data set: some planets! |
| 103 | +var header = ['Planet', 'Radius (km)'], |
| 104 | + mercury = ['Mercury', '2439.7'], |
| 105 | + venus = ['Venus', '6051.8'], |
| 106 | + earth = ['Earth', '6371.0'], |
| 107 | + mars = ['Mars', '3390.0'], |
| 108 | + jupiter = ['Jupiter', '69911'], |
| 109 | + saturn = ['Saturn', '58232']; |
| 110 | + |
| 111 | +// Initial data set |
| 112 | +var planets = [mercury, venus, earth, mars, jupiter, saturn]; |
| 113 | +var ascendingName = [earth, jupiter, mars, mercury, saturn, venus]; |
| 114 | +var ascendingRadius = [mercury, mars, venus, earth, saturn, jupiter]; |
| 115 | + |
| 116 | +tableTest( |
| 117 | + 'Basic planet table: ascending by name', |
| 118 | + header, |
| 119 | + planets, |
| 120 | + ascendingName, |
| 121 | + function( $table ) { |
| 122 | + $table.tablesorter(); |
| 123 | + $table.find('.headerSort:eq(0)').click(); |
| 124 | + } |
| 125 | +); |
| 126 | +tableTest( |
| 127 | + 'Basic planet table: ascending by name a second time', |
| 128 | + header, |
| 129 | + planets, |
| 130 | + ascendingName, |
| 131 | + function( $table ) { |
| 132 | + $table.tablesorter(); |
| 133 | + $table.find('.headerSort:eq(0)').click(); |
| 134 | + } |
| 135 | +); |
| 136 | +tableTest( |
| 137 | + 'Basic planet table: descending by name', |
| 138 | + header, |
| 139 | + planets, |
| 140 | + reversed(ascendingName), |
| 141 | + function( $table ) { |
| 142 | + $table.tablesorter(); |
| 143 | + $table.find('.headerSort:eq(0)').click().click(); |
| 144 | + } |
| 145 | +); |
| 146 | +tableTest( |
| 147 | + 'Basic planet table: ascending radius', |
| 148 | + header, |
| 149 | + planets, |
| 150 | + ascendingRadius, |
| 151 | + function( $table ) { |
| 152 | + $table.tablesorter(); |
| 153 | + $table.find('.headerSort:eq(1)').click(); |
| 154 | + } |
| 155 | +); |
| 156 | +tableTest( |
| 157 | + 'Basic planet table: descending radius', |
| 158 | + header, |
| 159 | + planets, |
| 160 | + reversed(ascendingRadius), |
| 161 | + function( $table ) { |
| 162 | + $table.tablesorter(); |
| 163 | + $table.find('.headerSort:eq(1)').click().click(); |
| 164 | + } |
| 165 | +); |
| 166 | + |
| 167 | +})(); |
\ No newline at end of file |
Property changes on: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 168 | + native |