[MediaWiki-commits] [Gerrit] mediawiki.api: Refactor to use server.respondImmediately - change (mediawiki/core)

2015-07-08 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: mediawiki.api: Refactor to use server.respondImmediately
..


mediawiki.api: Refactor to use server.respondImmediately

* Simplifies code a lot and removes the need to explicitly flush
  and handle the request queue with respond().
  This was especially annoying when a request spawned others as
  they wouldn't be in the queue yet.

* Make tests more explicit and resilient by specifying what they
  respond to instead of assigning to requests[i] directly.
  This also makes the failure better if one request isn't made,
  instead of throwing for accessing properties on undefined objects.

* Avoid relying on test order for badToken().
  Follows-up 7b05096bcae0. Tests should be atomic and not rely on
  order. This is already important as QUnit re-orders failed tests.
  And in the future they may run concurrently.

  Resolve using a unique name (like the other tests).

  Also, the previous test wasn't asserting that badToken() works,
  it was merely asserting that getToken() works and that it wasn't
  yet cached. The new test will fetch and purge its own token.

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

Approvals:
  Bartosz DziewoƄski: 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 4f199bd..de79198 100644
--- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
@@ -2,13 +2,38 @@
QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+   this.server.respondImmediately = true;
+   this.clock = this.sandbox.useFakeTimers();
+   },
+   teardown: function () {
+   // https://github.com/jquery/jquery/issues/2453
+   this.clock.tick();
}
} ) );
 
+   function sequence( responses ) {
+   var i = 0;
+   return function ( request ) {
+   var response = responses[i];
+   if ( response ) {
+   i++;
+   request.respond.apply( request, response );
+   }
+   };
+   }
+
+   function sequenceBodies( status, headers, bodies ) {
+   jQuery.each( bodies, function ( i, body ) {
+   bodies[i] = [ status, headers, body ];
+   } );
+   return sequence( bodies );
+   }
+
QUnit.test( 'Basic functionality', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api();
+
+   this.server.respond( [ 200, { 'Content-Type': 
'application/json' }, '[]' ] );
 
api.get( {} )
.done( function ( data ) {
@@ -19,35 +44,25 @@
.done( function ( data ) {
assert.deepEqual( data, [], 'Simple POST 
request' );
} );
-
-   this.server.respond( function ( request ) {
-   request.respond( 200, { 'Content-Type': 
'application/json' }, '[]' );
-   } );
} );
 
QUnit.test( 'API error', function ( assert ) {
QUnit.expect( 1 );
-
var api = new mw.Api();
+
+   this.server.respond( [ 200, { 'Content-Type': 
'application/json' },
+   '{ error: { code: unknown_action } }'
+   ] );
 
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( 'FormData support', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api();
-
-   api.post( { action: 'test' }, { contentType: 
'multipart/form-data' } );
 
this.server.respond( function ( request ) {
if ( window.FormData ) {
@@ -59,28 +74,28 @@
}
request.respond( 200, { 'Content-Type': 
'application/json' }, '[]' );

[MediaWiki-commits] [Gerrit] mediawiki.api: Refactor to use server.respondImmediately - change (mediawiki/core)

2015-07-07 Thread Krinkle (Code Review)
Krinkle has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/223316

Change subject: mediawiki.api: Refactor to use server.respondImmediately
..

mediawiki.api: Refactor to use server.respondImmediately

* Simplifies code a lot and removes the need to explicitly flush
  and handle the request queue with respond().
  This was especially annoying when a request spawned others as
  they wouldn't be in the queue yet.

* Make tests more explicit and resilient by specifying what they
  respond to instead of assingning to requests[i] directly.
  This also makes the failure better if one request isn't made,
  instead of throwing for accessing properties on undefined objects.

* Avoid relying on test order for badToken().
  Follows-up 7b05096bcae0. Tests should be atomic and not rely on
  order. This is already important as QUnit re-orders failed tests.
  And in the future they may run concurrently.

  Resolve using a unique name (like the other tests).

  Also, the previous test wasn't asserting that badToken works,
  it was merely asserting that getToken() works and that it wasn't
  yet cached. The new test will fetch and purge its own token.

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


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/16/223316/1

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 4f199bd..de79198 100644
--- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
@@ -2,13 +2,38 @@
QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+   this.server.respondImmediately = true;
+   this.clock = this.sandbox.useFakeTimers();
+   },
+   teardown: function () {
+   // https://github.com/jquery/jquery/issues/2453
+   this.clock.tick();
}
} ) );
 
+   function sequence( responses ) {
+   var i = 0;
+   return function ( request ) {
+   var response = responses[i];
+   if ( response ) {
+   i++;
+   request.respond.apply( request, response );
+   }
+   };
+   }
+
+   function sequenceBodies( status, headers, bodies ) {
+   jQuery.each( bodies, function ( i, body ) {
+   bodies[i] = [ status, headers, body ];
+   } );
+   return sequence( bodies );
+   }
+
QUnit.test( 'Basic functionality', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api();
+
+   this.server.respond( [ 200, { 'Content-Type': 
'application/json' }, '[]' ] );
 
api.get( {} )
.done( function ( data ) {
@@ -19,35 +44,25 @@
.done( function ( data ) {
assert.deepEqual( data, [], 'Simple POST 
request' );
} );
-
-   this.server.respond( function ( request ) {
-   request.respond( 200, { 'Content-Type': 
'application/json' }, '[]' );
-   } );
} );
 
QUnit.test( 'API error', function ( assert ) {
QUnit.expect( 1 );
-
var api = new mw.Api();
+
+   this.server.respond( [ 200, { 'Content-Type': 
'application/json' },
+   '{ error: { code: unknown_action } }'
+   ] );
 
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( 'FormData support', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api();
-
-   api.post( { action: 'test' }, { contentType: 
'multipart/form-data' } );
 
this.server.respond( function ( request ) {
if ( window.FormData ) {
@@ -59,28 +74,28 @@
}
request.respond( 200, { 'Content-Type':