mediawiki.api.category: Avoid exceptions
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.category.test.js
1 ( function ( mw ) {
2 QUnit.module( 'mediawiki.api.category', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 }
7 } ) );
8
9 QUnit.test( '.getCategoriesByPrefix()', function ( assert ) {
10 this.server.respondWith( [ 200, { 'Content-Type': 'application/json' },
11 '{ "query": { "allpages": [ ' +
12 '{ "title": "Category:Food" },' +
13 '{ "title": "Category:Fool Supermarine S.6" },' +
14 '{ "title": "Category:Fools" }' +
15 '] } }'
16 ] );
17
18 return new mw.Api().getCategoriesByPrefix( 'Foo' ).then( function ( matches ) {
19 assert.deepEqual(
20 matches,
21 [ 'Food', 'Fool Supermarine S.6', 'Fools' ]
22 );
23 } );
24 } );
25
26 QUnit.test( '.isCategory("")', function ( assert ) {
27 this.server.respondWith( /titles=$/, [
28 200,
29 { 'Content-Type': 'application/json' },
30 '{"batchcomplete":true}'
31 ] );
32 return new mw.Api().isCategory( '' ).then( function ( response ) {
33 assert.equal( response, false );
34 } );
35 } );
36
37 QUnit.test( '.isCategory("#")', function ( assert ) {
38 this.server.respondWith( /titles=%23$/, [
39 200,
40 { 'Content-Type': 'application/json' },
41 '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}'
42 ] );
43 return new mw.Api().isCategory( '#' ).then( function ( response ) {
44 assert.equal( response, false );
45 } );
46 } );
47
48 QUnit.test( '.isCategory("mw:")', function ( assert ) {
49 this.server.respondWith( /titles=mw%3A$/, [
50 200,
51 { 'Content-Type': 'application/json' },
52 '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}'
53 ] );
54 return new mw.Api().isCategory( 'mw:' ).then( function ( response ) {
55 assert.equal( response, false );
56 } );
57 } );
58
59 QUnit.test( '.isCategory("|")', function ( assert ) {
60 this.server.respondWith( /titles=%1F%7C$/, [
61 200,
62 { 'Content-Type': 'application/json' },
63 '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}'
64 ] );
65 return new mw.Api().isCategory( '|' ).then( function ( response ) {
66 assert.equal( response, false );
67 } );
68 } );
69
70 QUnit.test( '.getCategories("")', function ( assert ) {
71 this.server.respondWith( /titles=$/, [
72 200,
73 { 'Content-Type': 'application/json' },
74 '{"batchcomplete":true}'
75 ] );
76 return new mw.Api().getCategories( '' ).then( function ( response ) {
77 assert.equal( response, false );
78 } );
79 } );
80
81 QUnit.test( '.getCategories("#")', function ( assert ) {
82 this.server.respondWith( /titles=%23$/, [
83 200,
84 { 'Content-Type': 'application/json' },
85 '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}'
86 ] );
87 return new mw.Api().getCategories( '#' ).then( function ( response ) {
88 assert.equal( response, false );
89 } );
90 } );
91
92 QUnit.test( '.getCategories("mw:")', function ( assert ) {
93 this.server.respondWith( /titles=mw%3A$/, [
94 200,
95 { 'Content-Type': 'application/json' },
96 '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}'
97 ] );
98 return new mw.Api().getCategories( 'mw:' ).then( function ( response ) {
99 assert.equal( response, false );
100 } );
101 } );
102
103 QUnit.test( '.getCategories("|")', function ( assert ) {
104 this.server.respondWith( /titles=%1F%7C$/, [
105 200,
106 { 'Content-Type': 'application/json' },
107 '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}'
108 ] );
109 return new mw.Api().getCategories( '|' ).then( function ( response ) {
110 assert.equal( response, false );
111 } );
112 } );
113
114 }( mediaWiki ) );