jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/372173 )

Change subject: Return promises from QUnit test
......................................................................


Return promises from QUnit test

Simplify all our tests to return to promises
Use catch rather than fail when testing error cases.

Bug: T170812
Change-Id: I37c4e3f86343052c946d8586f8ff840a81f631f8
---
M tests/node-qunit/actions.test.js
M tests/node-qunit/gateway/mediawiki.test.js
M tests/node-qunit/gateway/rest.test.js
M tests/node-qunit/integration.test.js
M tests/node-qunit/ui/renderer.test.js
M tests/node-qunit/wait.test.js
6 files changed, 47 insertions(+), 90 deletions(-)

Approvals:
  Bmansurov: Looks good to me, approved
  jenkins-bot: Verified
  Jdlrobson: Looks good to me, approved



diff --git a/tests/node-qunit/actions.test.js b/tests/node-qunit/actions.test.js
index 7ce116e..c52c0cc 100644
--- a/tests/node-qunit/actions.test.js
+++ b/tests/node-qunit/actions.test.js
@@ -114,7 +114,7 @@
 } );
 
 QUnit.test( '#linkDwell', function ( assert ) {
-       var done = assert.async(),
+       var p,
                event = {},
                dispatch = this.sandbox.spy();
 
@@ -150,22 +150,21 @@
 
        // ---
 
-       this.waitPromises[ 0 ].then( function () {
+       p = this.waitPromises[ 0 ].then( function () {
                assert.strictEqual(
                        dispatch.callCount,
                        2,
                        'The fetch action is dispatched after FETCH_COMPLETE 
milliseconds.'
                );
-
-               done();
        } );
 
        // After FETCH_START_DELAY milliseconds...
        this.waitDeferreds[ 0 ].resolve();
+       return p;
 } );
 
 QUnit.test( '#linkDwell doesn\'t continue when previews are disabled', 
function ( assert ) {
-       var done = assert.async(),
+       var p,
                event = {},
                dispatch = this.sandbox.spy();
 
@@ -183,18 +182,17 @@
 
        assert.strictEqual( this.wait.callCount, 1 );
 
-       this.waitPromises[ 0 ].then( function () {
+       p = this.waitPromises[ 0 ].then( function () {
                assert.strictEqual( dispatch.callCount, 1 );
-
-               done();
        } );
 
        // After FETCH_START_DELAY milliseconds...
        this.waitDeferreds[ 0 ].resolve();
+       return p;
 } );
 
 QUnit.test( '#linkDwell doesn\'t continue if the token has changed', function 
( assert ) {
-       var done = assert.async(),
+       var p,
                event = {},
                dispatch = this.sandbox.spy();
 
@@ -222,18 +220,17 @@
                activeToken: 'banana'
        };
 
-       this.waitPromises[ 0 ].then( function () {
+       p = this.waitPromises[ 0 ].then( function () {
                assert.strictEqual( dispatch.callCount, 1 );
-
-               done();
        } );
 
        // After FETCH_START_DELAY milliseconds...
        this.waitDeferreds[ 0 ].resolve();
+       return p;
 } );
 
 QUnit.test( '#linkDwell dispatches the fetch action', function ( assert ) {
-       var done = assert.async(),
+       var p,
                event = {},
                dispatch = this.sandbox.spy();
 
@@ -247,14 +244,13 @@
                this.getState
        );
 
-       this.waitPromises[ 0 ].then( function () {
+       p = this.waitPromises[ 0 ].then( function () {
                assert.strictEqual( dispatch.callCount, 2 );
-
-               done();
        } );
 
        // After FETCH_START_DELAY milliseconds...
        this.waitDeferreds[ 0 ].resolve();
+       return p;
 } );
 
 QUnit.module( 'ext.popups/actions#fetch', {
@@ -308,15 +304,14 @@
 } );
 
 QUnit.test( 'it should dispatch the FETCH_END action when the API request 
ends', function ( assert ) {
-       var that = this,
-               done = assert.async();
+       var that = this;
 
        this.fetch();
 
        this.now += 115;
        this.gatewayDeferred.resolve( {} );
 
-       this.gatewayPromise.then( function () {
+       return this.gatewayPromise.then( function () {
                assert.deepEqual(
                        that.dispatch.getCall( 1 ).args[ 0 ],
                        {
@@ -325,8 +320,6 @@
                                timestamp: 115
                        }
                );
-
-               done();
        } );
 } );
 
@@ -335,8 +328,7 @@
                whenSpy,
                args,
                result = {},
-               that = this,
-               done = assert.async();
+               that = this;
 
        whenSpy = this.sandbox.stub( $, 'when' )
                .returns( whenDeferred.promise() );
@@ -362,7 +354,7 @@
        this.now += 500;
        whenDeferred.resolve( result );
 
-       whenDeferred.then( function () {
+       return whenDeferred.then( function () {
 
                // Ensure the following assertions are made after all callbacks 
have been
                // executed. Use setTimeout( _, 0 ) since it's not critical 
that these
@@ -379,8 +371,6 @@
                                        token: that.token
                                }
                        );
-
-                       done();
                }, 0 );
        } );
 } );
@@ -400,8 +390,7 @@
                                        activeToken: token
                                }
                        };
-               },
-               done = assert.async();
+               }, p;
 
        this.sandbox.stub( mw, 'now' ).returns( new Date() );
 
