mw.loader: Clean up unit tests by using newer QUnit syntax
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 4 Aug 2016 23:24:07 +0000 (16:24 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 4 Aug 2016 23:38:12 +0000 (16:38 -0700)
* Remove use of deprecated asyncTest() and QUnit.start().
  Instead, use test() and assert.async().

* Where possible, simply return a Promise instead of manually
  attaching up assert.async() to done(), fail() or always().

See <https://api.qunitjs.com/async/> and <https://api.qunitjs.com/QUnit.test/>

Change-Id: I8ec7fdc9c271bbc17555f2e750032282b1683d0e

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

index 3f426e9..baec37c 100644 (file)
                );
        }
 
-       QUnit.asyncTest( 'mw.loader', 2, function ( assert ) {
+       QUnit.test( 'mw.loader', 2, function ( assert ) {
                var isAwesomeDone;
 
                mw.loader.testCallback = function () {
-                       QUnit.start();
                        assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
                        isAwesomeDone = true;
                };
 
                mw.loader.implement( 'test.callback', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ] );
 
-               mw.loader.using( 'test.callback', function () {
-
-                       // /sample/awesome.js declares the "mw.loader.testCallback" function
-                       // which contains a call to start() and ok()
+               return mw.loader.using( 'test.callback', function () {
                        assert.strictEqual( isAwesomeDone, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
                        delete mw.loader.testCallback;
 
                }, function () {
-                       QUnit.start();
                        assert.ok( false, 'Error callback fired while loader.using "test.callback" module' );
                } );
        } );
 
-       QUnit.asyncTest( 'mw.loader with Object method as module name', 2, function ( assert ) {
+       QUnit.test( 'mw.loader with Object method as module name', 2, function ( assert ) {
                var isAwesomeDone;
 
                mw.loader.testCallback = function () {
-                       QUnit.start();
                        assert.strictEqual( isAwesomeDone, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
                        isAwesomeDone = true;
                };
 
                mw.loader.implement( 'hasOwnProperty', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ], {}, {} );
 
-               mw.loader.using( 'hasOwnProperty', function () {
-
-                       // /sample/awesome.js declares the "mw.loader.testCallback" function
-                       // which contains a call to start() and ok()
+               return mw.loader.using( 'hasOwnProperty', function () {
                        assert.strictEqual( isAwesomeDone, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
                        delete mw.loader.testCallback;
 
                }, function () {
-                       QUnit.start();
                        assert.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
                } );
        } );
 
-       QUnit.asyncTest( 'mw.loader.using( .. ) Promise', 2, function ( assert ) {
+       QUnit.test( 'mw.loader.using( .. ) Promise', 2, function ( assert ) {
                var isAwesomeDone;
 
                mw.loader.testCallback = function () {
-                       QUnit.start();
                        assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
                        isAwesomeDone = true;
                };
 
                mw.loader.implement( 'test.promise', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ] );
 
-               mw.loader.using( 'test.promise' )
+               return mw.loader.using( 'test.promise' )
                .done( function () {
-
-                       // /sample/awesome.js declares the "mw.loader.testCallback" function
-                       // which contains a call to start() and ok()
                        assert.strictEqual( isAwesomeDone, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
                        delete mw.loader.testCallback;
 
                } )
                .fail( function () {
-                       QUnit.start();
                        assert.ok( false, 'Error callback fired while loader.using "test.promise" module' );
                } );
        } );
 
-       QUnit.asyncTest( 'mw.loader.implement( styles={ "css": [text, ..] } )', 2, function ( assert ) {
+       QUnit.test( 'mw.loader.implement( styles={ "css": [text, ..] } )', 2, function ( assert ) {
                var $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
 
                assert.notEqual(
                                        'right',
                                        'style is applied'
                                );
-                               QUnit.start();
                        },
                        {
                                all: '.mw-test-implement-a { float: right; }'
                        }
                );
 
-               mw.loader.load( [
-                       'test.implement.a'
-               ] );
+               return mw.loader.using( 'test.implement.a' );
        } );
 
-       QUnit.asyncTest( 'mw.loader.implement( styles={ "url": { <media>: [url, ..] } } )', 7, function ( assert ) {
+       QUnit.test( 'mw.loader.implement( styles={ "url": { <media>: [url, ..] } } )', 7, function ( assert ) {
                var $element1 = $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
                        $element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
-                       $element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' );
+                       $element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
+                       done = assert.async();
 
                assert.notEqual(
                        $element1.css( 'text-align' ),
                mw.loader.implement(
                        'test.implement.b',
                        function () {
-                               // Note: QUnit.start() must only be called when the entire test is
+                               // Note: done() must only be called when the entire test is
                                // complete. So, make sure that we don't start until *both*
                                // assertStyleAsync calls have completed.
                                var pending = 2;
 
                                        pending--;
                                        if ( pending === 0 ) {
-                                               QUnit.start();
+                                               done();
                                        }
                                } );
                                assertStyleAsync( assert, $element3, 'float', 'right', function () {
 
                                        pending--;
                                        if ( pending === 0 ) {
-                                               QUnit.start();
+                                               done();
                                        }
                                } );
                        },
                        }
                );
 
-               mw.loader.load( [
-                       'test.implement.b'
-               ] );
+               mw.loader.load( 'test.implement.b' );
        } );
 
        // Backwards compatibility
-       QUnit.asyncTest( 'mw.loader.implement( styles={ <media>: text } ) (back-compat)', 2, function ( assert ) {
+       QUnit.test( 'mw.loader.implement( styles={ <media>: text } ) (back-compat)', 2, function ( assert ) {
                var $element = $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
 
                assert.notEqual(
                                        'right',
                                        'style is applied'
                                );
-                               QUnit.start();
                        },
                        {
                                all: '.mw-test-implement-c { float: right; }'
                        }
                );
 
-               mw.loader.load( [
-                       'test.implement.c'
-               ] );
+               return mw.loader.using( 'test.implement.c' );
        } );
 
        // Backwards compatibility
-       QUnit.asyncTest( 'mw.loader.implement( styles={ <media>: [url, ..] } ) (back-compat)', 4, function ( assert ) {
+       QUnit.test( 'mw.loader.implement( styles={ <media>: [url, ..] } ) (back-compat)', 4, function ( assert ) {
                var $element = $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
-                       $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' );
+                       $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
+                       done = assert.async();
 
                assert.notEqual(
                        $element.css( 'float' ),
                        'test.implement.d',
                        function () {
                                assertStyleAsync( assert, $element, 'float', 'right', function () {
-
                                        assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied (bug 40500)' );
-
-                                       QUnit.start();
+                                       done();
                                } );
                        },
                        {
                        }
                );
 
-               mw.loader.load( [
-                       'test.implement.d'
-               ] );
+               mw.loader.load( 'test.implement.d' );
        } );
 
        // @import (bug 31676)
-       QUnit.asyncTest( 'mw.loader.implement( styles has @import )', 7, function ( assert ) {
-               var isJsExecuted, $element;
+       QUnit.test( 'mw.loader.implement( styles has @import )', 7, function ( assert ) {
+               var isJsExecuted, $element,
+                       done = assert.async();
 
                mw.loader.implement(
                        'test.implement.import',
                                                'CSS styles after the @import rule are working'
                                        );
 
-                                       QUnit.start();
+                                       done();
                                } );
                        },
                        {
                } );
        } );
 
-       QUnit.asyncTest( 'mw.loader.implement( dependency with styles )', 4, function ( assert ) {
+       QUnit.test( 'mw.loader.implement( dependency with styles )', 4, function ( assert ) {
                var $element = $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
                        $element2 = $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
 
                                        'right',
                                        'Depending module\'s style is applied'
                                );
-                               QUnit.start();
                        },
                        {
                                all: '.mw-test-implement-e { float: right; }'
                        }
                );
 
-               mw.loader.load( [
-                       'test.implement.e'
-               ] );
+               return mw.loader.using( 'test.implement.e' );
        } );
 
        QUnit.test( 'mw.loader.implement( only scripts )', 1, function ( assert ) {
                assert.strictEqual( mw.loader.getState( 'test.onlyscripts' ), 'ready' );
        } );
 
-       QUnit.asyncTest( 'mw.loader.implement( only messages )', 2, function ( assert ) {
+       QUnit.test( 'mw.loader.implement( only messages )', 2, function ( assert ) {
                assert.assertFalse( mw.messages.exists( 'bug_29107' ), 'Verify that the test message doesn\'t exist yet' );
 
                // jscs: disable requireCamelCaseOrUpperCaseIdentifiers
                mw.loader.implement( 'test.implement.msgs', [], {}, { bug_29107: 'loaded' } );
                // jscs: enable requireCamelCaseOrUpperCaseIdentifiers
-               mw.loader.using( 'test.implement.msgs', function () {
-                       QUnit.start();
+
+               return mw.loader.using( 'test.implement.msgs', function () {
                        assert.ok( mw.messages.exists( 'bug_29107' ), 'Bug 29107: messages-only module should implement ok' );
                }, function () {
-                       QUnit.start();
                        assert.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
                } );
        } );
                );
        } );
 
-       QUnit.asyncTest( 'mw.loader dependency handling', 5, function ( assert ) {
+       QUnit.test( 'mw.loader dependency handling', 5, function ( assert ) {
+               var done = assert.async();
                mw.loader.register( [
                        // [module, version, dependencies, group, source]
                        [ 'testMissing', '1', [], null, 'testloader' ],
 
                                verifyModuleStates();
 
-                               QUnit.start();
+                               done();
                        },
                        function ( e, badmodules ) {
                                assert.ok( true, 'Error handler should be invoked.' );
 
                                verifyModuleStates();
 
-                               QUnit.start();
+                               done();
                        }
                );
        } );
 
-       QUnit.asyncTest( 'mw.loader skin-function handling', 5, function ( assert ) {
+       QUnit.test( 'mw.loader skin-function handling', 5, function ( assert ) {
                mw.loader.register( [
                        // [module, version, dependencies, group, source, skip]
                        [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
                        assert.equal( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
                }
 
-               mw.loader.using( [ 'testUsesSkippable' ],
+               return mw.loader.using( [ 'testUsesSkippable' ],
                        function () {
                                assert.ok( true, 'Success handler should be invoked.' );
                                assert.ok( true ); // Dummy to match error handler and reach QUnit expect()
 
                                verifyModuleStates();
-
-                               QUnit.start();
                        },
                        function ( e, badmodules ) {
                                assert.ok( false, 'Error handler should not be invoked.' );
                                assert.deepEqual( badmodules, [], 'Bad modules as expected.' );
 
                                verifyModuleStates();
-
-                               QUnit.start();
                        }
                );
        } );