Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.watch.test.js
1 ( function ( mw ) {
2 QUnit.module( 'mediawiki.api.watch', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 }
6 } ) );
7
8 QUnit.test( '.watch()', function ( assert ) {
9 QUnit.expect( 4 );
10
11 var api = new mw.Api();
12
13 // Ensure we don't mistake a single item array for a single item and vice versa.
14 // The query parameter in request is the same either way (separated by pipe).
15 api.watch( 'Foo' ).done( function ( item ) {
16 assert.equal( item.title, 'Foo' );
17 } );
18
19 api.watch( [ 'Foo' ] ).done( function ( items ) {
20 assert.equal( items[ 0 ].title, 'Foo' );
21 } );
22
23 api.watch( [ 'Foo', 'Bar' ] ).done( function ( items ) {
24 assert.equal( items[ 0 ].title, 'Foo' );
25 assert.equal( items[ 1 ].title, 'Bar' );
26 } );
27
28 // Requests are POST, match requestBody instead of url
29 this.server.respond( function ( req ) {
30 if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) {
31 req.respond( 200, { 'Content-Type': 'application/json' },
32 '{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }'
33 );
34 }
35
36 if ( /action=watch.*&titles=Foo%7CBar/.test( req.requestBody ) ) {
37 req.respond( 200, { 'Content-Type': 'application/json' },
38 '{ "watch": [ ' +
39 '{ "title": "Foo", "watched": true, "message": "<b>Added</b>" },' +
40 '{ "title": "Bar", "watched": true, "message": "<b>Added</b>" }' +
41 '] }'
42 );
43 }
44 } );
45 } );
46 }( mediaWiki ) );