@@ -420,7 +409,7 @@
                'Have you spoken with #Design about changing this value?'
        );
 
-       this.waitPromises[ 0 ].then( function () {
+       p = this.waitPromises[ 0 ].then( function () {
                assert.ok(
                        dispatch.calledWith( {
                                type: 'ABANDON_END',
@@ -428,12 +417,11 @@
                        } ),
                        'ABANDON_* share the same token.'
                );
-
-               done();
        } );
 
        // After ABANDON_END_DELAY milliseconds...
        this.waitDeferreds[ 0 ].resolve();
+       return p;
 } );
 
 QUnit.test( 'it shouldn\'t dispatch under certain conditions', function ( 
assert ) {
diff --git a/tests/node-qunit/gateway/mediawiki.test.js 
b/tests/node-qunit/gateway/mediawiki.test.js
index 733a460..a7d6197 100644
--- a/tests/node-qunit/gateway/mediawiki.test.js
+++ b/tests/node-qunit/gateway/mediawiki.test.js
@@ -153,19 +153,19 @@
 } );
 
 QUnit.test( 'MediaWiki API gateway handles the API failure', function ( assert 
) {
-       var deferred = $.Deferred(),
+       var p,
+               deferred = $.Deferred(),
                api = {
                        get: this.sandbox.stub().returns( deferred.promise() )
                },
-               gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS ),
-               done = assert.async( 1 );
+               gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
 
-       gateway.getPageSummary( 'Test Title' ).fail( function () {
+       p = gateway.getPageSummary( 'Test Title' ).catch( function () {
                assert.ok( true );
-               done();
        } );
 
        deferred.reject();
+       return p;
 } );
 
 QUnit.test( 'MediaWiki API gateway returns the correct data ', function ( 
assert ) {
@@ -174,12 +174,10 @@
                                $.Deferred().resolve( MEDIAWIKI_API_RESPONSE 
).promise()
                        )
                },
-               gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS ),
-               done = assert.async( 1 );
+               gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
 
-       gateway.getPageSummary( 'Test Title' ).done( function ( result ) {
+       return gateway.getPageSummary( 'Test Title' ).done( function ( result ) 
{
                assert.deepEqual( result, MEDIAWIKI_API_RESPONSE_PREVIEW_MODEL 
);
-               done();
        } );
 } );
 
@@ -213,12 +211,9 @@
                                $.Deferred().resolve( response ).promise()
                        )
                },
-               gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS ),
-               done = assert.async( 1 );
+               gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
 
-       gateway.getPageSummary( 'Test Title' ).done( function ( result ) {
+       return gateway.getPageSummary( 'Test Title' ).done( function ( result ) 
{
                assert.deepEqual( result, model );
-
-               done();
        } );
 } );
