Merge "Add test to validate special page aliases"
[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
33 QUnit.test( 'API error', function ( assert ) {
34 QUnit.expect( 1 );
35
36 var api = new mw.Api();
37
38 api.get( { action: 'doesntexist' } )
39 .fail( function ( errorCode ) {
40 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
41 } );
42
43 this.server.respond( function ( request ) {
44 request.respond( 200, { 'Content-Type': 'application/json' },
45 '{ "error": { "code": "unknown_action" } }'
46 );
47 } );
48 } );
49
50 QUnit.test( 'Deprecated callback methods', function ( assert ) {
51 QUnit.expect( 3 );
52
53 var api = new mw.Api();
54
55 api.get( {}, function () {
56 assert.ok( true, 'Function argument treated as success callback.' );
57 } );
58
59 api.get( {}, {
60 ok: function () {
61 assert.ok( true, '"ok" property treated as success callback.' );
62 }
63 } );
64
65 api.get( { action: 'doesntexist' }, {
66 err: function () {
67 assert.ok( true, '"err" property treated as error callback.' );
68 }
69 } );
70
71 this.server.respondWith( /action=query/, function ( request ) {
72 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
73 } );
74
75 this.server.respondWith( /action=doesntexist/, function ( request ) {
76 request.respond( 200, { 'Content-Type': 'application/json' },
77 '{ "error": { "code": "unknown_action" } }'
78 );
79 } );
80
81 this.server.respond();
82 } );
83
84 }( mediaWiki ) );