From 00af396ea7faef14d9765b0ab09a07a33845c52f Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 30 Jul 2012 16:46:50 -0700 Subject: [PATCH] (bug 37390) Clean up QUnit test suites * Use new QUnit 1.8.0+ pattern: - no-globals (only QUnit itself) - assert-object pattern - Remove pointless '-- Initial check' assertions. I introduced this pattern last year for no particular reason and everybody just copied it. There is no reason for it, it works fine without it. And the rest of the world that uses QUnit doesn't do it either. Exceptions are caught by QUnit and error handling there is much better than a dull '!ok()' assertion. * .jshintrc: - Tolerate "mulistr" (this is used in many extensions, test suites as well as in MediaWiki core) "foo\ bar" - Use "jquery: true" instead of "jQuery" so that JSHint can optimize for jQuery edge cases. Misc issues fixed: * Trailing comma's in jquery.tablesorter.test * Missing semi-colon in testrunner.js * Remove backwards-compatible line for "equals()" and "same()". These have been deprecated in QUnit since 2009 (which is way before we even started looking at QUnit). Inside QUnit itself is a deprecation notice, I don't know why I added this compatibility line, it is stupid and overrides the nice deprecation-warning that QUnit has built in for it). * Use QUnit.push inside assertTrue and assertFalse. QUnit assertion methods must not call each other, in order to keep the call stack length as expected so that QUnit can report the file and line number of the caller of the assertion in case of failure etc. Change-Id: I5cded6e8c32dba39170a02bdd3236c8b7b04d269 --- .jshintrc | 6 +- tests/qunit/data/qunitOkCall.js | 4 +- tests/qunit/data/testrunner.js | 55 ++- .../jquery/jquery.autoEllipsis.test.js | 25 +- .../jquery/jquery.byteLength.test.js | 35 +- .../resources/jquery/jquery.byteLimit.test.js | 37 +- .../resources/jquery/jquery.client.test.js | 46 +-- .../resources/jquery/jquery.colorUtil.test.js | 69 ++-- .../jquery/jquery.delayedBind.test.js | 40 +-- .../resources/jquery/jquery.getAttrs.test.js | 14 +- .../jquery/jquery.highlightText.test.js | 25 +- .../resources/jquery/jquery.localize.test.js | 43 +-- .../jquery/jquery.mwExtension.test.js | 62 ++-- .../resources/jquery/jquery.tabIndex.test.js | 25 +- .../jquery/jquery.tablesorter.test.js | 101 +++--- .../jquery/jquery.textSelection.test.js | 41 +-- .../mediawiki.api/mediawiki.api.parse.test.js | 2 +- .../mediawiki.special.recentchanges.test.js | 27 +- .../mediawiki/mediawiki.Title.test.js | 104 +++--- .../resources/mediawiki/mediawiki.Uri.test.js | 197 +++++----- .../mediawiki/mediawiki.jqueryMsg.test.js | 48 ++- .../mediawiki/mediawiki.jscompat.test.js | 40 +-- .../resources/mediawiki/mediawiki.test.js | 337 ++++++++---------- .../mediawiki/mediawiki.user.test.js | 31 +- .../mediawiki/mediawiki.util.test.js | 179 ++++------ 25 files changed, 675 insertions(+), 918 deletions(-) diff --git a/.jshintrc b/.jshintrc index 3c801c222e..5fb1173fcf 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,7 +1,7 @@ { "predef": [ "mediaWiki", - "jQuery", + "mw", "QUnit" ], @@ -17,6 +17,8 @@ "laxbreak": true, "smarttabs": true, + "multistr": true, - "browser": true + "browser": true, + "jquery": true } diff --git a/tests/qunit/data/qunitOkCall.js b/tests/qunit/data/qunitOkCall.js index 2fb6e01de7..25c42d6a3b 100644 --- a/tests/qunit/data/qunitOkCall.js +++ b/tests/qunit/data/qunitOkCall.js @@ -1,2 +1,2 @@ -start(); -ok( true, 'Successfully loaded!'); +QUnit.start(); +QUnit.assert.ok( true, 'Successfully loaded!'); diff --git a/tests/qunit/data/testrunner.js b/tests/qunit/data/testrunner.js index 57b1acb608..efa6549379 100644 --- a/tests/qunit/data/testrunner.js +++ b/tests/qunit/data/testrunner.js @@ -1,5 +1,7 @@ ( function ( $, mw, QUnit, undefined ) { -"use strict"; +/*global CompletenessTest */ +/*jshint evil:true */ +'use strict'; var mwTestIgnore, mwTester, addons; @@ -138,7 +140,7 @@ QUnit.newMwEnvironment = ( function () { mw.config.values = freshConfigCopy( localEnv.config ); mw.messages.values = freshMessagesCopy( localEnv.messages ); - localEnv.setup() + localEnv.setup(); }, teardown: function () { @@ -181,12 +183,12 @@ addons = { // Expect boolean true assertTrue: function ( actual, message ) { - strictEqual( actual, true, message ); + QUnit.push( actual === true, actual, true, message ); }, // Expect boolean false assertFalse: function ( actual, message ) { - strictEqual( actual, false, message ); + QUnit.push( actual === false, actual, false, message ); }, // Expect numerical value less than X @@ -207,22 +209,17 @@ addons = { // Expect numerical value greater than or equal to X gtOrEq: function ( actual, expected, message ) { QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message ); - }, - - // Backwards compatible with new verions of QUnit - equals: window.equal, - same: window.deepEqual + } }; -$.extend( QUnit, addons ); -$.extend( window, addons ); +$.extend( QUnit.assert, addons ); /** * Small test suite to confirm proper functionality of the utilities and * initializations in this file. */ var envExecCount = 0; -module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({ +QUnit.module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({ setup: function () { envExecCount += 1; this.mwHtmlLive = mw.html; @@ -243,33 +240,27 @@ module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({ } }) ); -test( 'Setup', function () { - expect( 3 ); - - equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' ); - equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' ); - equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' ); +QUnit.test( 'Setup', 3, function ( assert ) { + assert.equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' ); + assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' ); + assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' ); mw.config.set( 'testVar', 'bar' ); mw.messages.set( 'testMsg', 'Bar.' ); }); -test( 'Teardown', function () { - expect( 3 ); - - equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' ); - equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' ); - equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' ); +QUnit.test( 'Teardown', 3, function ( assert ) { + assert.equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' ); + assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' ); + assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' ); }); -module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() ); - -test( 'Teardown', function () { - expect( 3 ); +QUnit.module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() ); - equal( mw.html.escape( '<' ), '<', 'extra teardown() callback was ran.' ); - equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' ); - equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' ); +QUnit.test( 'Teardown', 3, function ( assert ) { + assert.equal( mw.html.escape( '<' ), '<', 'extra teardown() callback was ran.' ); + assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' ); + assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' ); }); -})( jQuery, mediaWiki, QUnit ); +}( jQuery, mediaWiki, QUnit ) ); diff --git a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js index 6e3713847f..0dee2ef0ba 100644 --- a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js @@ -1,9 +1,6 @@ -module( 'jquery.autoEllipsis', QUnit.newMwEnvironment() ); +( function ( mw, $ ) { -test( '-- Initial check', function() { - expect(1); - ok( $.fn.autoEllipsis, 'jQuery.fn.autoEllipsis defined' ); -}); +QUnit.module( 'jquery.autoEllipsis', QUnit.newMwEnvironment() ); function createWrappedDiv( text, width ) { var $wrapper = $( '
' ).css( 'width', width ); @@ -14,15 +11,13 @@ function createWrappedDiv( text, width ) { function findDivergenceIndex( a, b ) { var i = 0; - while ( i < a.length && i < b.length && a[i] == b[i] ) { + while ( i < a.length && i < b.length && a[i] === b[i] ) { i++; } return i; } -test( 'Position right', function() { - expect(4); - +QUnit.test( 'Position right', 4, function ( assert ) { // We need this thing to be visible, so append it to the DOM var origText = 'This is a really long random string and there is no way it fits in 100 pixels.'; var $wrapper = createWrappedDiv( origText, '100px' ); @@ -31,25 +26,27 @@ test( 'Position right', function() { // Verify that, and only one, span element was created var $span = $wrapper.find( '> span' ); - strictEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' ); + assert.strictEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' ); // Check that the text fits by turning on word wrapping $span.css( 'whiteSpace', 'nowrap' ); - ltOrEq( $span.width(), $span.parent().width(), "Text fits (making the span 'white-space:nowrap' does not make it wider than its parent)" ); + assert.ltOrEq( $span.width(), $span.parent().width(), "Text fits (making the span 'white-space:nowrap' does not make it wider than its parent)" ); // Add two characters using scary black magic var spanText = $span.text(); var d = findDivergenceIndex( origText, spanText ); var spanTextNew = spanText.substr( 0, d ) + origText[d] + origText[d] + '...'; - gt( spanTextNew.length, spanText.length, 'Verify that the new span-length is indeed greater' ); + assert.gt( spanTextNew.length, spanText.length, 'Verify that the new span-length is indeed greater' ); // Put this text in the span and verify it doesn't fit $span.text( spanTextNew ); // In IE6 width works like min-width, allow IE6's width to be "equal to" if ( $.browser.msie && Number( $.browser.version ) === 6 ) { - gtOrEq( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more) - IE6: Maybe equal to as well due to width behaving like min-width in IE6' ); + assert.gtOrEq( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more) - IE6: Maybe equal to as well due to width behaving like min-width in IE6' ); } else { - gt( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more)' ); + assert.gt( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more)' ); } }); + +}( mediaWiki, jQuery ) ); diff --git a/tests/qunit/suites/resources/jquery/jquery.byteLength.test.js b/tests/qunit/suites/resources/jquery/jquery.byteLength.test.js index 15fac69119..8d4ac034cc 100644 --- a/tests/qunit/suites/resources/jquery/jquery.byteLength.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.byteLength.test.js @@ -1,30 +1,21 @@ -module( 'jquery.byteLength', QUnit.newMwEnvironment() ); - -test( '-- Initial check', function() { - expect(1); - ok( $.byteLength, 'jQuery.byteLength defined' ); -} ); - -test( 'Simple text', function() { - expect(5); +QUnit.module( 'jquery.byteLength', QUnit.newMwEnvironment() ); +QUnit.test( 'Simple text', 5, function ( assert ) { var azLc = 'abcdefghijklmnopqrstuvwxyz', azUc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', num = '0123456789', x = '*', space = ' '; - equal( $.byteLength( azLc ), 26, 'Lowercase a-z' ); - equal( $.byteLength( azUc ), 26, 'Uppercase A-Z' ); - equal( $.byteLength( num ), 10, 'Numbers 0-9' ); - equal( $.byteLength( x ), 1, 'An asterisk' ); - equal( $.byteLength( space ), 3, '3 spaces' ); + assert.equal( $.byteLength( azLc ), 26, 'Lowercase a-z' ); + assert.equal( $.byteLength( azUc ), 26, 'Uppercase A-Z' ); + assert.equal( $.byteLength( num ), 10, 'Numbers 0-9' ); + assert.equal( $.byteLength( x ), 1, 'An asterisk' ); + assert.equal( $.byteLength( space ), 3, '3 spaces' ); } ); -test( 'Special text', window.foo = function() { - expect(5); - +QUnit.test( 'Special text', 5, function ( assert ) { // http://en.wikipedia.org/wiki/UTF-8 var U_0024 = '\u0024', U_00A2 = '\u00A2', @@ -34,9 +25,9 @@ test( 'Special text', window.foo = function() { // according to http://www.fileformat.info/info/unicode/char/24B62/index.htm U_024B62_alt = '\uD852\uDF62'; - strictEqual( $.byteLength( U_0024 ), 1, 'U+0024: 1 byte. \u0024 (dollar sign)' ); - strictEqual( $.byteLength( U_00A2 ), 2, 'U+00A2: 2 bytes. \u00A2 (cent sign)' ); - strictEqual( $.byteLength( U_20AC ), 3, 'U+20AC: 3 bytes. \u20AC (euro sign)' ); - strictEqual( $.byteLength( U_024B62 ), 4, 'U+024B62: 4 bytes. \uD852\uDF62 (a Han character)' ); - strictEqual( $.byteLength( U_024B62_alt ), 4, 'U+024B62: 4 bytes. \uD852\uDF62 (a Han character) - alternative method' ); + assert.strictEqual( $.byteLength( U_0024 ), 1, 'U+0024: 1 byte. \u0024 (dollar sign)' ); + assert.strictEqual( $.byteLength( U_00A2 ), 2, 'U+00A2: 2 bytes. \u00A2 (cent sign)' ); + assert.strictEqual( $.byteLength( U_20AC ), 3, 'U+20AC: 3 bytes. \u20AC (euro sign)' ); + assert.strictEqual( $.byteLength( U_024B62 ), 4, 'U+024B62: 4 bytes. \uD852\uDF62 (a Han character)' ); + assert.strictEqual( $.byteLength( U_024B62_alt ), 4, 'U+024B62: 4 bytes. \uD852\uDF62 (a Han character) - alternative method' ); } ); diff --git a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js index 2cb94d1b94..23a93a78f8 100644 --- a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js @@ -1,7 +1,7 @@ ( function ( $ ) { var simpleSample, U_20AC, mbSample; - module( 'jquery.byteLimit', QUnit.newMwEnvironment() ); + QUnit.module( 'jquery.byteLimit', QUnit.newMwEnvironment() ); // Simple sample (20 chars, 20 bytes) simpleSample = '12345678901234567890'; @@ -54,7 +54,7 @@ limit: null }, options); - test( opt.description, function () { + QUnit.test( opt.description, function ( assert ) { var rawVal, fn, newVal; opt.$input.appendTo( '#qunit-fixture' ); @@ -66,24 +66,24 @@ newVal = $.isFunction( fn ) ? fn( rawVal ) : rawVal; if ( opt.hasLimit ) { - expect(3); + QUnit.expect(3); - QUnit.ltOrEq( + assert.ltOrEq( $.byteLength( newVal ), opt.limit, 'Prevent keypresses after byteLimit was reached, length never exceeded the limit' ); - equal( + assert.equal( $.byteLength( rawVal ), $.byteLength( opt.expected ), 'Not preventing keypresses too early, length has reached the expected length' ); - equal( rawVal, opt.expected, 'New value matches the expected string' ); + assert.equal( rawVal, opt.expected, 'New value matches the expected string' ); } else { - expect(2); - equal( newVal, opt.expected, 'New value matches the expected string' ); - equal( + QUnit.expect(2); + assert.equal( newVal, opt.expected, 'New value matches the expected string' ); + assert.equal( $.byteLength( newVal ), $.byteLength( opt.expected ), 'Unlimited scenarios are not affected, expected length reached' @@ -92,11 +92,6 @@ } ); } - test( '-- Initial check', function () { - expect(1); - ok( $.fn.byteLimit, 'jQuery.fn.byteLimit defined' ); - } ); - byteLimitTest({ description: 'Plain text input', $input: $( '' ) @@ -212,18 +207,16 @@ expected: 'User:Sample' }); - test( 'Confirm properties and attributes set', function () { + QUnit.test( 'Confirm properties and attributes set', 5, function ( assert ) { var $el, $elA, $elB; - expect(5); - $el = $( '' ) .attr( 'type', 'text' ) .prop( 'maxLength', '7' ) .appendTo( '#qunit-fixture' ) .byteLimit(); - strictEqual( $el.prop( 'maxLength' ), 7, 'Pre-set maxLength property unchanged' ); + assert.strictEqual( $el.prop( 'maxLength' ), 7, 'Pre-set maxLength property unchanged' ); $el = $( '' ) .attr( 'type', 'text' ) @@ -231,7 +224,7 @@ .appendTo( '#qunit-fixture' ) .byteLimit( 12 ); - strictEqual( $el.prop( 'maxLength' ), 12, 'maxLength property updated if value was passed to $.fn.byteLimit' ); + assert.strictEqual( $el.prop( 'maxLength' ), 12, 'maxLength property updated if value was passed to $.fn.byteLimit' ); $elA = $( '' ) .addClass( 'mw-test-byteLimit-foo' ) @@ -247,7 +240,7 @@ $el = $( '.mw-test-byteLimit-foo' ); - strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' ); + assert.strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' ); $el.byteLimit(); @@ -255,8 +248,8 @@ // because $.fn.byteLimit sets: // `limit = limitArg || this.prop( 'maxLength' ); this.prop( 'maxLength', limit )` // and did so outside the each() loop. - strictEqual( $elA.prop( 'maxLength' ), 7, 'maxLength was not incorrectly set on #1 when calling byteLimit on multiple elements (bug 35294)' ); - strictEqual( $elB.prop( 'maxLength' ), 12, 'maxLength was not incorrectly set on #2 when calling byteLimit on multiple elements (bug 35294)' ); + assert.strictEqual( $elA.prop( 'maxLength' ), 7, 'maxLength was not incorrectly set on #1 when calling byteLimit on multiple elements (bug 35294)' ); + assert.strictEqual( $elB.prop( 'maxLength' ), 12, 'maxLength was not incorrectly set on #2 when calling byteLimit on multiple elements (bug 35294)' ); }); }( jQuery ) ); diff --git a/tests/qunit/suites/resources/jquery/jquery.client.test.js b/tests/qunit/suites/resources/jquery/jquery.client.test.js index 2c0b8e30e4..bf62b39ab4 100644 --- a/tests/qunit/suites/resources/jquery/jquery.client.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.client.test.js @@ -1,14 +1,9 @@ -module( 'jquery.client', QUnit.newMwEnvironment() ); - -test( '-- Initial check', function() { - expect(1); - ok( jQuery.client, 'jQuery.client defined' ); -}); +QUnit.module( 'jquery.client', QUnit.newMwEnvironment() ); /** Number of user-agent defined */ var uacount = 0; -var uas = (function() { +var uas = (function () { // Object keyed by userAgent. Value is an array (human-readable name, client-profile object, navigator.platform value) // Info based on results from http://toolserver.org/~krinkle/testswarm/job/174/ @@ -225,40 +220,39 @@ var uas = (function() { } } }; - $.each( uas, function() { uacount++ }); + $.each( uas, function () { + uacount++; + }); return uas; -})(); - -test( 'profile userAgent support', function() { - expect(uacount); +}()); +QUnit.test( 'profile userAgent support', uacount, function ( assert ) { // Generate a client profile object and compare recursively var uaTest = function( rawUserAgent, data ) { var ret = $.client.profile( { userAgent: rawUserAgent, platform: data.platform } ); - deepEqual( ret, data.profile, 'Client profile support check for ' + data.title + ' (' + data.platform + '): ' + rawUserAgent ); + assert.deepEqual( ret, data.profile, 'Client profile support check for ' + data.title + ' (' + data.platform + '): ' + rawUserAgent ); }; // Loop through and run tests $.each( uas, uaTest ); } ); -test( 'profile return validation for current user agent', function() { - expect(7); +QUnit.test( 'profile return validation for current user agent', 7, function ( assert ) { var p = $.client.profile(); - var unknownOrType = function( val, type, summary ) { - return ok( typeof val === type || val === 'unknown', summary ); - }; + function unknownOrType( val, type, summary ) { + assert.ok( typeof val === type || val === 'unknown', summary ); + } - equal( typeof p, 'object', 'profile returns an object' ); + assert.equal( typeof p, 'object', 'profile returns an object' ); unknownOrType( p.layout, 'string', 'p.layout is a string (or "unknown")' ); unknownOrType( p.layoutVersion, 'number', 'p.layoutVersion is a number (or "unknown")' ); unknownOrType( p.platform, 'string', 'p.platform is a string (or "unknown")' ); unknownOrType( p.version, 'string', 'p.version is a string (or "unknown")' ); unknownOrType( p.versionBase, 'string', 'p.versionBase is a string (or "unknown")' ); - equal( typeof p.versionNumber, 'number', 'p.versionNumber is a number' ); + assert.equal( typeof p.versionNumber, 'number', 'p.versionNumber is a number' ); }); // Example from WikiEditor @@ -289,20 +283,16 @@ var testMap = { } }; -test( 'test', function() { - expect(1); - +QUnit.test( 'test', 1, function ( assert ) { // .test() uses eval, make sure no exceptions are thrown // then do a basic return value type check var testMatch = $.client.test( testMap ); - equal( typeof testMatch, 'boolean', 'test returns a boolean value' ); + assert.equal( typeof testMatch, 'boolean', 'test returns a boolean value' ); }); -test( 'User-agent matches against WikiEditor\'s compatibility map', function() { - expect( uacount * 2 ); // double since we test both LTR and RTL - +QUnit.test( 'User-agent matches against WikiEditor\'s compatibility map', uacount * 2, function ( assert ) { var $body = $( 'body' ), bodyClasses = $body.attr( 'class' ); @@ -317,7 +307,7 @@ test( 'User-agent matches against WikiEditor\'s compatibility map', function() { var testMatch = $.client.test( testMap, profile ); $body.removeClass( dir ); - equal( testMatch, data.wikiEditor[dir], 'testing comparison based on ' + dir + ', ' + agent ); + assert.equal( testMatch, data.wikiEditor[dir], 'testing comparison based on ' + dir + ', ' + agent ); }); }); diff --git a/tests/qunit/suites/resources/jquery/jquery.colorUtil.test.js b/tests/qunit/suites/resources/jquery/jquery.colorUtil.test.js index 655ee564f9..7b37f5a059 100644 --- a/tests/qunit/suites/resources/jquery/jquery.colorUtil.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.colorUtil.test.js @@ -1,40 +1,31 @@ -module( 'jquery.colorUtil', QUnit.newMwEnvironment() ); - -test( '-- Initial check', function() { - expect(1); - ok( $.colorUtil, '$.colorUtil defined' ); -}); - -test( 'getRGB', function() { - expect(18); - - strictEqual( $.colorUtil.getRGB(), undefined, 'No arguments' ); - strictEqual( $.colorUtil.getRGB( '' ), undefined, 'Empty string' ); - deepEqual( $.colorUtil.getRGB( [0, 100, 255] ), [0, 100, 255], 'Parse array of rgb values' ); - deepEqual( $.colorUtil.getRGB( 'rgb(0,100,255)' ), [0, 100, 255], 'Parse simple rgb string' ); - deepEqual( $.colorUtil.getRGB( 'rgb(0, 100, 255)' ), [0, 100, 255], 'Parse simple rgb string with spaces' ); - deepEqual( $.colorUtil.getRGB( 'rgb(0%,20%,40%)' ), [0, 51, 102], 'Parse rgb string with percentages' ); - deepEqual( $.colorUtil.getRGB( 'rgb(0%, 20%, 40%)' ), [0, 51, 102], 'Parse rgb string with percentages and spaces' ); - deepEqual( $.colorUtil.getRGB( '#f2ddee' ), [242, 221, 238], 'Hex string: 6 char lowercase' ); - deepEqual( $.colorUtil.getRGB( '#f2DDEE' ), [242, 221, 238], 'Hex string: 6 char uppercase' ); - deepEqual( $.colorUtil.getRGB( '#f2DdEe' ), [242, 221, 238], 'Hex string: 6 char mixed' ); - deepEqual( $.colorUtil.getRGB( '#eee' ), [238, 238, 238], 'Hex string: 3 char lowercase' ); - deepEqual( $.colorUtil.getRGB( '#EEE' ), [238, 238, 238], 'Hex string: 3 char uppercase' ); - deepEqual( $.colorUtil.getRGB( '#eEe' ), [238, 238, 238], 'Hex string: 3 char mixed' ); - deepEqual( $.colorUtil.getRGB( 'rgba(0, 0, 0, 0)' ), [255, 255, 255], 'Zero rgba for Safari 3; Transparent (whitespace)' ); +QUnit.module( 'jquery.colorUtil', QUnit.newMwEnvironment() ); + +QUnit.test( 'getRGB', 18, function ( assert ) { + assert.strictEqual( $.colorUtil.getRGB(), undefined, 'No arguments' ); + assert.strictEqual( $.colorUtil.getRGB( '' ), undefined, 'Empty string' ); + assert.deepEqual( $.colorUtil.getRGB( [0, 100, 255] ), [0, 100, 255], 'Parse array of rgb values' ); + assert.deepEqual( $.colorUtil.getRGB( 'rgb(0,100,255)' ), [0, 100, 255], 'Parse simple rgb string' ); + assert.deepEqual( $.colorUtil.getRGB( 'rgb(0, 100, 255)' ), [0, 100, 255], 'Parse simple rgb string with spaces' ); + assert.deepEqual( $.colorUtil.getRGB( 'rgb(0%,20%,40%)' ), [0, 51, 102], 'Parse rgb string with percentages' ); + assert.deepEqual( $.colorUtil.getRGB( 'rgb(0%, 20%, 40%)' ), [0, 51, 102], 'Parse rgb string with percentages and spaces' ); + assert.deepEqual( $.colorUtil.getRGB( '#f2ddee' ), [242, 221, 238], 'Hex string: 6 char lowercase' ); + assert.deepEqual( $.colorUtil.getRGB( '#f2DDEE' ), [242, 221, 238], 'Hex string: 6 char uppercase' ); + assert.deepEqual( $.colorUtil.getRGB( '#f2DdEe' ), [242, 221, 238], 'Hex string: 6 char mixed' ); + assert.deepEqual( $.colorUtil.getRGB( '#eee' ), [238, 238, 238], 'Hex string: 3 char lowercase' ); + assert.deepEqual( $.colorUtil.getRGB( '#EEE' ), [238, 238, 238], 'Hex string: 3 char uppercase' ); + assert.deepEqual( $.colorUtil.getRGB( '#eEe' ), [238, 238, 238], 'Hex string: 3 char mixed' ); + assert.deepEqual( $.colorUtil.getRGB( 'rgba(0, 0, 0, 0)' ), [255, 255, 255], 'Zero rgba for Safari 3; Transparent (whitespace)' ); // Perhaps this is a bug in colorUtil, but it is the current behaviour so, let's keep // track of it, so we will know in case it would ever change. - strictEqual( $.colorUtil.getRGB( 'rgba(0,0,0,0)' ), undefined, 'Zero rgba without whitespace' ); + assert.strictEqual( $.colorUtil.getRGB( 'rgba(0,0,0,0)' ), undefined, 'Zero rgba without whitespace' ); - deepEqual( $.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (lightGreen)' ); - deepEqual( $.colorUtil.getRGB( 'transparent' ), [255, 255, 255], 'Color names (transparent)' ); - strictEqual( $.colorUtil.getRGB( 'mediaWiki' ), undefined, 'Inexisting color name' ); + assert.deepEqual( $.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (lightGreen)' ); + assert.deepEqual( $.colorUtil.getRGB( 'transparent' ), [255, 255, 255], 'Color names (transparent)' ); + assert.strictEqual( $.colorUtil.getRGB( 'mediaWiki' ), undefined, 'Inexisting color name' ); }); -test( 'rgbToHsl', function() { - expect(1); - +QUnit.test( 'rgbToHsl', 1, function ( assert ) { var hsl = $.colorUtil.rgbToHsl( 144, 238, 144 ); // Cross-browser differences in decimals... @@ -45,27 +36,23 @@ test( 'rgbToHsl', function() { // Re-create the rgbToHsl return array items, limited to two decimals. var ret = [dualDecimals(hsl[0]), dualDecimals(hsl[1]), dualDecimals(hsl[2])]; - deepEqual( ret, [0.33, 0.73, 0.75], 'rgb(144, 238, 144): hsl(0.33, 0.73, 0.75)' ); + assert.deepEqual( ret, [0.33, 0.73, 0.75], 'rgb(144, 238, 144): hsl(0.33, 0.73, 0.75)' ); }); -test( 'hslToRgb', function() { - expect(1); - +QUnit.test( 'hslToRgb', 1, function ( assert ) { var rgb = $.colorUtil.hslToRgb( 0.3, 0.7, 0.8 ); // Cross-browser differences in decimals... // Re-create the hslToRgb return array items, rounded to whole numbers. var ret = [Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2])]; - deepEqual( ret ,[183, 240, 168], 'hsl(0.3, 0.7, 0.8): rgb(183, 240, 168)' ); + assert.deepEqual( ret ,[183, 240, 168], 'hsl(0.3, 0.7, 0.8): rgb(183, 240, 168)' ); }); -test( 'getColorBrightness', function() { - expect(2); - +QUnit.test( 'getColorBrightness', 2, function ( assert ) { var a = $.colorUtil.getColorBrightness( 'red', +0.1 ); - equal( a, 'rgb(255,50,50)', 'Start with named color "red", brighten 10%' ); + assert.equal( a, 'rgb(255,50,50)', 'Start with named color "red", brighten 10%' ); var b = $.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 ); - equal( b, 'rgb(118,29,29)', 'Start with rgb string "rgb(200,50,50)", darken 20%' ); + assert.equal( b, 'rgb(118,29,29)', 'Start with rgb string "rgb(200,50,50)", darken 20%' ); }); diff --git a/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js b/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js index 6489a1f176..a307983557 100644 --- a/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js @@ -1,41 +1,35 @@ -test('jquery.delayedBind with data option', function() { +QUnit.asyncTest('jquery.delayedBind with data option', 2, function ( assert ) { var $fixture = $('
').appendTo('#qunit-fixture'), data = { magic: "beeswax" }, delay = 50; - $fixture.delayedBind(delay, 'testevent', data, function(event) { - start(); // continue! - ok(true, 'testevent fired'); - ok(event.data === data, 'data is passed through delayedBind'); + $fixture.delayedBind(delay, 'testevent', data, function ( e ) { + QUnit.start(); // continue! + assert.ok( true, 'testevent fired'); + assert.ok( e.data === data, 'data is passed through delayedBind'); }); - expect(2); - stop(); // async! - // We'll trigger it thrice, but it should only happen once. - $fixture.trigger('testevent', {}); - $fixture.trigger('testevent', {}); - $fixture.trigger('testevent', {}); - $fixture.trigger('testevent', {}); + $fixture.trigger( 'testevent', {} ); + $fixture.trigger( 'testevent', {} ); + $fixture.trigger( 'testevent', {} ); + $fixture.trigger( 'testevent', {} ); }); -test('jquery.delayedBind without data option', function() { +QUnit.asyncTest('jquery.delayedBind without data option', 1, function ( assert ) { var $fixture = $('
').appendTo('#qunit-fixture'), data = { magic: "beeswax" }, delay = 50; - $fixture.delayedBind(delay, 'testevent', function(event) { - start(); // continue! - ok(true, 'testevent fired'); + $fixture.delayedBind(delay, 'testevent', function ( e ) { + QUnit.start(); // continue! + assert.ok(true, 'testevent fired'); }); - expect(1); - stop(); // async! - // We'll trigger it thrice, but it should only happen once. - $fixture.trigger('testevent', {}); - $fixture.trigger('testevent', {}); - $fixture.trigger('testevent', {}); - $fixture.trigger('testevent', {}); + $fixture.trigger( 'testevent', {} ); + $fixture.trigger( 'testevent', {} ); + $fixture.trigger( 'testevent', {} ); + $fixture.trigger( 'testevent', {} ); }); diff --git a/tests/qunit/suites/resources/jquery/jquery.getAttrs.test.js b/tests/qunit/suites/resources/jquery/jquery.getAttrs.test.js index 9377a2f63f..6eef1abba1 100644 --- a/tests/qunit/suites/resources/jquery/jquery.getAttrs.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.getAttrs.test.js @@ -1,17 +1,11 @@ -module( 'jquery.getAttrs', QUnit.newMwEnvironment() ); +QUnit.module( 'jquery.getAttrs', QUnit.newMwEnvironment() ); -test( '-- Initial check', function() { - expect(1); - ok( $.fn.getAttrs, 'jQuery.fn.getAttrs defined' ); -} ); - -test( 'Check', function() { - expect(1); +QUnit.test( 'Check', 1, function ( assert ) { var attrs = { foo: 'bar', 'class': 'lorem' }, - $el = $( '
', attrs ); + $el = jQuery( '
', attrs ); - deepEqual( $el.getAttrs(), attrs, 'getAttrs() return object should match the attributes set, no more, no less' ); + assert.deepEqual( $el.getAttrs(), attrs, 'getAttrs() return object should match the attributes set, no more, no less' ); } ); diff --git a/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js b/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js index 4750d2b879..a94dca313a 100644 --- a/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js @@ -1,12 +1,7 @@ -module( 'jquery.highlightText', QUnit.newMwEnvironment() ); +QUnit.module( 'jquery.highlightText', QUnit.newMwEnvironment() ); -test( '-- Initial check', function() { - expect(1); - ok( $.fn.highlightText, 'jQuery.fn.highlightText defined' ); -} ); - -test( 'Check', function() { - var cases = [ +QUnit.test( 'Check', function ( assert ) { + var $fixture, cases = [ { desc: 'Test 001', text: 'Blue Öyster Cult', @@ -224,16 +219,14 @@ test( 'Check', function() { expected: 'بول إيردوس' } ]; - expect(cases.length); - var $fixture; + QUnit.expect( cases.length ); - $.each(cases, function( i, item ) { - $fixture = $( '