diff --git a/tests/node-qunit/gateway/rest.test.js 
b/tests/node-qunit/gateway/rest.test.js
index 63b5e15..53ad73f 100644
--- a/tests/node-qunit/gateway/rest.test.js
+++ b/tests/node-qunit/gateway/rest.test.js
@@ -231,12 +231,10 @@
 QUnit.test( 'RESTBase gateway handles the API failure', function ( assert ) {
        var deferred = $.Deferred(),
                api = this.sandbox.stub().returns( deferred.reject( { status: 
500 } ).promise() ),
-               gateway = createRESTBaseGateway( api ),
-               done = assert.async( 1 );
+               gateway = createRESTBaseGateway( api );
 
-       gateway.getPageSummary( 'Test Title' ).fail( function () {
+       return gateway.getPageSummary( 'Test Title' ).catch( function () {
                assert.ok( true );
-               done();
        } );
 
 } );
@@ -244,12 +242,10 @@
 QUnit.test( 'RESTBase gateway does not treat a 404 as a failure', function ( 
assert ) {
        var deferred = $.Deferred(),
                api = this.sandbox.stub().returns( deferred.reject( { status: 
404 } ).promise() ),
-               gateway = createRESTBaseGateway( api, { THUMBNAIL_SIZE: 200 }, 
provideParsedExtract ),
-               done = assert.async( 1 );
+               gateway = createRESTBaseGateway( api, { THUMBNAIL_SIZE: 200 }, 
provideParsedExtract );
 
-       gateway.getPageSummary( 'Test Title' ).done( function () {
+       return gateway.getPageSummary( 'Test Title' ).done( function () {
                assert.ok( true );
-               done();
        } );
 } );
 
@@ -257,12 +253,10 @@
        var api = this.sandbox.stub().returns(
                        $.Deferred().resolve( RESTBASE_RESPONSE ).promise()
                ),
-               gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS, 
provideParsedExtract ),
-               done = assert.async( 1 );
+               gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS, 
provideParsedExtract );
 
-       gateway.getPageSummary( 'Test Title' ).done( function ( result ) {
+       return gateway.getPageSummary( 'Test Title' ).done( function ( result ) 
{
                assert.deepEqual( result, RESTBASE_RESPONSE_PREVIEW_MODEL );
-               done();
        } );
 } );
 
@@ -289,12 +283,9 @@
                api = this.sandbox.stub().returns(
                        $.Deferred().reject( response ).promise()
                ),
-               gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS, 
provideParsedExtract ),
-               done = assert.async( 1 );
+               gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS, 
provideParsedExtract );
 
-       gateway.getPageSummary( 'Missing Page' ).fail( function () {
+       return gateway.getPageSummary( 'Missing Page' ).catch( function () {
                assert.ok( true );
-
-               done();
        } );
 } );
