2 QUnit
.module( 'mediawiki.api', QUnit
.newMwEnvironment( {
4 this.clock
= this.sandbox
.useFakeTimers();
5 this.server
= this.sandbox
.useFakeServer();
7 teardown: function () {
12 QUnit
.test( 'Basic functionality', function ( assert
) {
15 var api
= new mw
.Api();
18 .done( function ( data
) {
19 assert
.deepEqual( data
, [], 'If request succeeds without errors, resolve deferred' );
23 .done( function ( data
) {
24 assert
.deepEqual( data
, [], 'Simple POST request' );
27 this.server
.respond( function ( request
) {
28 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
32 QUnit
.test( 'API error', function ( assert
) {
35 var api
= new mw
.Api();
37 api
.get( { action
: 'doesntexist' } )
38 .fail( function ( errorCode
) {
39 assert
.equal( errorCode
, 'unknown_action', 'API error should reject the deferred' );
42 this.server
.respond( function ( request
) {
43 request
.respond( 200, { 'Content-Type': 'application/json' },
44 '{ "error": { "code": "unknown_action" } }'
49 QUnit
.test( 'Deprecated callback methods', function ( assert
) {
52 var api
= new mw
.Api();
54 api
.get( {}, function () {
55 assert
.ok( true, 'Function argument treated as success callback.' );
60 assert
.ok( true, '"ok" property treated as success callback.' );
64 api
.get( { action
: 'doesntexist' }, {
66 assert
.ok( true, '"err" property treated as error callback.' );
70 this.server
.respondWith( /action=query/, function ( request
) {
71 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
74 this.server
.respondWith( /action=doesntexist/, function ( request
) {
75 request
.respond( 200, { 'Content-Type': 'application/json' },
76 '{ "error": { "code": "unknown_action" } }'
80 this.server
.respond();