Merge "Update jQuery to 1.8.2"
[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 "' + prop + ': ' + val + '" 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, ..] } } )', 7, function ( assert ) {
257 var $element1 = $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
258 $element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
259 $element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' );
260
261 assert.notEqual(
262 $element1.css( 'text-align' ),
263 'center',
264 'style is clear'
265 );
266 assert.notEqual(
267 $element2.css( 'float' ),
268 'left',
269 'style is clear'
270 );
271 assert.notEqual(
272 $element3.css( 'text-align' ),
273 'right',
274 'style is clear'
275 );
276
277 mw.loader.implement(
278 'test.implement.b',
279 function () {
280 assertStyleAsync( assert, $element2, 'float', 'left', function () {
281 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
282
283 QUnit.start();
284 } );
285 assertStyleAsync( assert, $element3, 'float', 'right', function () {
286 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
287
288 QUnit.start();
289 } );
290 },
291 {
292 'url': {
293 'print': [urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' )],
294 'screen': [
295 // bug 40834: Make sure it actually works with more than 1 stylesheet reference
296 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
297 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
298 ]
299 }
300 },
301 {}
302 );
303
304 mw.loader.load([
305 'test.implement.b'
306 ]);
307 } );
308
309 // Backwards compatibility
310 QUnit.test( 'mw.loader.implement( styles={ <media>: text } ) (back-compat)', 2, function ( assert ) {
311 var $element = $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
312
313 assert.notEqual(
314 $element.css( 'float' ),
315 'right',
316 'style is clear'
317 );
318
319 mw.loader.implement(
320 'test.implement.c',
321 function () {
322 assert.equal(
323 $element.css( 'float' ),
324 'right',
325 'style is applied'
326 );
327 },
328 {
329 'all': '.mw-test-implement-c { float: right; }'
330 },
331 {}
332 );
333
334 mw.loader.load([
335 'test.implement.c'
336 ]);
337 } );
338
339 // Backwards compatibility
340 QUnit.asyncTest( 'mw.loader.implement( styles={ <media>: [url, ..] } ) (back-compat)', 4, function ( assert ) {
341 var $element = $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
342 $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' );
343
344 assert.notEqual(
345 $element.css( 'float' ),
346 'right',
347 'style is clear'
348 );
349 assert.notEqual(
350 $element2.css( 'text-align' ),
351 'center',
352 'style is clear'
353 );
354
355 mw.loader.implement(
356 'test.implement.d',
357 function () {
358 assertStyleAsync( assert, $element, 'float', 'right', function () {
359
360 assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied (bug 40500)' );
361
362 QUnit.start();
363 } );
364 },
365 {
366 'all': [urlStyleTest( '.mw-test-implement-d', 'float', 'right' )],
367 'print': [urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' )]
368 },
369 {}
370 );
371
372 mw.loader.load([
373 'test.implement.d'
374 ]);
375 } );
376
377 // @import (bug 31676)
378 QUnit.asyncTest( 'mw.loader.implement( styles has @import)', 5, function ( assert ) {
379 var isJsExecuted, $element;
380
381 mw.loader.implement(
382 'test.implement.import',
383 function () {
384 assert.strictEqual( isJsExecuted, undefined, 'javascript not executed multiple times' );
385 isJsExecuted = true;
386
387 assert.equal( mw.loader.getState( 'test.implement.import' ), 'ready', 'module state is "ready" while implement() is executing javascript' );
388
389 $element = $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
390
391 assert.equal( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'Messages are loaded before javascript execution' );
392
393 assertStyleAsync( assert, $element, 'float', 'right', function () {
394 assert.equal( $element.css( 'text-align' ),'center',
395 'CSS styles after the @import rule are working'
396 );
397
398 QUnit.start();
399 } );
400 },
401 {
402 'css': [
403 '@import url(\''
404 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
405 + '\');\n'
406 + '.mw-test-implement-import { text-align: center; }'
407 ]
408 },
409 {
410 'test-foobar': 'Hello Foobar, $1!'
411 }
412 );
413
414 mw.loader.load( 'test.implement' );
415
416 });
417
418 QUnit.asyncTest( 'mw.loader.implement( only messages )' , 2, function ( assert ) {
419 assert.assertFalse( mw.messages.exists( 'bug_29107' ), 'Verify that the test message doesn\'t exist yet' );
420
421 mw.loader.implement( 'test.implement.msgs', [], {}, { 'bug_29107': 'loaded' } );
422 mw.loader.using( 'test.implement.msgs', function() {
423 QUnit.start();
424 assert.ok( mw.messages.exists( 'bug_29107' ), 'Bug 29107: messages-only module should implement ok' );
425 }, function() {
426 QUnit.start();
427 assert.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
428 });
429 });
430
431 QUnit.test( 'mw.loader erroneous indirect dependency', 3, function ( assert ) {
432 mw.loader.register( [
433 ['test.module1', '0'],
434 ['test.module2', '0', ['test.module1']],
435 ['test.module3', '0', ['test.module2']]
436 ] );
437 mw.loader.implement( 'test.module1', function () { throw new Error( 'expected' ); }, {}, {} );
438 assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
439 assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
440 assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
441 } );
442
443 QUnit.test( 'mw.loader out-of-order implementation', 9, function ( assert ) {
444 mw.loader.register( [
445 ['test.module4', '0'],
446 ['test.module5', '0', ['test.module4']],
447 ['test.module6', '0', ['test.module5']]
448 ] );
449 mw.loader.implement( 'test.module4', function () {}, {}, {} );
450 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
451 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
452 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
453 mw.loader.implement( 'test.module6', function () {}, {}, {} );
454 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
455 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
456 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
457 mw.loader.implement( 'test.module5', function() {}, {}, {} );
458 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
459 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
460 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
461 } );
462
463 QUnit.test( 'mw.loader missing dependency', 13, function ( assert ) {
464 mw.loader.register( [
465 ['test.module7', '0'],
466 ['test.module8', '0', ['test.module7']],
467 ['test.module9', '0', ['test.module8']]
468 ] );
469 mw.loader.implement( 'test.module8', function () {}, {}, {} );
470 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
471 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
472 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
473 mw.loader.state( 'test.module7', 'missing' );
474 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
475 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
476 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
477 mw.loader.implement( 'test.module9', function () {}, {}, {} );
478 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
479 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
480 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
481 mw.loader.using(
482 ['test.module7'],
483 function () {
484 assert.ok( false, "Success fired despite missing dependency" );
485 assert.ok( true , "QUnit expected() count dummy" );
486 },
487 function ( e, dependencies ) {
488 assert.strictEqual( $.isArray( dependencies ), true, 'Expected array of dependencies' );
489 assert.deepEqual( dependencies, ['test.module7'], 'Error callback called with module test.module7' );
490 }
491 );
492 mw.loader.using(
493 ['test.module9'],
494 function () {
495 assert.ok( false, "Success fired despite missing dependency" );
496 assert.ok( true , "QUnit expected() count dummy" );
497 },
498 function ( e, dependencies ) {
499 assert.strictEqual( $.isArray( dependencies ), true, 'Expected array of dependencies' );
500 dependencies.sort();
501 assert.deepEqual(
502 dependencies,
503 ['test.module7', 'test.module8', 'test.module9'],
504 'Error callback called with all three modules as dependencies'
505 );
506 }
507 );
508 } );
509
510 QUnit.asyncTest( 'mw.loader dependency handling', 5, function ( assert ) {
511 mw.loader.addSource(
512 'testloader',
513 {
514 loadScript: QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
515 }
516 );
517
518 mw.loader.register( [
519 // [module, version, dependencies, group, source]
520 ['testMissing', '1', [], null, 'testloader'],
521 ['testUsesMissing', '1', ['testMissing'], null, 'testloader'],
522 ['testUsesNestedMissing', '1', ['testUsesMissing'], null, 'testloader']
523 ] );
524
525 function verifyModuleStates() {
526 assert.equal( mw.loader.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
527 assert.equal( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
528 assert.equal( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
529 }
530
531 mw.loader.using( ['testUsesNestedMissing'],
532 function () {
533 assert.ok( false, 'Error handler should be invoked.' );
534 assert.ok( true ); // Dummy to reach QUnit expect()
535
536 verifyModuleStates();
537
538 QUnit.start();
539 },
540 function ( e, badmodules ) {
541 assert.ok( true, 'Error handler should be invoked.' );
542 // As soon as server spits out state('testMissing', 'missing');
543 // it will bubble up and trigger the error callback.
544 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
545 assert.deepEqual( badmodules, ['testMissing'], 'Bad modules as expected.' );
546
547 verifyModuleStates();
548
549 QUnit.start();
550 }
551 );
552 } );
553
554 QUnit.asyncTest( 'mw.loader( "//protocol-relative" ) (bug 30825)', 2, function ( assert ) {
555 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
556 // Test is for regressions!
557
558 // Forge an URL to the test callback script
559 var target = QUnit.fixurl(
560 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
561 );
562
563 // Confirm that mw.loader.load() works with protocol-relative URLs
564 target = target.replace( /https?:/, '' );
565
566 assert.equal( target.substr( 0, 2 ), '//',
567 'URL must be relative to test relative URLs!'
568 );
569
570 // Async!
571 // The target calls QUnit.start
572 mw.loader.load( target );
573 });
574
575 QUnit.test( 'mw.html', 13, function ( assert ) {
576 assert.throws( function () {
577 mw.html.escape();
578 }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
579
580 assert.equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
581 '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'escape() escapes special characters to html entities' );
582
583 assert.equal( mw.html.element(),
584 '<undefined/>', 'element() always returns a valid html string (even without arguments)' );
585
586 assert.equal( mw.html.element( 'div' ), '<div/>', 'element() Plain DIV (simple)' );
587
588 assert.equal( mw.html.element( 'div', {}, '' ), '<div></div>', 'element() Basic DIV (simple)' );
589
590 assert.equal(
591 mw.html.element(
592 'div', {
593 id: 'foobar'
594 }
595 ),
596 '<div id="foobar"/>',
597 'html.element DIV (attribs)' );
598
599 assert.equal( mw.html.element( 'p', null, 12 ), '<p>12</p>', 'Numbers are valid content and should be casted to a string' );
600
601 assert.equal( mw.html.element( 'p', { title: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
602
603 // Example from https://www.mediawiki.org/wiki/ResourceLoader/Default_modules#mediaWiki.html
604 assert.equal(
605 mw.html.element(
606 'div',
607 {},
608 new mw.html.Raw(
609 mw.html.element( 'img', { src: '<' } )
610 )
611 ),
612 '<div><img src="&lt;"/></div>',
613 'Raw inclusion of another element'
614 );
615
616 assert.equal(
617 mw.html.element(
618 'option', {
619 selected: true
620 }, 'Foo'
621 ),
622 '<option selected="selected">Foo</option>',
623 'Attributes may have boolean values. True copies the attribute name to the value.'
624 );
625
626 assert.equal(
627 mw.html.element(
628 'option', {
629 value: 'foo',
630 selected: false
631 }, 'Foo'
632 ),
633 '<option value="foo">Foo</option>',
634 'Attributes may have boolean values. False keeps the attribute from output.'
635 );
636
637 assert.equal( mw.html.element( 'div',
638 null, 'a' ),
639 '<div>a</div>',
640 'html.element DIV (content)' );
641
642 assert.equal( mw.html.element( 'a',
643 { href: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
644 '<a href="http://mediawiki.org/w/index.php?title=RL&amp;action=history">a</a>',
645 'html.element DIV (attribs + content)' );
646
647 });
648
649 }( mediaWiki ) );