[tests] use core qunit-fixture properly
authorKrinkle <krinkle@users.mediawiki.org>
Sun, 5 Feb 2012 12:27:20 +0000 (12:27 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Sun, 5 Feb 2012 12:27:20 +0000 (12:27 +0000)
QUnit provides a <div id="qunit-fixture"> by default that is cleared out after each test.

* Switch tests to append to the provided #qunit-fixture instead of appending to the body.
* Remove redundant .remove() calls, which now happens automatically
* Fix jquery.textSelection.test to not cause dozens of elements to be appended to the body with duplicate IDs, which was causing WebFonts test suite to fail (since the DOM can't select multiple elements with the same DOM). (Fixes r92923, r100391)

tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js
tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js
tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js
tests/qunit/suites/resources/jquery/jquery.tabIndex.test.js
tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js
tests/qunit/suites/resources/jquery/jquery.textSelection.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index ba03f2b..6e37138 100644 (file)
@@ -6,8 +6,8 @@ test( '-- Initial check', function() {
 });
 
 function createWrappedDiv( text, width ) {
-       var $wrapper = $( '<div />' ).css( 'width', width );
-       var $div = $( '<div />' ).text( text );
+       var $wrapper = $( '<div>' ).css( 'width', width );
+       var $div = $( '<div>' ).text( text );
        $wrapper.append( $div );
        return $wrapper;
 }
@@ -26,7 +26,7 @@ test( 'Position right', function() {
        // 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' );
-       $( 'body' ).append( $wrapper );
+       $( '#qunit-fixture' ).append( $wrapper );
        $wrapper.autoEllipsis( { position: 'right' } );
 
        // Verify that, and only one, span element was created
@@ -47,12 +47,9 @@ test( 'Position right', function() {
        // 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 ) {
+       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' );
        } else {
                gt( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more)' );
        }
-
-       // Clean up
-       $wrapper.remove();
 });
index 2c4dda6..3346c2d 100644 (file)
@@ -46,7 +46,7 @@ var byteLimitTest = function( options ) {
 
        test( opt.description, function() {
 
-               opt.$input.appendTo( 'body' );
+               opt.$input.appendTo( '#qunit-fixture' );
 
                // Simulate pressing keys for each of the sample characters
                $.addChars( opt.$input, opt.sample );
@@ -66,8 +66,6 @@ var byteLimitTest = function( options ) {
                        equal( newVal, opt.expected, 'New value matches the expected string' );
                        equal( $.byteLength( newVal ), $.byteLength( opt.expected ), 'Unlimited scenarios are not affected, expected length reached' );
                }
-
-               opt.$input.remove();
        } );
 };
 
index 8688f12..6489a1f 100644 (file)
@@ -1,5 +1,5 @@
 test('jquery.delayedBind with data option', function() {
-       var $fixture = $('<div>').appendTo('body'),
+       var $fixture = $('<div>').appendTo('#qunit-fixture'),
                data = { magic: "beeswax" },
                delay = 50;
 
@@ -20,7 +20,7 @@ test('jquery.delayedBind with data option', function() {
 });
 
 test('jquery.delayedBind without data option', function() {
-       var $fixture = $('<div>').appendTo('body'),
+       var $fixture = $('<div>').appendTo('#qunit-fixture'),
                data = { magic: "beeswax" },
                delay = 50;
 
index f26ba7b..98ff550 100644 (file)
@@ -18,14 +18,11 @@ test( 'firstTabIndex', function() {
        '<textarea tabindex="5">Foobar</textarea>' +
 '</form>';
 
-       var $testA = $( '<div>' ).html( testEnvironment ).appendTo( 'body' );
+       var $testA = $( '<div>' ).html( testEnvironment ).appendTo( '#qunit-fixture' );
        strictEqual( $testA.firstTabIndex(), 2, 'First tabindex should be 2 within this context.' );
 
        var $testB = $( '<div>' );
        strictEqual( $testB.firstTabIndex(), null, 'Return null if none available.' );
-
-       // Clean up
-       $testA.add( $testB ).remove();
 });
 
 test( 'lastTabIndex', function() {
@@ -39,12 +36,9 @@ test( 'lastTabIndex', function() {
        '<textarea tabindex="5">Foobar</textarea>' +
 '</form>';
 
-       var $testA = $( '<div>' ).html( testEnvironment ).appendTo( 'body' );
+       var $testA = $( '<div>' ).html( testEnvironment ).appendTo( '#qunit-fixture' );
        strictEqual( $testA.lastTabIndex(), 9, 'Last tabindex should be 9 within this context.' );
 
        var $testB = $( '<div>' );
        strictEqual( $testB.lastTabIndex(), null, 'Return null if none available.' );
-
-       // Clean up
-       $testA.add( $testB ).remove();
 });
index 31415d6..7ecdc4b 100644 (file)
@@ -79,7 +79,6 @@ var tableTest = function( msg, header, data, expected, callback ) {
                expect(1);
 
                var $table = tableCreate( header, data );
-               //$( 'body' ).append($table);
 
                // Give caller a chance to set up sorting and manipulate the table.
                callback( $table );
index a9a2446..1b2f302 100644 (file)
@@ -36,16 +36,14 @@ var encapsulateTest = function( options ) {
 
        test( opt.description, function() {
                var tests = 1;
-               if (opt.after.selected !== null) {
+               if ( opt.after.selected !== null ) {
                        tests++;
                }
-               expect(tests);
+               expect( tests );
 
-               var $fixture = $( '<div id="qunit-fixture"></div>' );
                var $textarea = $( '<textarea>' );
 
-               $fixture.append($textarea);
-               $( 'body' ).append($fixture);
+               $( '#qunit-fixture' ).append( $textarea );
 
                //$textarea.textSelection( 'setContents', opt.before.text); // this method is actually missing atm...
                $textarea.val( opt.before.text ); // won't work with the WikiEditor iframe?
@@ -228,11 +226,9 @@ var caretTest = function(options) {
        test(options.description, function() {
                expect(2);
 
-               var $fixture = $( '<div id="qunit-fixture"></div>' );
                var $textarea = $( '<textarea>' ).text(options.text);
 
-               $fixture.append($textarea);
-               $( 'body' ).append($fixture);
+               $( '#qunit-fixture' ).append( $textarea );
 
                if (options.mode == 'set') {
                        $textarea.textSelection('setSelection', {
index c60a3ef..ea28935 100644 (file)
@@ -52,7 +52,7 @@ test( 'wikiScript', function() {
 test( 'addCSS', function() {
        expect(3);
 
-       var $testEl = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( 'body' );
+       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' );
@@ -61,9 +61,7 @@ test( 'addCSS', function() {
        equal( $testEl.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
 
        // Clean up
-       $( style.ownerNode )
-               .add( $testEl )
-               .remove();
+       $( style.ownerNode ).remove();
 });
 
 test( 'toggleToc', function() {
@@ -79,7 +77,7 @@ test( 'toggleToc', function() {
                '</div>' +
                '<ul><li></li></ul>' +
        '</td></tr></table>',
-               $toc = $(tocHtml).appendTo( 'body' ),
+               $toc = $(tocHtml).appendTo( '#qunit-fixture' ),
                $toggleLink = $( '#togglelink' );
 
        strictEqual( $toggleLink.length, 1, 'Toggle link is appended to the page.' );
@@ -90,9 +88,6 @@ test( 'toggleToc', function() {
 
        var actionC = function() {
                start();
-
-               // Clean up
-               $toc.remove();
        };
        var actionB = function() {
                start(); stop();
@@ -158,8 +153,8 @@ test( 'addPortletLink', function() {
        <h5>Views</h5>\
        <ul></ul>\
 </div>',
-       $mwPanel = $(mwPanel).appendTo( 'body' ),
-       $vectorTabs = $(vectorTabs).appendTo( 'body' );
+       $mwPanel = $(mwPanel).appendTo( '#qunit-fixture' ),
+       $vectorTabs = $(vectorTabs).appendTo( '#qunit-fixture' );
 
        var tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
                'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' );
@@ -186,10 +181,7 @@ test( 'addPortletLink', function() {
        strictEqual( $( caFoo ).find( 'span').length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
 
        // Clean up
-       $( [tbRL, tbMW, tbRLDM, caFoo] )
-               .add( $mwPanel )
-               .add( $vectorTabs )
-               .remove();
+       $( [tbRL, tbMW, tbRLDM, caFoo] ).remove();
 });
 
 test( 'jsMessage', function() {