Debian Codegraph @

tests/qunit/resources/mediawiki.api/mediawiki.api.watch.test.js

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