r90595 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90594‎ | r90595 | r90596 >
Date:19:30, 22 June 2011
Author:brion
Status:ok
Tags:
Comment:
Add initial QUnit JS test cases for jquery.tablesorter -- NOTE THERE ARE IN FACT REGRESSIONS FROM 1.17 VISIBLE HERE!

The numeric sorting doesn't work correctly, and the alphabetic sorting sometimes returns wrong results.
Modified paths:
  • /trunk/phase3/tests/qunit/index.html (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js (added) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/index.html
@@ -25,6 +25,7 @@
2626 <script src="../../resources/jquery/jquery.makeCollapsible.js"></script>
2727 <script src="../../resources/jquery/jquery.mwPrototypes.js"></script>
2828 <script src="../../resources/jquery/jquery.placeholder.js"></script>
 29+ <script src="../../resources/jquery/jquery.tablesorter.js"></script>
2930 <script src="../../resources/mediawiki/mediawiki.util.js"></script>
3031
3132 <!-- MW: user.options -->
@@ -53,6 +54,7 @@
5455 <script src="suites/resources/jquery/jquery.autoEllipsis.js"></script>
5556 <script src="suites/resources/jquery/jquery.colorUtil.js"></script>
5657 <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
 58+ <script src="suites/resources/jquery/jquery.tablesorter.test.js"></script>
5759 <script src="suites/resources/mediawiki/mediawiki.Title.js"></script>
5860
5961 <!-- 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
1168 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r90613Followup r90595: Tweak tablesorter tests so they run on r86088 version of jqu...brion21:56, 22 June 2011
r90635Followup r90595, r90626: remove async magic from table sorter tests now that ...brion23:47, 22 June 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r86088Completely rewritten table sorting script....diebuche21:47, 14 April 2011

Status & tagging log