2ad762231a4cac9755982aaa1aca02d7358674a2
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' }, '[]' );
33 QUnit
.test( 'API error', function ( assert
) {
36 var api
= new mw
.Api();
38 api
.get( { action
: 'doesntexist' } )
39 .fail( function ( errorCode
) {
40 assert
.equal( errorCode
, 'unknown_action', 'API error should reject the deferred' );
43 this.server
.respond( function ( request
) {
44 request
.respond( 200, { 'Content-Type': 'application/json' },
45 '{ "error": { "code": "unknown_action" } }'
50 QUnit
.test( 'Deprecated callback methods', function ( assert
) {
53 var api
= new mw
.Api();
55 api
.get( {}, function () {
56 assert
.ok( true, 'Function argument treated as success callback.' );
61 assert
.ok( true, '"ok" property treated as success callback.' );
65 api
.get( { action
: 'doesntexist' }, {
67 assert
.ok( true, '"err" property treated as error callback.' );
71 this.server
.respondWith( /action=query/, function ( request
) {
72 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
75 this.server
.respondWith( /action=doesntexist/, function ( request
) {
76 request
.respond( 200, { 'Content-Type': 'application/json' },
77 '{ "error": { "code": "unknown_action" } }'
81 this.server
.respond();