QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
}
} ) );
QUnit.test( 'saveOption', function ( assert ) {
- QUnit.expect( 2 );
-
- var
- api = new mw.Api(),
+ var api = new mw.Api(),
stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' );
api.saveOption( 'foo', 'bar' );
} );
QUnit.test( 'saveOptions without Unit Separator', function ( assert ) {
- QUnit.expect( 13 );
-
var api = new mw.Api( { useUS: false } );
// We need to respond to the request for token first, otherwise the other requests won't be sent
'{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
);
- api.saveOptions( {} ).done( function () {
- assert.ok( true, 'Request completed: empty case' );
- } );
- api.saveOptions( { foo: 'bar' } ).done( function () {
- assert.ok( true, 'Request completed: simple' );
- } );
- api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
- assert.ok( true, 'Request completed: two options' );
- } );
- api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
- assert.ok( true, 'Request completed: not bundleable' );
- } );
- api.saveOptions( { foo: null } ).done( function () {
- assert.ok( true, 'Request completed: reset an option' );
- } );
- api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
- assert.ok( true, 'Request completed: reset an option, not bundleable' );
- } );
-
// Requests are POST, match requestBody instead of url
this.server.respond( function ( request ) {
- switch ( request.requestBody ) {
+ if ( $.inArray( request.requestBody, [
// simple
- case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
// two options
- case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
// not bundleable
- case 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C':
- case 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C':
- case 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C',
+ 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C',
+ 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C',
// reset an option
- case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
// reset an option, not bundleable
- case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
- assert.ok( true, 'Repond to ' + request.requestBody );
- request.respond( 200, { 'Content-Type': 'application/json' },
- '{ "options": "success" }' );
- break;
- default:
- assert.ok( false, 'Unexpected request: ' + request.requestBody );
+ 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
+ ] ) !== -1 ) {
+ assert.ok( true, 'Repond to ' + request.requestBody );
+ request.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "options": "success" }' );
+ } else {
+ assert.ok( false, 'Unexpected request: ' + request.requestBody );
}
} );
+
+ return QUnit.whenPromisesComplete(
+ api.saveOptions( {} ).then( function () {
+ assert.ok( true, 'Request completed: empty case' );
+ } ),
+ api.saveOptions( { foo: 'bar' } ).then( function () {
+ assert.ok( true, 'Request completed: simple' );
+ } ),
+ api.saveOptions( { foo: 'bar', baz: 'quux' } ).then( function () {
+ assert.ok( true, 'Request completed: two options' );
+ } ),
+ api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).then( function () {
+ assert.ok( true, 'Request completed: not bundleable' );
+ } ),
+ api.saveOptions( { foo: null } ).then( function () {
+ assert.ok( true, 'Request completed: reset an option' );
+ } ),
+ api.saveOptions( { 'foo|bar=quux': null } ).then( function () {
+ assert.ok( true, 'Request completed: reset an option, not bundleable' );
+ } )
+ );
} );
QUnit.test( 'saveOptions with Unit Separator', function ( assert ) {
- QUnit.expect( 14 );
-
var api = new mw.Api( { useUS: true } );
// We need to respond to the request for token first, otherwise the other requests won't be sent
'{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
);
- api.saveOptions( {} ).done( function () {
- assert.ok( true, 'Request completed: empty case' );
- } );
- api.saveOptions( { foo: 'bar' } ).done( function () {
- assert.ok( true, 'Request completed: simple' );
- } );
- api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
- assert.ok( true, 'Request completed: two options' );
- } );
- api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
- assert.ok( true, 'Request completed: bundleable with unit separator' );
- } );
- api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', 'baz=baz': 'quux' } ).done( function () {
- assert.ok( true, 'Request completed: not bundleable with unit separator' );
- } );
- api.saveOptions( { foo: null } ).done( function () {
- assert.ok( true, 'Request completed: reset an option' );
- } );
- api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
- assert.ok( true, 'Request completed: reset an option, not bundleable' );
- } );
-
// Requests are POST, match requestBody instead of url
this.server.respond( function ( request ) {
- switch ( request.requestBody ) {
+ if ( $.inArray( request.requestBody, [
// simple
- case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
// two options
- case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
// bundleable with unit separator
- case 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc%1Fbaz%3Dquux&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc%1Fbaz%3Dquux&token=%2B%5C',
// not bundleable with unit separator
- case 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C':
- case 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C',
+ 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C',
// reset an option
- case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
+ 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
// reset an option, not bundleable
- case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
- assert.ok( true, 'Repond to ' + request.requestBody );
- request.respond( 200, { 'Content-Type': 'application/json' },
+ 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
+ ] ) !== -1 ) {
+ assert.ok( true, 'Repond to ' + request.requestBody );
+ request.respond( 200, { 'Content-Type': 'application/json' },
'{ "options": "success" }' );
- break;
- default:
- assert.ok( false, 'Unexpected request: ' + request.requestBody );
+ } else {
+ assert.ok( false, 'Unexpected request: ' + request.requestBody );
}
} );
+
+ return QUnit.whenPromisesComplete(
+ api.saveOptions( {} ).done( function () {
+ assert.ok( true, 'Request completed: empty case' );
+ } ),
+ api.saveOptions( { foo: 'bar' } ).done( function () {
+ assert.ok( true, 'Request completed: simple' );
+ } ),
+ api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
+ assert.ok( true, 'Request completed: two options' );
+ } ),
+ api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
+ assert.ok( true, 'Request completed: bundleable with unit separator' );
+ } ),
+ api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', 'baz=baz': 'quux' } ).done( function () {
+ assert.ok( true, 'Request completed: not bundleable with unit separator' );
+ } ),
+ api.saveOptions( { foo: null } ).done( function () {
+ assert.ok( true, 'Request completed: reset an option' );
+ } ),
+ api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
+ assert.ok( true, 'Request completed: reset an option, not bundleable' );
+ } )
+ );
} );
}( mediaWiki ) );