diff options
Diffstat (limited to 'tests/qunit/suites/resources/mediawiki.api')
-rw-r--r-- | tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js | 78 | ||||
-rw-r--r-- | tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js | 62 |
2 files changed, 107 insertions, 33 deletions
diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js new file mode 100644 index 00000000..c0a6585f --- /dev/null +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js @@ -0,0 +1,78 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + } + } ) ); + + QUnit.test( 'saveOption', function ( assert ) { + QUnit.expect( 2 ); + + var + api = new mw.Api(), + stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' ); + + api.saveOption( 'foo', 'bar' ); + + assert.ok( stub.calledOnce, '#saveOptions called once' ); + assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' } ], '#saveOptions called correctly' ); + } ); + + QUnit.test( 'saveOptions', function ( assert ) { + QUnit.expect( 13 ); + + var api = new mw.Api(); + + // We need to respond to the request for token first, otherwise the other requests won't be sent + // until after the server.respond call, which confuses sinon terribly. This sucks a lot. + api.getToken( 'options' ); + this.server.respond( + /action=tokens.*&type=options/, + [ 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "optionstoken": "+\\\\" } }' ] + ); + + 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 ) { + // simple + case 'action=options&format=json&change=foo%3Dbar&token=%2B%5C': + // two options + case 'action=options&format=json&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C': + // not bundleable + case 'action=options&format=json&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C': + case 'action=options&format=json&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C': + case 'action=options&format=json&change=baz%3Dquux&token=%2B%5C': + // reset an option + case 'action=options&format=json&change=foo&token=%2B%5C': + // reset an option, not bundleable + case 'action=options&format=json&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 ); + } + } ); + } ); +}( mediaWiki ) ); 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 f156c728..b89526fb 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -51,52 +51,26 @@ this.server.respond( function ( request ) { if ( window.FormData ) { - assert.ok( !request.url.match( /action=/), 'Request has no query string' ); + assert.ok( !request.url.match( /action=/ ), 'Request has no query string' ); assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' ); } else { - assert.ok( !request.url.match( /action=test/), 'Request has no query string' ); + assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' ); assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' ); } request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); } ); } ); - QUnit.test( 'Deprecated callback methods', function ( assert ) { - QUnit.expect( 3 ); + QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) { + QUnit.expect( 1 ); var api = new mw.Api(); + api.get( { test: [ 'foo', 'bar', 'baz' ] } ); - this.suppressWarnings(); - - api.get( {}, function () { - assert.ok( true, 'Function argument treated as success callback.' ); - } ); - - api.get( {}, { - ok: function () { - assert.ok( true, '"ok" property treated as success callback.' ); - } - } ); - - api.get( { action: 'doesntexist' }, { - err: function () { - assert.ok( true, '"err" property treated as error callback.' ); - } - } ); - - this.restoreWarnings(); - - this.server.respondWith( /action=query/, function ( request ) { + this.server.respond( function ( request ) { + assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' ); 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(); } ); QUnit.test( 'getToken( pre-populated )', function ( assert ) { @@ -196,6 +170,28 @@ ); } ); + QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) { + QUnit.expect( 2 ); + + var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); + + api.postWithToken( 'testasserttoken', { action: 'example', key: 'foo', assert: 'user' } ) + .fail( function ( errorCode ) { + assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' ); + } ); + + assert.equal( this.server.requests.length, 1, 'Request for token made' ); + this.server.respondWith( /assert=user/, function ( request ) { + request.respond( + 200, + { 'Content-Type': 'application/json' }, + '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }' + ); + } ); + + this.server.respond(); + } ); + QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) { QUnit.expect( 3 ); |