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();
}
} ) );
- QUnit.test( 'origin is included in GET requests', function ( assert ) {
- QUnit.expect( 1 );
+ QUnit.test( 'origin is included in GET requests', 1, function ( assert ) {
var api = new mw.ForeignApi( '//localhost:4242/w/api.php' );
this.server.respond( function ( request ) {
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
} );
- api.get( {} );
+ return api.get( {} );
} );
- QUnit.test( 'origin is included in POST requests', function ( assert ) {
- QUnit.expect( 2 );
+ QUnit.test( 'origin is included in POST requests', 2, function ( assert ) {
var api = new mw.ForeignApi( '//localhost:4242/w/api.php' );
this.server.respond( function ( request ) {
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
} );
- api.post( {} );
+ return api.post( {} );
} );
}( mediaWiki ) );
QUnit.module( 'mediawiki.api.category', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
}
} ) );
- QUnit.test( '.getCategoriesByPrefix()', function ( assert ) {
- QUnit.expect( 1 );
+ QUnit.test( '.getCategoriesByPrefix()', 1, function ( assert ) {
+ this.server.respondWith( [ 200, { 'Content-Type': 'application/json' },
+ '{ "query": { "allpages": [ ' +
+ '{ "title": "Category:Food" },' +
+ '{ "title": "Category:Fool Supermarine S.6" },' +
+ '{ "title": "Category:Fools" }' +
+ '] } }'
+ ] );
- var api = new mw.Api();
-
- api.getCategoriesByPrefix( 'Foo' ).done( function ( matches ) {
+ return new mw.Api().getCategoriesByPrefix( 'Foo' ).then( function ( matches ) {
assert.deepEqual(
matches,
[ 'Food', 'Fool Supermarine S.6', 'Fools' ]
);
} );
-
- this.server.respond( function ( req ) {
- req.respond( 200, { 'Content-Type': 'application/json' },
- '{ "query": { "allpages": [ ' +
- '{ "title": "Category:Food" },' +
- '{ "title": "Category:Fool Supermarine S.6" },' +
- '{ "title": "Category:Fools" }' +
- '] } }'
- );
- } );
} );
}( mediaWiki ) );
QUnit.module( 'mediawiki.api.messages', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
}
} ) );
- QUnit.test( '.getMessages()', function ( assert ) {
- QUnit.expect( 1 );
+ QUnit.test( '.getMessages()', 1, function ( assert ) {
+ this.server.respondWith( /ammessages=foo%7Cbaz/, [
+ 200,
+ { 'Content-Type': 'application/json' },
+ '{ "query": { "allmessages": [' +
+ '{ "name": "foo", "content": "Foo bar" },' +
+ '{ "name": "baz", "content": "Baz Quux" }' +
+ '] } }'
+ ] );
- var api = new mw.Api();
- api.getMessages( [ 'foo', 'baz' ] ).then( function ( messages ) {
+ return new mw.Api().getMessages( [ 'foo', 'baz' ] ).then( function ( messages ) {
assert.deepEqual(
messages,
{
}
);
} );
-
- this.server.respond( /ammessages=foo%7Cbaz/, [
- 200,
- { 'Content-Type': 'application/json' },
- '{ "query": { "allmessages": [' +
- '{ "name": "foo", "content": "Foo bar" },' +
- '{ "name": "baz", "content": "Baz Quux" }' +
- '] } }'
- ] );
} );
}( mediaWiki ) );
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(),
+ QUnit.test( 'saveOption', 2, function ( assert ) {
+ var api = new mw.Api(),
stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' );
api.saveOption( 'foo', 'bar' );
assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' } ], '#saveOptions called correctly' );
} );
- QUnit.test( 'saveOptions without Unit Separator', function ( assert ) {
- QUnit.expect( 13 );
-
+ QUnit.test( 'saveOptions without Unit Separator', 13, function ( assert ) {
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 ) {
assert.ok( false, 'Unexpected request: ' + request.requestBody );
}
} );
- } );
- QUnit.test( 'saveOptions with Unit Separator', function ( assert ) {
- QUnit.expect( 14 );
+ 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', 14, function ( assert ) {
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 ) {
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 ) );
QUnit.module( 'mediawiki.api.parse', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
}
} ) );
- QUnit.test( 'Hello world', function ( assert ) {
- QUnit.expect( 3 );
+ QUnit.test( '.parse( string )', function ( assert ) {
+ this.server.respondWith( /action=parse.*&text='''Hello(\+|%20)world'''/, [ 200,
+ { 'Content-Type': 'application/json' },
+ '{ "parse": { "text": "<p><b>Hello world</b></p>" } }'
+ ] );
- var api = new mw.Api();
-
- api.parse( '\'\'\'Hello world\'\'\'' ).done( function ( html ) {
+ return new mw.Api().parse( '\'\'\'Hello world\'\'\'' ).done( function ( html ) {
assert.equal( html, '<p><b>Hello world</b></p>', 'Parse wikitext by string' );
} );
+ } );
- api.parse( {
+ QUnit.test( '.parse( Object.toString )', function ( assert ) {
+ this.server.respondWith( /action=parse.*&text='''Hello(\+|%20)world'''/, [ 200,
+ { 'Content-Type': 'application/json' },
+ '{ "parse": { "text": "<p><b>Hello world</b></p>" } }'
+ ] );
+
+ return new mw.Api().parse( {
toString: function () {
return '\'\'\'Hello world\'\'\'';
}
} ).done( function ( html ) {
assert.equal( html, '<p><b>Hello world</b></p>', 'Parse wikitext by toString object' );
} );
+ } );
- this.server.respondWith( /action=parse.*&text='''Hello\+world'''/, function ( request ) {
- request.respond( 200, { 'Content-Type': 'application/json' },
- '{ "parse": { "text": "<p><b>Hello world</b></p>" } }'
- );
- } );
+ QUnit.test( '.parse( mw.Title )', function ( assert ) {
+ this.server.respondWith( /action=parse.*&page=Earth/, [ 200,
+ { 'Content-Type': 'application/json' },
+ '{ "parse": { "text": "<p><b>Earth</b> is a planet.</p>" } }'
+ ] );
- api.parse( new mw.Title( 'Earth' ) ).done( function ( html ) {
+ return new mw.Api().parse( new mw.Title( 'Earth' ) ).done( function ( html ) {
assert.equal( html, '<p><b>Earth</b> is a planet.</p>', 'Parse page by Title object' );
} );
-
- this.server.respondWith( /action=parse.*&page=Earth/, function ( request ) {
- request.respond( 200, { 'Content-Type': 'application/json' },
- '{ "parse": { "text": "<p><b>Earth</b> is a planet.</p>" } }'
- );
- } );
-
- this.server.respond();
} );
}( mediaWiki ) );
( function ( mw, $ ) {
QUnit.module( 'mediawiki.api.upload', QUnit.newMwEnvironment( {} ) );
- QUnit.test( 'Basic functionality', function ( assert ) {
- QUnit.expect( 2 );
+ QUnit.test( 'Basic functionality', 2, function ( assert ) {
var api = new mw.Api();
assert.ok( api.upload );
assert.throws( function () {
} );
} );
- QUnit.test( 'Set up iframe upload', function ( assert ) {
- QUnit.expect( 5 );
+ QUnit.test( 'Set up iframe upload', 5, function ( assert ) {
var $iframe, $form, $input,
api = new mw.Api();
QUnit.module( 'mediawiki.api.watch', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
}
} ) );
- QUnit.test( '.watch()', function ( assert ) {
- QUnit.expect( 4 );
-
- var api = new mw.Api();
-
- // Ensure we don't mistake a single item array for a single item and vice versa.
- // The query parameter in request is the same either way (separated by pipe).
- api.watch( 'Foo' ).done( function ( item ) {
- assert.equal( item.title, 'Foo' );
- } );
-
- api.watch( [ 'Foo' ] ).done( function ( items ) {
- assert.equal( items[ 0 ].title, 'Foo' );
+ QUnit.test( '.watch( string )', function ( assert ) {
+ this.server.respond( function ( req ) {
+ // Match POST requestBody
+ if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) {
+ req.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }'
+ );
+ }
} );
- api.watch( [ 'Foo', 'Bar' ] ).done( function ( items ) {
- assert.equal( items[ 0 ].title, 'Foo' );
- assert.equal( items[ 1 ].title, 'Bar' );
+ return new mw.Api().watch( 'Foo' ).done( function ( item ) {
+ assert.equal( item.title, 'Foo' );
} );
+ } );
- // Requests are POST, match requestBody instead of url
+ // Ensure we don't mistake a single item array for a single item and vice versa.
+ // The query parameter in request is the same either way (separated by pipe).
+ QUnit.test( '.watch( Array ) - single', function ( assert ) {
this.server.respond( function ( req ) {
+ // Match POST requestBody
if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) {
req.respond( 200, { 'Content-Type': 'application/json' },
'{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }'
);
}
+ } );
+
+ return new mw.Api().watch( [ 'Foo' ] ).done( function ( items ) {
+ assert.equal( items[ 0 ].title, 'Foo' );
+ } );
+ } );
+ QUnit.test( '.watch( Array ) - multi', function ( assert ) {
+ this.server.respond( function ( req ) {
+ // Match POST requestBody
if ( /action=watch.*&titles=Foo%7CBar/.test( req.requestBody ) ) {
req.respond( 200, { 'Content-Type': 'application/json' },
'{ "watch": [ ' +
);
}
} );
+
+ return new mw.Api().watch( [ 'Foo', 'Bar' ] ).done( function ( items ) {
+ assert.equal( items[ 0 ].title, 'Foo' );
+ assert.equal( items[ 1 ].title, 'Bar' );
+ } );
} );
+
}( mediaWiki ) );
QUnit.test( 'Match PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
mw.messages.set( mw.libs.phpParserData.messages );
var tasks = $.map( mw.libs.phpParserData.tests, function ( test ) {
+ var done = assert.async();
return function ( next, abort ) {
- var done = assert.async();
getMwLanguage( test.lang )
.then( function ( langClass ) {
mw.config.set( 'wgUserLanguage', test.lang );
mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
var queue = $.map( formatnumTests, function ( test ) {
+ var done = assert.async();
return function ( next, abort ) {
- var done = assert.async();
getMwLanguage( test.lang )
.then( function ( langClass ) {
mw.config.set( 'wgUserLanguage', test.lang );
.done( function () {
assert.strictEqual( isAwesomeDone, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
delete mw.loader.testCallback;
-
} )
.fail( function () {
assert.ok( false, 'Error callback fired while loader.using "test.promise" module' );
} );
QUnit.test( '.using() Error: Circular dependency', function ( assert ) {
+ var done = assert.async();
+
mw.loader.register( [
[ 'test.circle1', '0', [ 'test.circle2' ] ],
[ 'test.circle2', '0', [ 'test.circle3' ] ],
function fail( e ) {
assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
}
- );
+ )
+ .always( done );
} );
QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
} );
QUnit.test( '.using() - Error: Unregistered', function ( assert ) {
+ var done = assert.async();
+
mw.loader.using( 'test.using.unreg' ).then(
function done() {
assert.ok( false, 'Unexpected resolution, expected error.' );
function fail( e ) {
assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' );
}
- );
+ ).always( done );
} );
QUnit.test( '.load() - Error: Unregistered (ignored)', 0, function ( assert ) {