diff options
Diffstat (limited to 'tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js')
-rw-r--r-- | tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js | 241 |
1 files changed, 112 insertions, 129 deletions
diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index ea28935e..ababa8d9 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -1,73 +1,58 @@ -module( 'mediawiki.util', QUnit.newMwEnvironment() ); +QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment() ); -test( '-- Initial check', function() { - expect(1); - - ok( mw.util, 'mw.util defined' ); -}); - -test( 'rawurlencode', function() { - expect(1); - - equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' ); +QUnit.test( 'rawurlencode', 1, function ( assert ) { + assert.equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' ); }); -test( 'wikiUrlencode', function() { - expect(1); - - equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' ); +QUnit.test( 'wikiUrlencode', 1, function ( assert ) { + assert.equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' ); }); -test( 'wikiGetlink', function() { - expect(3); - +QUnit.test( 'wikiGetlink', 3, function ( assert ) { // Not part of startUp module mw.config.set( 'wgArticlePath', '/wiki/$1' ); mw.config.set( 'wgPageName', 'Foobar' ); var hrefA = mw.util.wikiGetlink( 'Sandbox' ); - equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' ); + assert.equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' ); var hrefB = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' ); - equal( hrefB, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage', + assert.equal( hrefB, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage', 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' ); var hrefC = mw.util.wikiGetlink(); - equal( hrefC, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' ); + assert.equal( hrefC, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' ); }); -test( 'wikiScript', function() { - expect(2); - +QUnit.test( 'wikiScript', 4, function ( assert ) { mw.config.set({ - 'wgScript': '/w/index.php', + 'wgScript': '/w/i.php', // customized wgScript for bug 39103 + 'wgLoadScript': '/w/l.php', // customized wgLoadScript for bug 39103 'wgScriptPath': '/w', 'wgScriptExtension': '.php' }); - equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript' ); - equal( mw.util.wikiScript( 'api' ), '/w/api.php', 'API path' ); + assert.equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ), 'wikiScript() returns wgScript' ); + assert.equal( mw.util.wikiScript( 'index' ), mw.config.get( 'wgScript' ), "wikiScript( 'index' ) returns wgScript" ); + assert.equal( mw.util.wikiScript( 'load' ), mw.config.get( 'wgLoadScript' ), "wikiScript( 'load' ) returns wgLoadScript" ); + assert.equal( mw.util.wikiScript( 'api' ), '/w/api.php', 'API path' ); }); -test( 'addCSS', function() { - expect(3); - +QUnit.test( 'addCSS', 3, function ( assert ) { var $testEl = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' ); var style = mw.util.addCSS( '#mw-addcsstest { visibility: hidden; }' ); - equal( typeof style, 'object', 'addCSS returned an object' ); - strictEqual( style.disabled, false, 'property "disabled" is available and set to false' ); + assert.equal( typeof style, 'object', 'addCSS returned an object' ); + assert.strictEqual( style.disabled, false, 'property "disabled" is available and set to false' ); - equal( $testEl.css( 'visibility' ), 'hidden', 'Added style properties are in effect' ); + assert.equal( $testEl.css( 'visibility' ), 'hidden', 'Added style properties are in effect' ); // Clean up $( style.ownerNode ).remove(); }); -test( 'toggleToc', function() { - expect(4); - - strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' ); +QUnit.asyncTest( 'toggleToc', 4, function ( assert ) { + assert.strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' ); var tocHtml = '<table id="toc" class="toc"><tr><td>' + @@ -80,57 +65,46 @@ test( 'toggleToc', function() { $toc = $(tocHtml).appendTo( '#qunit-fixture' ), $toggleLink = $( '#togglelink' ); - strictEqual( $toggleLink.length, 1, 'Toggle link is appended to the page.' ); - - // Toggle animation is asynchronous - // QUnit should not finish this test() untill they are all done - stop(); + assert.strictEqual( $toggleLink.length, 1, 'Toggle link is appended to the page.' ); var actionC = function() { - start(); + QUnit.start(); }; var actionB = function() { - start(); stop(); - strictEqual( mw.util.toggleToc( $toggleLink, actionC ), true, 'Return boolean true if the TOC is now visible.' ); + assert.strictEqual( mw.util.toggleToc( $toggleLink, actionC ), true, 'Return boolean true if the TOC is now visible.' ); }; var actionA = function() { - strictEqual( mw.util.toggleToc( $toggleLink, actionB ), false, 'Return boolean false if the TOC is now hidden.' ); + assert.strictEqual( mw.util.toggleToc( $toggleLink, actionB ), false, 'Return boolean false if the TOC is now hidden.' ); }; actionA(); }); -test( 'getParamValue', function() { - expect(5); - +QUnit.test( 'getParamValue', 5, function ( assert ) { var url1 = 'http://example.org/?foo=wrong&foo=right#&foo=bad'; - equal( mw.util.getParamValue( 'foo', url1 ), 'right', 'Use latest one, ignore hash' ); - strictEqual( mw.util.getParamValue( 'bar', url1 ), null, 'Return null when not found' ); + assert.equal( mw.util.getParamValue( 'foo', url1 ), 'right', 'Use latest one, ignore hash' ); + assert.strictEqual( mw.util.getParamValue( 'bar', url1 ), null, 'Return null when not found' ); var url2 = 'http://example.org/#&foo=bad'; - strictEqual( mw.util.getParamValue( 'foo', url2 ), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' ); + assert.strictEqual( mw.util.getParamValue( 'foo', url2 ), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' ); var url3 = 'example.org?' + $.param({ 'TEST': 'a b+c' }); - strictEqual( mw.util.getParamValue( 'TEST', url3 ), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' ); + assert.strictEqual( mw.util.getParamValue( 'TEST', url3 ), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' ); var url4 = 'example.org?' + $.param({ 'TEST': 'a b+c d' }); // check for sloppy code from r95332 :) - strictEqual( mw.util.getParamValue( 'TEST', url4 ), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' ); + assert.strictEqual( mw.util.getParamValue( 'TEST', url4 ), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' ); }); -test( 'tooltipAccessKey', function() { - expect(3); - - equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' ); - ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' ); - ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' ); +QUnit.test( 'tooltipAccessKey', 3, function ( assert ) { + assert.equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' ); + assert.ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' ); + assert.ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' ); }); -test( '$content', function() { - expect(2); - - ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' ); - strictEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' ); +QUnit.test( '$content', 2, function ( assert ) { + assert.ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' ); + assert.strictEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' ); }); @@ -138,87 +112,98 @@ test( '$content', function() { * Portlet names are prefixed with 'p-test' to avoid conflict with core * when running the test suite under a wiki page. * Previously, test elements where invisible to the selector since only - * one element can have a given id. + * one element can have a given id. */ -test( 'addPortletLink', function() { - expect(7); - - var mwPanel = '<div id="mw-panel" class="noprint">\ - <h5>Toolbox</h5>\ +QUnit.test( 'addPortletLink', 8, function ( assert ) { + var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo; + pTestTb = '\ <div class="portlet" id="p-test-tb">\ + <h5>Toolbox</h5>\ <ul class="body"></ul>\ - </div>\ -</div>', - vectorTabs = '<div id="p-test-views" class="vectorTabs">\ - <h5>Views</h5>\ - <ul></ul>\ -</div>', - $mwPanel = $(mwPanel).appendTo( '#qunit-fixture' ), - $vectorTabs = $(vectorTabs).appendTo( '#qunit-fixture' ); - - var tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader', + </div>'; + pCustom = '\ + <div class="portlet" id="p-test-custom">\ + <h5>Views</h5>\ + <ul class="body">\ + <li id="c-foo"><a href="#">Foo</a></li>\ + <li id="c-barmenu">\ + <ul>\ + <li id="c-bar-baz"><a href="#">Baz</a></a>\ + </ul>\ + </li>\ + </ul>\ + </div>'; + vectorTabs = '\ + <div id="p-test-views" class="vectorTabs">\ + <h5>Views</h5>\ + <ul></ul>\ + </div>'; + + $( '#qunit-fixture' ).append( pTestTb, pCustom, vectorTabs ); + + tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader', 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' ); - ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' ); + assert.ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' ); - var tbMW = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/', - 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL ), - $tbMW = $( tbMW ); + tbMW = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/', + 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL ); + $tbMW = $( tbMW ); - equal( $tbMW.attr( 'id' ), 't-mworg', 'Link has correct ID set' ); - equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' ); - equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' ); + assert.equal( $tbMW.attr( 'id' ), 't-mworg', 'Link has correct ID set' ); + assert.equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' ); + assert.equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' ); - var tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM', - 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' ); + cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux' ); + $cuQuux = $(cuQuux); - equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' ); + assert.equal( + $( '#p-test-custom #c-barmenu ul li' ).length, + 1, + 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)' + ); - var caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' ); + tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM', + 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' ); - strictEqual( $tbMW.find( 'span').length, 0, 'No <span> element should be added for porlets without vectorTabs class.' ); - strictEqual( $( caFoo ).find( 'span').length, 1, 'A <span> element should be added for porlets with vectorTabs class.' ); + assert.equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' ); - // Clean up - $( [tbRL, tbMW, tbRLDM, caFoo] ).remove(); -}); + caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' ); -test( 'jsMessage', function() { - expect(1); + assert.strictEqual( $tbMW.find( 'span').length, 0, 'No <span> element should be added for porlets without vectorTabs class.' ); + assert.strictEqual( $( caFoo ).find( 'span').length, 1, 'A <span> element should be added for porlets with vectorTabs class.' ); +}); +QUnit.test( 'jsMessage', 1, function ( assert ) { var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." ); - ok( a, 'Basic checking of return value' ); + assert.ok( a, 'Basic checking of return value' ); // Clean up $( '#mw-js-message' ).remove(); }); -test( 'validateEmail', function() { - expect(6); - - strictEqual( mw.util.validateEmail( "" ), null, 'Should return null for empty string ' ); - strictEqual( mw.util.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' ); +QUnit.test( 'validateEmail', 6, function ( assert ) { + assert.strictEqual( mw.util.validateEmail( "" ), null, 'Should return null for empty string ' ); + assert.strictEqual( mw.util.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' ); // testEmailWithCommasAreInvalids - strictEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' ); - strictEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' ); + assert.strictEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' ); + assert.strictEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' ); // testEmailWithHyphens - strictEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' ); - strictEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' ); + assert.strictEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' ); + assert.strictEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' ); }); -test( 'isIPv6Address', function() { - expect(40); - +QUnit.test( 'isIPv6Address', 40, function ( assert ) { // Shortcuts - var assertFalseIPv6 = function( addy, summary ) { - return strictEqual( mw.util.isIPv6Address( addy ), false, summary ); - }, - assertTrueIPv6 = function( addy, summary ) { - return strictEqual( mw.util.isIPv6Address( addy ), true, summary ); - }; + function assertFalseIPv6( addy, summary ) { + return assert.strictEqual( mw.util.isIPv6Address( addy ), false, summary ); + } + function assertTrueIPv6( addy, summary ) { + return assert.strictEqual( mw.util.isIPv6Address( addy ), true, summary ); + } // Based on IPTest.php > testisIPv6 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' ); @@ -232,7 +217,7 @@ test( 'isIPv6Address', function() { 'fc:100:a:d::', 'fc:100:a:d:1::', 'fc:100:a:d:1:e::', - 'fc:100:a:d:1:e:ac::'], function( i, addy ){ + 'fc:100:a:d:1:e:ac::'], function ( i, addy ){ assertTrueIPv6( addy, addy + ' is a valid IP' ); }); @@ -253,7 +238,7 @@ test( 'isIPv6Address', function() { '::fc:100:a:d:1:e', '::fc:100:a:d:1:e:ac', - 'fc:100:a:d:1:e:ac:0'], function( i, addy ){ + 'fc:100:a:d:1:e:ac:0'], function ( i, addy ){ assertTrueIPv6( addy, addy + ' is a valid IP' ); }); @@ -278,16 +263,14 @@ test( 'isIPv6Address', function() { assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' ); }); -test( 'isIPv4Address', function() { - expect(11); - +QUnit.test( 'isIPv4Address', 11, function ( assert ) { // Shortcuts - var assertFalseIPv4 = function( addy, summary ) { - return strictEqual( mw.util.isIPv4Address( addy ), false, summary ); - }, - assertTrueIPv4 = function( addy, summary ) { - return strictEqual( mw.util.isIPv4Address( addy ), true, summary ); - }; + function assertFalseIPv4( addy, summary ) { + assert.strictEqual( mw.util.isIPv4Address( addy ), false, summary ); + } + function assertTrueIPv4( addy, summary ) { + assert.strictEqual( mw.util.isIPv4Address( addy ), true, summary ); + } // Based on IPTest.php > testisIPv4 assertFalseIPv4( false, 'Boolean false is not an IP' ); |