jenkins-bot has submitted this change and it was merged.

Change subject: mediawiki.api.test: Use sinon sandbox for unit tests
......................................................................


mediawiki.api.test: Use sinon sandbox for unit tests

Make the unit tests faster and more standalone:

* Don't make a request to the actual API, instead provide the
  response via the fake server and purely test the mw.Api interface.

* No need for promise aggregration since the process is now
  synchronous.

* No arbitrary delays for async or animations etc. as sinon
  artificially fast-forwards setTimeout, Date and others for us.

Also:

* Move assertion for API error handling to separate test.

Change-Id: I20b17f256bc5114c6c2c3185653973076c15bc02
---
M tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
1 file changed, 50 insertions(+), 27 deletions(-)

Approvals:
  Ori.livneh: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js 
b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
index 9eda75c..c903193 100644
--- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
@@ -1,61 +1,84 @@
 ( function ( mw ) {
-       QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment() );
+       QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
+               setup: function () {
+                       this.clock = this.sandbox.useFakeTimers();
+                       this.server = this.sandbox.useFakeServer();
+               },
+               teardown: function () {
+                       this.clock.tick( 1 );
+               }
+       }) );
 
-       QUnit.asyncTest( 'Basic functionality', function ( assert ) {
-               var api, d1, d2, d3;
-               QUnit.expect( 3 );
+       QUnit.test( 'Basic functionality', function ( assert ) {
+               QUnit.expect( 2 );
 
-               api = new mw.Api();
+               var api = new mw.Api();
 
-               d1 = api.get( {} )
+               api.get( {} )
                        .done( function ( data ) {
                                assert.deepEqual( data, [], 'If request 
succeeds without errors, resolve deferred' );
                        } );
 
-               d2 = api.get( {
-                       action: 'doesntexist'
-               } )
-                       .fail( function ( errorCode ) {
-                               assert.equal( errorCode, 'unknown_action', 'API 
error (e.g. "unknown_action") should reject the deferred' );
-                       } );
-
-               d3 = api.post( {} )
+               api.post( {} )
                        .done( function ( data ) {
                                assert.deepEqual( data, [], 'Simple POST 
request' );
                        } );
 
-               // After all are completed, continue the test suite.
-               QUnit.whenPromisesComplete( d1, d2, d3 ).always( function () {
-                       QUnit.start();
+               this.server.respond( function ( request ) {
+                       request.respond( 200, { 'Content-Type': 
'application/json' }, '[]' );
                } );
        } );
 
-       QUnit.asyncTest( 'Deprecated callback methods', function ( assert ) {
-               var api, d1, d2, d3;
+
+       QUnit.test( 'API error', function ( assert ) {
+               QUnit.expect( 1 );
+
+               var api = new mw.Api();
+
+               api.get( { action: 'doesntexist' } )
+                       .fail( function ( errorCode ) {
+                               assert.equal( errorCode, 'unknown_action', 'API 
error should reject the deferred' );
+                       } );
+
+               this.server.respond( function ( request ) {
+                       request.respond( 200, { 'Content-Type': 
'application/json' },
+                               '{ "error": { "code": "unknown_action" } }'
+                       );
+               } );
+       } );
+
+       QUnit.test( 'Deprecated callback methods', function ( assert ) {
                QUnit.expect( 3 );
 
-               api = new mw.Api();
+               var api = new mw.Api();
 
-               d1 = api.get( {}, function () {
+               api.get( {}, function () {
                        assert.ok( true, 'Function argument treated as success 
callback.' );
                } );
 
-               d2 = api.get( {}, {
+               api.get( {}, {
                        ok: function () {
                                assert.ok( true, '"ok" property treated as 
success callback.' );
                        }
                } );
 
-               d3 = api.get( {
-                       action: 'doesntexist'
-               }, {
+               api.get( { action: 'doesntexist' }, {
                        err: function () {
                                assert.ok( true, '"err" property treated as 
error callback.' );
                        }
                } );
 
-               QUnit.whenPromisesComplete( d1, d2, d3 ).always( function () {
-                       QUnit.start();
+               this.server.respondWith( /action=query/, function ( request ) {
+                       request.respond( 200, { 'Content-Type': 
'application/json' }, '[]' );
                } );
+
+               this.server.respondWith( /action=doesntexist/, function ( 
request ) {
+                       request.respond( 200, { 'Content-Type': 
'application/json' },
+                               '{ "error": { "code": "unknown_action" } }'
+                       );
+               } );
+
+               this.server.respond();
        } );
+
 }( mediaWiki ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I20b17f256bc5114c6c2c3185653973076c15bc02
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Alex Monk <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Bartosz DziewoƄski <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to