Debian Codegraph @

tests/qunit/resources/startup/mediawiki.test.js

raw symbols SCIP
1 ( function () {
2 const jqueryMsg = require( 'mediawiki.jqueryMsg' ).test;
3 QUnit.module( 'mediawiki' );
4
5 QUnit.test( 'Initial check', ( assert ) => {
6 assert.strictEqual( typeof window.jQuery, 'function', 'jQuery defined' );
7 assert.strictEqual( typeof window.$, 'function', '$ defined' );
8 assert.strictEqual( window.$, window.jQuery, '$ alias to jQuery' );
9
10 assert.strictEqual( typeof window.mediaWiki, 'object', 'mediaWiki defined' );
11 assert.strictEqual( typeof window.mw, 'object', 'mw defined' );
12 assert.strictEqual( window.mw, window.mediaWiki, 'mw alias to mediaWiki' );
13 } );
14
15 QUnit.test( 'mw.format', ( assert ) => {
16 assert.strictEqual(
17 mw.format( 'Format $1 $2', 'foo', 'bar' ),
18 'Format foo bar',
19 'Simple parameters'
20 );
21 assert.strictEqual(
22 mw.format( 'Format $1 $2' ),
23 'Format $1 $2',
24 'Missing parameters'
25 );
26 } );
27
28 QUnit.test( 'mw.now', ( assert ) => {
29 assert.strictEqual( typeof mw.now(), 'number', 'Return a number' );
30 assert.strictEqual(
31 String( Math.round( mw.now() ) ).length,
32 String( Date.now() ).length,
33 'Match size of current timestamp'
34 );
35 } );
36
37 QUnit.module( 'mw.Message', ( hooks ) => {
38 let parserDefaults;
39 hooks.before( () => {
40 parserDefaults = jqueryMsg.getParserDefaults();
41 jqueryMsg.setParserDefaults( {
42 magic: {
43 SITENAME: 'My Wiki'
44 }
45 } );
46 mw.messages.set( {
47 hello: 'Hello <b>awesome</b> world',
48 script: '<script >alert( "Who?" );</script>'
49 } );
50 } );
51 hooks.after( () => {
52 jqueryMsg.setParserDefaults( parserDefaults );
53 mw.config.set( 'wgUserLanguage', 'qqx' );
54 } );
55
56 QUnit.test( 'Construct', ( assert ) => {
57 const hello = mw.message( 'hello' );
58
59 assert.strictEqual( hello.map, mw.messages, 'internal "map" property' );
60 assert.strictEqual( hello.key, 'hello', 'internal "key" property' );
61 assert.deepEqual( hello.parameters, [], 'internal "parameters" property' );
62 } );
63
64 QUnit.test( 'plain()', ( assert ) => {
65 const hello = mw.message( 'hello' );
66 assert.strictEqual( hello.plain(), 'Hello <b>awesome</b> world', 'hello' );
67 const script = mw.message( 'script' );
68 assert.strictEqual( script.plain(), '<script >alert( "Who?" );</script>', 'script' );
69 } );
70
71 QUnit.test( 'escaped()', ( assert ) => {
72 const hello = mw.message( 'hello' );
73 assert.strictEqual( hello.escaped(), 'Hello &lt;b&gt;awesome&lt;/b&gt; world', 'hello' );
74 const script = mw.message( 'script' );
75 assert.strictEqual( script.escaped(), '&lt;script &gt;alert( &quot;Who?&quot; );&lt;/script&gt;', 'script' );
76 } );
77
78 QUnit.test( 'parse()', ( assert ) => {
79 const hello = mw.message( 'hello' );
80 assert.strictEqual( hello.parse(), 'Hello <b>awesome</b> world', 'hello' );
81 const script = mw.message( 'script' );
82 assert.strictEqual( script.parse(), '&lt;script &gt;alert( "Who?" );&lt;/script&gt;', 'script' );
83 } );
84
85 QUnit.test( 'exists()', ( assert ) => {
86 const hello = mw.message( 'hello' );
87 assert.true( hello.exists(), 'Existing message' );
88
89 const goodbye = mw.message( 'goodbye' );
90 assert.false( goodbye.exists(), 'Non-existing message' );
91 } );
92
93 QUnit.test( 'toString() non-existing', ( assert ) => {
94 mw.config.set( 'wgUserLanguage', 'en' );
95 const obj = mw.message( 'good<>bye' );
96 const expected = '⧼good&lt;&gt;bye⧽';
97 assert.strictEqual( obj.plain(), expected, 'plain' );
98 assert.strictEqual( obj.text(), expected, 'text' );
99 assert.strictEqual( obj.escaped(), expected, 'escaped' );
100 assert.strictEqual( obj.parse(), expected, 'parse' );
101
102 mw.config.set( 'wgUserLanguage', 'qqx' );
103 mw.messages.set( 'test-qqx', '(test-qqx)' );
104 mw.messages.set( 'test-nonqqx', 'hello world' );
105
106 assert.strictEqual( mw.message( 'missing-message' ).plain(), '(missing-message)', 'qqx message (missing)' );
107 assert.strictEqual( mw.message( 'missing-message', 'bar', 'baz' ).plain(), '(missing-message: bar, baz)', 'qqx message (missing) with parameters' );
108 assert.strictEqual( mw.message( 'test-qqx' ).plain(), '(test-qqx)', 'qqx message (defined)' );
109 assert.strictEqual( mw.message( 'test-qqx', 'bar', 'baz' ).plain(), '(test-qqx: bar, baz)', 'qqx message (defined) with parameters' );
110 assert.strictEqual( mw.message( 'test-nonqqx' ).plain(), 'hello world', 'non-qqx message in qqx mode' );
111 } );
112
113 // Basic integration test for magic words
114 // See mediawiki.jqueryMsg.test.js for deep coverage.
115 QUnit.test( 'jqueryMsg / Magic words', ( assert ) => {
116 mw.messages.set( {
117 'multiple-curly-brace': '"{{SITENAME}}" is the home of {{int:other-message}}',
118 'other-message': 'Other Message',
119 'test-formatnum': '{{formatnum:$1}}',
120 'test-gender': '{{GENDER:$1|his|her|their}} {{PLURAL:$2|thing|things}}',
121 'test-grammar': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
122 'test-int': 'Some {{int:other-message}}',
123 'test-plural': 'There {{PLURAL:$1|is|are}} $1 {{PLURAL:$1|result|results}}'
124 } );
125
126 let obj = mw.message( 'test-plural', 6 );
127 const expected = 'There are 6 results';
128
129 assert.strictEqual( obj.plain(), 'There {{PLURAL:6|is|are}} 6 {{PLURAL:6|result|results}}', 'plain applies parameter but leaves magic words' );
130 assert.strictEqual( obj.text(), expected, 'Plural text' );
131 assert.strictEqual( obj.escaped(), expected, 'Plural escaped' );
132 assert.strictEqual( obj.parse(), expected, 'Plural parse' );
133
134 // Use English for formatnum
135 mw.config.set( 'wgUserLanguage', 'en' );
136 assert.strictEqual( mw.message( 'test-formatnum', '987654321.654321' ).text(), '987,654,321.654', 'Expand formatnum' );
137 assert.strictEqual( mw.message( 'test-grammar' ).text(), 'Przeszukaj My Wiki', 'Expand grammar' );
138 assert.strictEqual( mw.message( 'test-int' ).text(), 'Some Other Message', 'Expand int' );
139
140 assert.strictEqual( mw.message( 'test-gender', 'male', 1 ).text(), 'his thing', 'Gender male' );
141 assert.strictEqual( mw.message( 'test-gender', 'female', 1 ).text(), 'her thing', 'Gender female' );
142 assert.strictEqual( mw.message( 'test-gender', 'unknown', 1 ).text(), 'their thing', 'Gender neutral' );
143 assert.strictEqual( mw.message( 'test-gender', 'unknown', 10 ).text(), 'their things', 'Gender neutral plural' );
144
145 obj = mw.message( 'multiple-curly-brace' );
146 assert.strictEqual( obj.escaped(), '&quot;My Wiki&quot; is the home of Other Message', 'Expand sitename and int' );
147 } );
148
149 QUnit.test( 'mw.msg()', ( assert ) => {
150 mw.messages.set( 'hello', 'Hello <b>awesome</b> world' );
151 assert.strictEqual( mw.msg( 'goodbye' ), '⧼goodbye⧽', 'Non-existing message' );
152 assert.strictEqual( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Shortcut does not escape' );
153 assert.strictEqual( mw.msg( 'test-gender', 'unknown', 10 ), 'their things', 'Shortcut does text/magic expansion' );
154 } );
155 } );
156 }() );