' ).text( item.text ); - $fixture.highlightText( item.highlight ); - equals( + $.each( cases, function ( i, item ) { + $fixture = $( '

' ).text( item.text ).highlightText( item.highlight ); + assert.equal( $fixture.html(), - $('

' + item.expected + '

').html(), // re-parse to normalize! + $( '

' ).html( item.expected ).html(), // re-parse to normalize! item.desc || undefined - ); + ); } ); } ); diff --git a/tests/qunit/suites/resources/jquery/jquery.localize.test.js b/tests/qunit/suites/resources/jquery/jquery.localize.test.js index cd82863465..1b38010fe1 100644 --- a/tests/qunit/suites/resources/jquery/jquery.localize.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.localize.test.js @@ -1,13 +1,6 @@ -module( 'jquery.localize', QUnit.newMwEnvironment() ); - -test( '-- Initial check', function() { - expect(1); - ok( $.fn.localize, 'jQuery.fn.localize defined' ); -} ); - -test( 'Handle basic replacements', function() { - expect(3); +QUnit.module( 'jquery.localize', QUnit.newMwEnvironment() ); +QUnit.test( 'Handle basic replacements', 3, function ( assert ) { var html, $lc; mw.messages.set( 'basic', 'Basic stuff' ); @@ -15,24 +8,22 @@ test( 'Handle basic replacements', function() { html = '

