Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.watch.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.api.watch', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 }
7 } ) );
8
9 QUnit.test( '.watch( string )', function ( assert ) {
10 this.server.respond( function ( req ) {
11 // Match POST requestBody
12 if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) {
13 req.respond( 200, { 'Content-Type': 'application/json' },
14 '{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }'
15 );
16 }
17 } );
18
19 return new mw.Api().watch( 'Foo' ).done( function ( item ) {
20 assert.strictEqual( item.title, 'Foo' );
21 } );
22 } );
23
24 // Ensure we don't mistake a single item array for a single item and vice versa.
25 // The query parameter in request is the same either way (separated by pipe).
26 QUnit.test( '.watch( Array ) - single', function ( assert ) {
27 this.server.respond( function ( req ) {
28 // Match POST requestBody
29 if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) {
30 req.respond( 200, { 'Content-Type': 'application/json' },
31 '{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }'
32 );
33 }
34 } );
35
36 return new mw.Api().watch( [ 'Foo' ] ).done( function ( items ) {
37 assert.strictEqual( items[ 0 ].title, 'Foo' );
38 } );
39 } );
40
41 QUnit.test( '.watch( Array ) - multi', function ( assert ) {
42 this.server.respond( function ( req ) {
43 // Match POST requestBody
44 if ( /action=watch.*&titles=Foo%7CBar/.test( req.requestBody ) ) {
45 req.respond( 200, { 'Content-Type': 'application/json' },
46 '{ "watch": [ ' +
47 '{ "title": "Foo", "watched": true, "message": "<b>Added</b>" },' +
48 '{ "title": "Bar", "watched": true, "message": "<b>Added</b>" }' +
49 '] }'
50 );
51 }
52 } );
53
54 return new mw.Api().watch( [ 'Foo', 'Bar' ] ).done( function ( items ) {
55 assert.strictEqual( items[ 0 ].title, 'Foo' );
56 assert.strictEqual( items[ 1 ].title, 'Bar' );
57 } );
58 } );
59
60 }() );