3be3642d13bbd88e376dd57c4c7725fe7676e39b
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.test.js
1 ( function ( mw ) {
2 QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.clock = this.sandbox.useFakeTimers();
5 this.server = this.sandbox.useFakeServer();
6 },
7 teardown: function () {
8 this.clock.tick( 1 );
9 }
10 } ) );
11
12 QUnit.test( 'Basic functionality', function ( assert ) {
13 QUnit.expect( 2 );
14
15 var api = new mw.Api();
16
17 api.get( {} )
18 .done( function ( data ) {
19 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
20 } );
21
22 api.post( {} )
23 .done( function ( data ) {
24 assert.deepEqual( data, [], 'Simple POST request' );
25 } );
26
27 this.server.respond( function ( request ) {
28 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
29 } );
30 } );
31
32 QUnit.test( 'API error', function ( assert ) {
33 QUnit.expect( 1 );
34
35 var api = new mw.Api();
36
37 api.get( { action: 'doesntexist' } )
38 .fail( function ( errorCode ) {
39 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
40 } );
41
42 this.server.respond( function ( request ) {
43 request.respond( 200, { 'Content-Type': 'application/json' },
44 '{ "error": { "code": "unknown_action" } }'
45 );
46 } );
47 } );
48
49 QUnit.test( 'Deprecated callback methods', function ( assert ) {
50 QUnit.expect( 3 );
51
52 var api = new mw.Api();
53
54 api.get( {}, function () {
55 assert.ok( true, 'Function argument treated as success callback.' );
56 } );
57
58 api.get( {}, {
59 ok: function () {
60 assert.ok( true, '"ok" property treated as success callback.' );
61 }
62 } );
63
64 api.get( { action: 'doesntexist' }, {
65 err: function () {
66 assert.ok( true, '"err" property treated as error callback.' );
67 }
68 } );
69
70 this.server.respondWith( /action=query/, function ( request ) {
71 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
72 } );
73
74 this.server.respondWith( /action=doesntexist/, function ( request ) {
75 request.respond( 200, { 'Content-Type': 'application/json' },
76 '{ "error": { "code": "unknown_action" } }'
77 );
78 } );
79
80 this.server.respond();
81 } );
82
83 }( mediaWiki ) );