2 QUnit
.module( 'mediawiki.api.options', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
8 QUnit
.test( 'saveOption', function ( assert
) {
13 stub
= this.sandbox
.stub( mw
.Api
.prototype, 'saveOptions' );
15 api
.saveOption( 'foo', 'bar' );
17 assert
.ok( stub
.calledOnce
, '#saveOptions called once' );
18 assert
.deepEqual( stub
.getCall( 0 ).args
, [ { foo
: 'bar' } ], '#saveOptions called correctly' );
21 QUnit
.test( 'saveOptions', function ( assert
) {
24 var api
= new mw
.Api();
26 // We need to respond to the request for token first, otherwise the other requests won't be sent
27 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
28 api
.getToken( 'options' );
30 /meta=tokens&type=csrf/,
31 [ 200, { 'Content-Type': 'application/json' },
32 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
35 api
.saveOptions( {} ).done( function () {
36 assert
.ok( true, 'Request completed: empty case' );
38 api
.saveOptions( { foo
: 'bar' } ).done( function () {
39 assert
.ok( true, 'Request completed: simple' );
41 api
.saveOptions( { foo
: 'bar', baz
: 'quux' } ).done( function () {
42 assert
.ok( true, 'Request completed: two options' );
44 api
.saveOptions( { foo
: 'bar|quux', bar
: 'a|b|c', baz
: 'quux' } ).done( function () {
45 assert
.ok( true, 'Request completed: not bundleable' );
47 api
.saveOptions( { foo
: null } ).done( function () {
48 assert
.ok( true, 'Request completed: reset an option' );
50 api
.saveOptions( { 'foo|bar=quux': null } ).done( function () {
51 assert
.ok( true, 'Request completed: reset an option, not bundleable' );
54 // Requests are POST, match requestBody instead of url
55 this.server
.respond( function ( request
) {
56 switch ( request
.requestBody
) {
58 case 'action=options&format=json&change=foo%3Dbar&token=%2B%5C':
60 case 'action=options&format=json&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
62 case 'action=options&format=json&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C':
63 case 'action=options&format=json&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C':
64 case 'action=options&format=json&change=baz%3Dquux&token=%2B%5C':
66 case 'action=options&format=json&change=foo&token=%2B%5C':
67 // reset an option, not bundleable
68 case 'action=options&format=json&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
69 assert
.ok( true, 'Repond to ' + request
.requestBody
);
70 request
.respond( 200, { 'Content-Type': 'application/json' },
71 '{ "options": "success" }' );
74 assert
.ok( false, 'Unexpected request: ' + request
.requestBody
);