'; $lc = $( html ).localize().find( 'span' ); - strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' ); + assert.strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' ); // Attribute: title-msg html = '
'; $lc = $( html ).localize().find( 'span' ); - strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' ); + assert.strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' ); // Attribute: alt-msg html = '
'; $lc = $( html ).localize().find( 'span' ); - strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' ); + assert.strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' ); } ); -test( 'Proper escaping', function() { - expect(2); - +QUnit.test( 'Proper escaping', 2, function ( assert ) { var html, $lc; mw.messages.set( 'properfoo', '' ); @@ -43,18 +34,16 @@ test( 'Proper escaping', function() { html = '
'; $lc = $( html ).localize().find( 'span' ); - strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' ); + assert.strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' ); // Attribute escaping html = '
'; $lc = $( html ).localize().find( 'span' ); - strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' ); + assert.strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' ); } ); -test( 'Options', function() { - expect(7); - +QUnit.test( 'Options', 7, function ( assert ) { mw.messages.set( { 'foo-lorem': 'Lorem', 'foo-ipsum': 'Ipsum', @@ -72,8 +61,8 @@ test( 'Options', function() { prefix: 'foo-' } ).find( 'span' ); - strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' ); - strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' ); + assert.strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' ); + assert.strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' ); // Variable keys mapping x = 'bar'; @@ -85,8 +74,8 @@ test( 'Options', function() { } } ).find( 'span' ); - strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' ); - strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' ); + assert.strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' ); + assert.strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' ); // Passing parameteters to mw.msg html = '
'; @@ -96,7 +85,7 @@ test( 'Options', function() { } } ).find( 'span' ); - strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' ); + assert.strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' ); // Combination of options prefix, params and keys x = 'bazz'; @@ -114,6 +103,6 @@ test( 'Options', function() { } } ).find( 'span' ); - strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' ); - strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' ); + assert.strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' ); + assert.strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' ); } ); diff --git a/tests/qunit/suites/resources/jquery/jquery.mwExtension.test.js b/tests/qunit/suites/resources/jquery/jquery.mwExtension.test.js index 3a2d0d831d..5b566ae0e8 100644 --- a/tests/qunit/suites/resources/jquery/jquery.mwExtension.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.mwExtension.test.js @@ -1,58 +1,58 @@ -module( 'jquery.mwExtension', QUnit.newMwEnvironment() ); +QUnit.module( 'jquery.mwExtension', QUnit.newMwEnvironment() ); -test( 'String functions', function() { +QUnit.test( 'String functions', function ( assert ) { - equal( $.trimLeft( ' foo bar ' ), 'foo bar ', 'trimLeft' ); - equal( $.trimRight( ' foo bar ' ), ' foo bar', 'trimRight' ); - equal( $.ucFirst( 'foo' ), 'Foo', 'ucFirst' ); + assert.equal( $.trimLeft( ' foo bar ' ), 'foo bar ', 'trimLeft' ); + assert.equal( $.trimRight( ' foo bar ' ), ' foo bar', 'trimRight' ); + assert.equal( $.ucFirst( 'foo' ), 'Foo', 'ucFirst' ); - equal( $.escapeRE( '