diff --git a/tests/node-qunit/integration.test.js 
b/tests/node-qunit/integration.test.js
index dec663b..0be8023 100644
--- a/tests/node-qunit/integration.test.js
+++ b/tests/node-qunit/integration.test.js
@@ -185,50 +185,43 @@
 
 QUnit.test( 'in ACTIVE state, fetch end switches it to DATA', function ( 
assert ) {
        var store = this.store,
-               done = assert.async(),
                el = this.el;
 
-       this.dwellAndShowPreview( this.title, el, 'event', 42 ).then( function 
() {
+       return this.dwellAndShowPreview( this.title, el, 'event', 42 ).then( 
function () {
                var state = store.getState();
                assert.equal( state.preview.activeLink, el );
                assert.equal( state.preview.shouldShow, true, 'Should show when 
data has been fetched' );
-               done();
        } );
 } );
 
 QUnit.test( 'in ACTIVE state, abandon start, and then end, switch it to 
INACTIVE', function ( assert ) {
        var that = this,
-               done = assert.async(),
                el = this.el;
 
-       this.dwellAndShowPreview( this.title, el, 'event', 42 ).then( function 
() {
+       return this.dwellAndShowPreview( this.title, el, 'event', 42 ).then( 
function () {
                return that.abandonAndWait( el );
        } ).then( function () {
                var state = that.store.getState();
                assert.equal( state.preview.activeLink, undefined, 'After 
abandoning, preview is back to INACTIVE' );
-               done();
        } );
 } );
 
 QUnit.test( 'in ACTIVE state, abandon link, and then dwell preview, should 
keep it active after all delays', function ( assert ) {
        var that = this,
-               done = assert.async(),
                el = this.el;
 
-       this.dwellAndPreviewDwell( this.title, el, 'event', 42 )
+       return this.dwellAndPreviewDwell( this.title, el, 'event', 42 )
                .then( function () {
                        var state = that.store.getState();
                        assert.equal( state.preview.activeLink, el );
-                       done();
                } );
 } );
 
 QUnit.test( 'in ACTIVE state, abandon link, hover preview, back to link, 
should keep it active after all delays', function ( assert ) {
        var that = this,
-               done = assert.async(),
                el = this.el;
 
-       this.dwellAndPreviewDwell( this.title, el, 'event', 42 )
+       return this.dwellAndPreviewDwell( this.title, el, 'event', 42 )
                .then( function () {
                        var abandonPreviewDeferred, dwellPromise, dwellDeferred;
 
@@ -251,6 +244,5 @@
                .then( function () {
                        var state = that.store.getState();
                        assert.equal( state.preview.activeLink, el );
-                       done();
                } );
 } );
diff --git a/tests/node-qunit/ui/renderer.test.js 
b/tests/node-qunit/ui/renderer.test.js
index cfdf299..990cfb4 100644
--- a/tests/node-qunit/ui/renderer.test.js
+++ b/tests/node-qunit/ui/renderer.test.js
@@ -280,7 +280,6 @@
                behavior = createBehavior( this.sandbox ),
                token = 'some-token',
                $container = $( '<div>' ),
-               done = assert.async( 1 ),
                promise;
 
        preview.el.show = this.sandbox.stub();
@@ -298,12 +297,11 @@
                'Preview has been shown.'
        );
 
-       promise.done( function () {
+       return promise.done( function () {
                assert.ok(
                        behavior.previewShow.calledWith( token ),
                        'previewShow has been called with the correct token.'
                );
-               done();
        } );
 } );
 
@@ -314,7 +312,6 @@
                        thumbnail: null,
                        isTall: false
                },
-               done = assert.async( 1 ),
                $container = $( '<div>' ).append( preview.el ),
                promise = renderer.hide( preview );
 
@@ -331,13 +328,12 @@
                '',
                'Preview is still in the container.'
        );
-       promise.done( function () {
+       return promise.done( function () {
                assert.equal(
                        $container.html(),
                        '',
                        'Preview has been removed from the container.'
                );
-               done();
        } );
 } );
 
@@ -348,7 +344,6 @@
                        thumbnail: null,
                        isTall: false
                },
-               done = assert.async( 1 ),
                $container = $( '<div>' ).append( preview.el ),
                promise = renderer.hide( preview );
 
@@ -365,13 +360,12 @@
                '',
                'Preview is still in the container.'
        );
-       promise.done( function () {
+       return promise.done( function () {
                assert.equal(
                        $container.html(),
                        '',
                        'Preview has been removed from the container.'
                );
-               done();
        } );
 } );
 
diff --git a/tests/node-qunit/wait.test.js b/tests/node-qunit/wait.test.js
index 55f4889..318820b 100644
--- a/tests/node-qunit/wait.test.js
+++ b/tests/node-qunit/wait.test.js
@@ -3,8 +3,7 @@
 QUnit.module( 'ext.popups/wait' );
 
 QUnit.test( 'it should resolve after waiting', function ( assert ) {
-       var done = assert.async(),
-               timeout;
+       var timeout;
 
        assert.expect( 1 );
 
@@ -12,15 +11,13 @@
                callback();
        } );
 
-       wait( 150 ).done( function () {
+       return wait( 150 ).done( function () {
                assert.strictEqual(
                        timeout.getCall( 0 ).args[ 1 ],
                        150,
                        'It waits for the given duration'
                );
-
-               done();
+       } ).always( function () {
+               timeout.restore();
        } );
-
-       timeout.restore();
 } );

-- 
To view, visit https://gerrit.wikimedia.org/r/372173
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I37c4e3f86343052c946d8586f8ff840a81f631f8
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: Bmansurov <[email protected]>
Gerrit-Reviewer: Frankiebot <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Pmiazga <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to