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