Merge "CologneBlue rewrite: fix search form(s) generation"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.test.js
1 ( function ( mw ) {
2
3 QUnit.module( 'mediawiki', QUnit.newMwEnvironment() );
4
5 QUnit.test( 'Initial check', 8, function ( assert ) {
6 assert.ok( window.jQuery, 'jQuery defined' );
7 assert.ok( window.$, '$j defined' );
8 assert.ok( window.$j, '$j defined' );
9 assert.strictEqual( window.$, window.jQuery, '$ alias to jQuery' );
10 assert.strictEqual( window.$j, window.jQuery, '$j alias to jQuery' );
11
12 assert.ok( window.mediaWiki, 'mediaWiki defined' );
13 assert.ok( window.mw, 'mw defined' );
14 assert.strictEqual( window.mw, window.mediaWiki, 'mw alias to mediaWiki' );
15 });
16
17 QUnit.test( 'mw.Map', 17, function ( assert ) {
18 var arry, conf, funky, globalConf, nummy, someValues;
19
20 assert.ok( mw.Map, 'mw.Map defined' );
21
22 conf = new mw.Map();
23 // Dummy variables
24 funky = function () {};
25 arry = [];
26 nummy = 7;
27
28 // Tests for input validation
29 assert.strictEqual( conf.get( 'inexistantKey' ), null, 'Map.get returns null if selection was a string and the key was not found' );
30 assert.strictEqual( conf.set( 'myKey', 'myValue' ), true, 'Map.set returns boolean true if a value was set for a valid key string' );
31 assert.strictEqual( conf.set( funky, 'Funky' ), false, 'Map.set returns boolean false if key was invalid (Function)' );
32 assert.strictEqual( conf.set( arry, 'Arry' ), false, 'Map.set returns boolean false if key was invalid (Array)' );
33 assert.strictEqual( conf.set( nummy, 'Nummy' ), false, 'Map.set returns boolean false if key was invalid (Number)' );
34 assert.equal( conf.get( 'myKey' ), 'myValue', 'Map.get returns a single value value correctly' );
35 assert.strictEqual( conf.get( nummy ), null, 'Map.get ruturns null if selection was invalid (Number)' );
36 assert.strictEqual( conf.get( funky ), null, 'Map.get ruturns null if selection was invalid (Function)' );
37
38 // Multiple values at once
39 someValues = {
40 'foo': 'bar',
41 'lorem': 'ipsum',
42 'MediaWiki': true
43 };
44 assert.strictEqual( conf.set( someValues ), true, 'Map.set returns boolean true if multiple values were set by passing an object' );
45 assert.deepEqual( conf.get( ['foo', 'lorem'] ), {
46 'foo': 'bar',
47 'lorem': 'ipsum'
48 }, 'Map.get returns multiple values correctly as an object' );
49
50 assert.deepEqual( conf.get( ['foo', 'notExist'] ), {
51 'foo': 'bar',
52 'notExist': null
53 }, 'Map.get return includes keys that were not found as null values' );
54
55 assert.strictEqual( conf.exists( 'foo' ), true, 'Map.exists returns boolean true if a key exists' );
56 assert.strictEqual( conf.exists( 'notExist' ), false, 'Map.exists returns boolean false if a key does not exists' );
57
58 // Interacting with globals and accessing the values object
59 assert.strictEqual( conf.get(), conf.values, 'Map.get returns the entire values object by reference (if called without arguments)' );
60
61 conf.set( 'globalMapChecker', 'Hi' );
62
63 assert.ok( false === 'globalMapChecker' in window, 'new mw.Map did not store its values in the global window object by default' );
64
65 globalConf = new mw.Map( true );
66 globalConf.set( 'anotherGlobalMapChecker', 'Hello' );
67
68 assert.ok( 'anotherGlobalMapChecker' in window, 'new mw.Map( true ) did store its values in the global window object' );
69
70 // Whitelist this global variable for QUnit's 'noglobal' mode
71 if ( QUnit.config.noglobals ) {
72 QUnit.config.pollution.push( 'anotherGlobalMapChecker' );
73 }
74 });
75
76 QUnit.test( 'mw.config', 1, function ( assert ) {
77 assert.ok( mw.config instanceof mw.Map, 'mw.config instance of mw.Map' );
78 });
79
80 QUnit.test( 'mw.message & mw.messages', 20, function ( assert ) {
81 var goodbye, hello, pluralMessage;
82
83 assert.ok( mw.messages, 'messages defined' );
84 assert.ok( mw.messages instanceof mw.Map, 'mw.messages instance of mw.Map' );
85 assert.ok( mw.messages.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
86
87 hello = mw.message( 'hello' );
88
89 assert.equal( hello.format, 'plain', 'Message property "format" defaults to "plain"' );
90 assert.strictEqual( hello.map, mw.messages, 'Message property "map" defaults to the global instance in mw.messages' );
91 assert.equal( hello.key, 'hello', 'Message property "key" (currect key)' );
92 assert.deepEqual( hello.parameters, [], 'Message property "parameters" defaults to an empty array' );
93
94 // Todo
95 assert.ok( hello.params, 'Message prototype "params"' );
96
97 hello.format = 'plain';
98 assert.equal( hello.toString(), 'Hello <b>awesome</b> world', 'Message.toString returns the message as a string with the current "format"' );
99
100 assert.equal( hello.escaped(), 'Hello &lt;b&gt;awesome&lt;/b&gt; world', 'Message.escaped returns the escaped message' );
101 assert.equal( hello.format, 'escaped', 'Message.escaped correctly updated the "format" property' );
102
103 hello.parse();
104 assert.equal( hello.format, 'parse', 'Message.parse correctly updated the "format" property' );
105
106 hello.plain();
107 assert.equal( hello.format, 'plain', 'Message.plain correctly updated the "format" property' );
108
109 assert.strictEqual( hello.exists(), true, 'Message.exists returns true for existing messages' );
110
111 goodbye = mw.message( 'goodbye' );
112 assert.strictEqual( goodbye.exists(), false, 'Message.exists returns false for nonexistent messages' );
113
114 assert.equal( goodbye.plain(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
115 // bug 30684
116 assert.equal( goodbye.escaped(), '&lt;goodbye&gt;', 'Message.toString returns properly escaped &lt;key&gt; if format is "escaped" and key does not exist' );
117
118 assert.ok( mw.messages.set( 'pluraltestmsg', 'There {{PLURAL:$1|is|are}} $1 {{PLURAL:$1|result|results}}' ), 'mw.messages.set: Register' );
119 pluralMessage = mw.message( 'pluraltestmsg' , 6 );
120 assert.equal( pluralMessage.plain(), 'There are 6 results', 'plural get resolved when format is plain' );
121 assert.equal( pluralMessage.parse(), 'There are 6 results', 'plural get resolved when format is parse' );
122
123 });
124
125 QUnit.test( 'mw.msg', 11, function ( assert ) {
126 assert.ok( mw.messages.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
127 assert.equal( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
128 assert.equal( mw.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (nonexistent message)' );
129
130 assert.ok( mw.messages.set( 'plural-item' , 'Found $1 {{PLURAL:$1|item|items}}' ) );
131 assert.equal( mw.msg( 'plural-item', 5 ), 'Found 5 items', 'Apply plural for count 5' );
132 assert.equal( mw.msg( 'plural-item', 0 ), 'Found 0 items', 'Apply plural for count 0' );
133 assert.equal( mw.msg( 'plural-item', 1 ), 'Found 1 item', 'Apply plural for count 1' );
134
135 assert.ok( mw.messages.set('gender-plural-msg' , '{{GENDER:$1|he|she|they}} {{PLURAL:$2|is|are}} awesome' ) );
136 assert.equal( mw.msg( 'gender-plural-msg', 'male', 1 ), 'he is awesome', 'Gender test for male, plural count 1' );
137 assert.equal( mw.msg( 'gender-plural-msg', 'female', '1' ), 'she is awesome', 'Gender test for female, plural count 1' );
138 assert.equal( mw.msg( 'gender-plural-msg', 'unknown', 10 ), 'they are awesome', 'Gender test for neutral, plural count 10' );
139
140 });
141
142 /**
143 * The sync style load test (for @import). This is, in a way, also an open bug for
144 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
145 * way to get a callback from when a stylesheet is loaded (that is, including any
146 * @import rules inside). To work around this, we'll have a little time loop to check
147 * if the styles apply.
148 * Note: This test originally used new Image() and onerror to get a callback
149 * when the url is loaded, but that is fragile since it doesn't monitor the
150 * same request as the css @import, and Safari 4 has issues with
151 * onerror/onload not being fired at all in weird cases like this.
152 */
153 function assertStyleAsync( assert, $element, prop, val, fn ) {
154 var styleTestStart,
155 el = $element.get( 0 ),
156 styleTestTimeout = ( QUnit.config.testTimeout - 200 ) || 5000;
157
158 function isCssImportApplied() {
159 // Trigger reflow, repaint, redraw, whatever (cross-browser)
160 var x = $element.css( 'height' );
161 x = el.innerHTML;
162 el.className = el.className;
163 x = document.documentElement.clientHeight;
164
165 return $element.css( prop ) === val;
166 }
167
168 function styleTestLoop() {
169 var styleTestSince = new Date().getTime() - styleTestStart;
170 // If it is passing or if we timed out, run the real test and stop the loop
171 if ( isCssImportApplied() || styleTestSince > styleTestTimeout ) {
172 assert.equal( $element.css( prop ), val,
173 'style from url is applied (after ' + styleTestSince + 'ms)'
174 );
175
176 if ( fn ) {
177 fn();
178 }
179
180 return;
181 }
182 // Otherwise, keep polling
183 setTimeout( styleTestLoop, 150 );
184 }
185
186 // Start the loop
187 styleTestStart = new Date().getTime();
188 styleTestLoop();
189 }
190
191 function urlStyleTest( selector, prop, val ) {
192 return QUnit.fixurl(
193 mw.config.get( 'wgScriptPath' ) +
194 '/tests/qunit/data/styleTest.css.php?' +
195 $.param( {
196 selector: selector,
197 prop: prop,
198 val: val
199 } )
200 );
201 }
202
203 QUnit.asyncTest( 'mw.loader', 2, function ( assert ) {
204 var isAwesomeDone;
205
206 mw.loader.testCallback = function () {
207 QUnit.start();
208 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined');
209 isAwesomeDone = true;
210 };
211
212 mw.loader.implement( 'test.callback', [QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' )], {}, {} );
213
214 mw.loader.using( 'test.callback', function () {
215
216 // /sample/awesome.js declares the "mw.loader.testCallback" function
217 // which contains a call to start() and ok()
218 assert.strictEqual( isAwesomeDone, true, "test.callback module should've caused isAwesomeDone to be true" );
219 delete mw.loader.testCallback;
220
221 }, function () {
222 QUnit.start();
223 assert.ok( false, 'Error callback fired while loader.using "test.callback" module' );
224 });
225 });
226
227 QUnit.test( 'mw.loader.implement( styles={ "css": [text, ..] } )', 2, function ( assert ) {
228 var $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
229
230 assert.notEqual(
231 $element.css( 'float' ),
232 'right',
233 'style is clear'
234 );
235
236 mw.loader.implement(
237 'test.implement.a',
238 function () {
239 assert.equal(
240 $element.css( 'float' ),
241 'right',
242 'style is applied'
243 );
244 },
245 {
246 'all': '.mw-test-implement-a { float: right; }'
247 },
248 {}
249 );
250
251 mw.loader.load([
252 'test.implement.a'
253 ]);
254 } );
255
256 QUnit.asyncTest( 'mw.loader.implement( styles={ "url": { <media>: [url, ..] } } )', 4, function ( assert ) {
257 var $element = $( '<div class="mw-test-implement-b"></div>' ).appendTo( '#qunit-fixture' ),
258 $element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' );
259
260 assert.notEqual(
261 $element.css( 'float' ),
262 'right',
263 'style is clear'
264 );
265 assert.notEqual(
266 $element2.css( 'text-align' ),
267 'center',
268 'style is clear'
269 );
270
271 mw.loader.implement(
272 'test.implement.b',
273 function () {
274 assertStyleAsync( assert, $element, 'float', 'right', function () {
275
276 assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied' );
277
278 QUnit.start();
279 } );
280 },
281 {
282 'url': {
283 'screen': [urlStyleTest( '.mw-test-implement-b', 'float', 'right' )],
284 'print': [urlStyleTest( '.mw-test-implement-b2', 'text-align', 'center' )]
285 }
286 },
287 {}
288 );
289
290 mw.loader.load([
291 'test.implement.b'
292 ]);
293 } );
294
295 // Backwards compatibility
296 QUnit.test( 'mw.loader.implement( styles={ <media>: text } ) (back-compat)', 2, function ( assert ) {
297 var $element = $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
298
299 assert.notEqual(
300 $element.css( 'float' ),
301 'right',
302 'style is clear'
303 );
304
305 mw.loader.implement(
306 'test.implement.c',
307 function () {
308 assert.equal(
309 $element.css( 'float' ),
310 'right',
311 'style is applied'
312 );
313 },
314 {
315 'all': '.mw-test-implement-c { float: right; }'
316 },
317 {}
318 );
319
320 mw.loader.load([
321 'test.implement.c'
322 ]);
323 } );
324
325 // Backwards compatibility
326 QUnit.asyncTest( 'mw.loader.implement( styles={ <media>: [url, ..] } ) (back-compat)', 4, function ( assert ) {
327 var $element = $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
328 $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' );
329
330 assert.notEqual(
331 $element.css( 'float' ),
332 'right',
333 'style is clear'
334 );
335 assert.notEqual(
336 $element2.css( 'text-align' ),
337 'center',
338 'style is clear'
339 );
340
341 mw.loader.implement(
342 'test.implement.d',
343 function () {
344 assertStyleAsync( assert, $element, 'float', 'right', function () {
345
346 assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied (bug 40500)' );
347
348 QUnit.start();
349 } );
350 },
351 {
352 'all': [urlStyleTest( '.mw-test-implement-d', 'float', 'right' )],
353 'print': [urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' )]
354 },
355 {}
356 );
357
358 mw.loader.load([
359 'test.implement.d'
360 ]);
361 } );
362
363 // @import (bug 31676)
364 QUnit.asyncTest( 'mw.loader.implement( styles has @import)', 5, function ( assert ) {
365 var isJsExecuted, $element;
366
367 mw.loader.implement(
368 'test.implement.import',
369 function () {
370 assert.strictEqual( isJsExecuted, undefined, 'javascript not executed multiple times' );
371 isJsExecuted = true;
372
373 assert.equal( mw.loader.getState( 'test.implement.import' ), 'ready', 'module state is "ready" while implement() is executing javascript' );
374
375 $element = $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
376
377 assert.equal( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'Messages are loaded before javascript execution' );
378
379 assertStyleAsync( assert, $element, 'float', 'right', function () {
380 assert.equal( $element.css( 'text-align' ),'center',
381 'CSS styles after the @import rule are working'
382 );
383
384 QUnit.start();
385 } );
386 },
387 {
388 'css': [
389 '@import url(\''
390 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
391 + '\');\n'
392 + '.mw-test-implement-import { text-align: center; }'
393 ]
394 },
395 {
396 'test-foobar': 'Hello Foobar, $1!'
397 }
398 );
399
400 mw.loader.load( 'test.implement' );
401
402 });
403
404 QUnit.asyncTest( 'mw.loader.implement( only messages )' , 2, function ( assert ) {
405 assert.assertFalse( mw.messages.exists( 'bug_29107' ), 'Verify that the test message doesn\'t exist yet' );
406
407 mw.loader.implement( 'test.implement.msgs', [], {}, { 'bug_29107': 'loaded' } );
408 mw.loader.using( 'test.implement.msgs', function() {
409 QUnit.start();
410 assert.ok( mw.messages.exists( 'bug_29107' ), 'Bug 29107: messages-only module should implement ok' );
411 }, function() {
412 QUnit.start();
413 assert.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
414 });
415 });
416
417 QUnit.test( 'mw.loader erroneous indirect dependency', 3, function ( assert ) {
418 mw.loader.register( [
419 ['test.module1', '0'],
420 ['test.module2', '0', ['test.module1']],
421 ['test.module3', '0', ['test.module2']]
422 ] );
423 mw.loader.implement( 'test.module1', function () { throw new Error( 'expected' ); }, {}, {} );
424 assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
425 assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
426 assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
427 } );
428
429 QUnit.test( 'mw.loader out-of-order implementation', 9, function ( assert ) {
430 mw.loader.register( [
431 ['test.module4', '0'],
432 ['test.module5', '0', ['test.module4']],
433 ['test.module6', '0', ['test.module5']]
434 ] );
435 mw.loader.implement( 'test.module4', function () {}, {}, {} );
436 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
437 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
438 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
439 mw.loader.implement( 'test.module6', function () {}, {}, {} );
440 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
441 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
442 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
443 mw.loader.implement( 'test.module5', function() {}, {}, {} );
444 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
445 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
446 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
447 } );
448
449 QUnit.test( 'mw.loader missing dependency', 13, function ( assert ) {
450 mw.loader.register( [
451 ['test.module7', '0'],
452 ['test.module8', '0', ['test.module7']],
453 ['test.module9', '0', ['test.module8']]
454 ] );
455 mw.loader.implement( 'test.module8', function () {}, {}, {} );
456 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
457 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
458 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
459 mw.loader.state( 'test.module7', 'missing' );
460 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
461 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
462 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
463 mw.loader.implement( 'test.module9', function () {}, {}, {} );
464 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
465 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
466 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
467 mw.loader.using(
468 ['test.module7'],
469 function () {
470 assert.ok( false, "Success fired despite missing dependency" );
471 assert.ok( true , "QUnit expected() count dummy" );
472 },
473 function ( e, dependencies ) {
474 assert.strictEqual( $.isArray( dependencies ), true, 'Expected array of dependencies' );
475 assert.deepEqual( dependencies, ['test.module7'], 'Error callback called with module test.module7' );
476 }
477 );
478 mw.loader.using(
479 ['test.module9'],
480 function () {
481 assert.ok( false, "Success fired despite missing dependency" );
482 assert.ok( true , "QUnit expected() count dummy" );
483 },
484 function ( e, dependencies ) {
485 assert.strictEqual( $.isArray( dependencies ), true, 'Expected array of dependencies' );
486 dependencies.sort();
487 assert.deepEqual(
488 dependencies,
489 ['test.module7', 'test.module8', 'test.module9'],
490 'Error callback called with all three modules as dependencies'
491 );
492 }
493 );
494 } );
495
496 QUnit.asyncTest( 'mw.loader dependency handling', 5, function ( assert ) {
497 mw.loader.addSource(
498 'testloader',
499 {
500 loadScript: QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
501 }
502 );
503
504 mw.loader.register( [
505 // [module, version, dependencies, group, source]
506 ['testMissing', '1', [], null, 'testloader'],
507 ['testUsesMissing', '1', ['testMissing'], null, 'testloader'],
508 ['testUsesNestedMissing', '1', ['testUsesMissing'], null, 'testloader']
509 ] );
510
511 function verifyModuleStates() {
512 assert.equal( mw.loader.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
513 assert.equal( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
514 assert.equal( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
515 }
516
517 mw.loader.using( ['testUsesNestedMissing'],
518 function () {
519 assert.ok( false, 'Error handler should be invoked.' );
520 assert.ok( true ); // Dummy to reach QUnit expect()
521
522 verifyModuleStates();
523
524 QUnit.start();
525 },
526 function ( e, badmodules ) {
527 assert.ok( true, 'Error handler should be invoked.' );
528 // As soon as server spits out state('testMissing', 'missing');
529 // it will bubble up and trigger the error callback.
530 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
531 assert.deepEqual( badmodules, ['testMissing'], 'Bad modules as expected.' );
532
533 verifyModuleStates();
534
535 QUnit.start();
536 }
537 );
538 } );
539
540 QUnit.asyncTest( 'mw.loader( "//protocol-relative" ) (bug 30825)', 2, function ( assert ) {
541 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
542 // Test is for regressions!
543
544 // Forge an URL to the test callback script
545 var target = QUnit.fixurl(
546 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
547 );
548
549 // Confirm that mw.loader.load() works with protocol-relative URLs
550 target = target.replace( /https?:/, '' );
551
552 assert.equal( target.substr( 0, 2 ), '//',
553 'URL must be relative to test relative URLs!'
554 );
555
556 // Async!
557 // The target calls QUnit.start
558 mw.loader.load( target );
559 });
560
561 QUnit.test( 'mw.html', 13, function ( assert ) {
562 assert.throws( function () {
563 mw.html.escape();
564 }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
565
566 assert.equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
567 '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'escape() escapes special characters to html entities' );
568
569 assert.equal( mw.html.element(),
570 '<undefined/>', 'element() always returns a valid html string (even without arguments)' );
571
572 assert.equal( mw.html.element( 'div' ), '<div/>', 'element() Plain DIV (simple)' );
573
574 assert.equal( mw.html.element( 'div', {}, '' ), '<div></div>', 'element() Basic DIV (simple)' );
575
576 assert.equal(
577 mw.html.element(
578 'div', {
579 id: 'foobar'
580 }
581 ),
582 '<div id="foobar"/>',
583 'html.element DIV (attribs)' );
584
585 assert.equal( mw.html.element( 'p', null, 12 ), '<p>12</p>', 'Numbers are valid content and should be casted to a string' );
586
587 assert.equal( mw.html.element( 'p', { title: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
588
589 // Example from https://www.mediawiki.org/wiki/ResourceLoader/Default_modules#mediaWiki.html
590 assert.equal(
591 mw.html.element(
592 'div',
593 {},
594 new mw.html.Raw(
595 mw.html.element( 'img', { src: '<' } )
596 )
597 ),
598 '<div><img src="&lt;"/></div>',
599 'Raw inclusion of another element'
600 );
601
602 assert.equal(
603 mw.html.element(
604 'option', {
605 selected: true
606 }, 'Foo'
607 ),
608 '<option selected="selected">Foo</option>',
609 'Attributes may have boolean values. True copies the attribute name to the value.'
610 );
611
612 assert.equal(
613 mw.html.element(
614 'option', {
615 value: 'foo',
616 selected: false
617 }, 'Foo'
618 ),
619 '<option value="foo">Foo</option>',
620 'Attributes may have boolean values. False keeps the attribute from output.'
621 );
622
623 assert.equal( mw.html.element( 'div',
624 null, 'a' ),
625 '<div>a</div>',
626 'html.element DIV (content)' );
627
628 assert.equal( mw.html.element( 'a',
629 { href: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
630 '<a href="http://mediawiki.org/w/index.php?title=RL&amp;action=history">a</a>',
631 'html.element DIV (attribs + content)' );
632
633 });
634
635 }( mediaWiki ) );