increase qunit coverage of mediawiki.html and mediawiki.util
authorTimo Tijhof <ttijhof@wikimedia.org>
Wed, 4 Apr 2012 20:48:46 +0000 (22:48 +0200)
committerTimo Tijhof <ttijhof@wikimedia.org>
Wed, 4 Apr 2012 20:48:46 +0000 (22:48 +0200)
- mw.html
 * Adding test for mw.html.Raw
 * Adding more tests for mw.html.element

- mw.util
 * Minor coding style update (local variables)
 * Updating mw.util.addPortletLink to not create its own "#mw-panel",
 this was previously done for ./qunit/index.html (which has been
 removed) but when ran on [[Special:JavaScriptTest/qunit]] it collides
 with the existing "#mw-panel", so far didn't break anything but that
 was purely lucky. This element is redundant so taking it out of the
 html sample
 * Removing redundant clean up, #qunit-fixture is automatically cleaned
 after each test

Change-Id: Iaf9791ca3cdcac1b732c851cdecc8fcd9f96fdd8

tests/qunit/suites/resources/mediawiki/mediawiki.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index ea1187e..e66cac2 100644 (file)
@@ -266,20 +266,22 @@ test( 'mw.loader.bug30825', function() {
        mw.loader.load( target );
 });
 
-test( 'mw.html', function() {
-       expect(11);
+test( 'mw.html', function () {
+       expect(13);
 
-       raises( function(){
+       raises( function () {
                mw.html.escape();
        }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
 
        equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
-               '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'html.escape escapes html snippet' );
+               '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'escape() escapes special characters to html entities' );
 
        equal( mw.html.element(),
-               '<undefined/>', 'html.element Always return a valid html string (even without arguments)' );
+               '<undefined/>', 'element() always returns a valid html string (even without arguments)' );
+
+       equal( mw.html.element( 'div' ), '<div/>', 'element() Plain DIV (simple)' );
 
-       equal( mw.html.element( 'div' ), '<div/>', 'html.element DIV (simple)' );
+       equal( mw.html.element( 'div', {}, '' ), '<div></div>', 'element() Basic DIV (simple)' );
 
        equal(
                mw.html.element(
@@ -294,6 +296,19 @@ test( 'mw.html', function() {
 
        equal( mw.html.element( 'p', { title: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
 
+       // Example from https://www.mediawiki.org/wiki/ResourceLoader/Default_modules#mediaWiki.html
+       equal(
+               mw.html.element(
+                       'div',
+                       {},
+                       new mw.html.Raw(
+                               mw.html.element( 'img', { src: '<' } )
+                       )
+               ),
+               '<div><img src="&lt;"/></div>',
+               'Raw inclusion of another element'
+       );
+
        equal(
                mw.html.element(
                        'option', {
index ea28935..d396b04 100644 (file)
@@ -140,48 +140,45 @@ test( '$content', function() {
  * Previously, test elements where invisible to the selector since only
  * one element can have a given id. 
  */
-test( 'addPortletLink', function() {
+test( 'addPortletLink', function () {
+       var pTestTb, vectorTabs, tbRL, tbMW, $tbMW, tbRLDM, caFoo;
        expect(7);
 
-       var mwPanel = '<div id="mw-panel" class="noprint">\
-       <h5>Toolbox</h5>\
+       pTestTb = '\
        <div class="portlet" id="p-test-tb">\
                <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>';
+       vectorTabs = '\
+       <div id="p-test-views" class="vectorTabs">\
+               <h5>Views</h5>\
+               <ul></ul>\
+       </div>';
+
+       $('#qunit-fixture').append(pTestTb, 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' );
 
-       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)' );
 
-       var tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
+       tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
                'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
 
        equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
 
-       var caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' );
+       caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' );
 
        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.' );
-
-       // Clean up
-       $( [tbRL, tbMW, tbRLDM, caFoo] ).remove();
 });
 
 test( 'jsMessage', function() {