summaryrefslogtreecommitdiff
path: root/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js
blob: 64a51847110e57faa6281505b2f3723e32c73e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
( function ( mw ) {
	QUnit.module( 'mediawiki.api.watch', QUnit.newMwEnvironment( {
		setup: function () {
			this.server = this.sandbox.useFakeServer();
		}
	} ) );

	QUnit.test( '.watch()', function ( assert ) {
		QUnit.expect( 4 );

		var api = new mw.Api();

		// Ensure we don't mistake a single item array for a single item and vice versa.
		// The query parameter in request is the same either way (separated by pipe).
		api.watch( 'Foo' ).done( function ( item ) {
			assert.equal( item.title, 'Foo' );
		} );

		api.watch( [ 'Foo' ] ).done( function ( items ) {
			assert.equal( items[ 0 ].title, 'Foo' );
		} );

		api.watch( [ 'Foo', 'Bar' ] ).done( function ( items ) {
			assert.equal( items[ 0 ].title, 'Foo' );
			assert.equal( items[ 1 ].title, 'Bar' );
		} );

		// Requests are POST, match requestBody instead of url
		this.server.respond( function ( req ) {
			if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) {
				req.respond( 200, { 'Content-Type': 'application/json' },
					'{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }'
				);
			}

			if ( /action=watch.*&titles=Foo%7CBar/.test( req.requestBody ) ) {
				req.respond( 200, { 'Content-Type': 'application/json' },
					'{ "watch": [ ' +
						'{ "title": "Foo", "watched": true, "message": "<b>Added</b>" },' +
						'{ "title": "Bar", "watched": true, "message": "<b>Added</b>" }' +
						'] }'
				);
			}
		} );
	} );
}( mediaWiki ) );