Use OO.ui.MultilineTextInputWidget
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.loader.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki (mw.loader)', QUnit.newMwEnvironment( {
3 setup: function () {
4 mw.loader.store.enabled = false;
5 },
6 teardown: function () {
7 mw.loader.store.enabled = false;
8 // Teardown for StringSet shim test
9 if ( this.nativeSet ) {
10 window.Set = this.nativeSet;
11 mw.redefineFallbacksForTest();
12 }
13 }
14 } ) );
15
16 mw.loader.addSource(
17 'testloader',
18 QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
19 );
20
21 /**
22 * The sync style load test (for @import). This is, in a way, also an open bug for
23 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
24 * way to get a callback from when a stylesheet is loaded (that is, including any
25 * `@import` rules inside). To work around this, we'll have a little time loop to check
26 * if the styles apply.
27 *
28 * Note: This test originally used new Image() and onerror to get a callback
29 * when the url is loaded, but that is fragile since it doesn't monitor the
30 * same request as the css @import, and Safari 4 has issues with
31 * onerror/onload not being fired at all in weird cases like this.
32 */
33 function assertStyleAsync( assert, $element, prop, val, fn ) {
34 var styleTestStart,
35 el = $element.get( 0 ),
36 styleTestTimeout = ( QUnit.config.testTimeout || 5000 ) - 200;
37
38 function isCssImportApplied() {
39 // Trigger reflow, repaint, redraw, whatever (cross-browser)
40 $element.css( 'height' );
41 // eslint-disable-next-line no-unused-expressions
42 el.innerHTML;
43 el.className = el.className;
44 // eslint-disable-next-line no-unused-expressions
45 document.documentElement.clientHeight;
46
47 return $element.css( prop ) === val;
48 }
49
50 function styleTestLoop() {
51 var styleTestSince = new Date().getTime() - styleTestStart;
52 // If it is passing or if we timed out, run the real test and stop the loop
53 if ( isCssImportApplied() || styleTestSince > styleTestTimeout ) {
54 assert.equal( $element.css( prop ), val,
55 'style "' + prop + ': ' + val + '" from url is applied (after ' + styleTestSince + 'ms)'
56 );
57
58 if ( fn ) {
59 fn();
60 }
61
62 return;
63 }
64 // Otherwise, keep polling
65 setTimeout( styleTestLoop );
66 }
67
68 // Start the loop
69 styleTestStart = new Date().getTime();
70 styleTestLoop();
71 }
72
73 function urlStyleTest( selector, prop, val ) {
74 return QUnit.fixurl(
75 mw.config.get( 'wgScriptPath' ) +
76 '/tests/qunit/data/styleTest.css.php?' +
77 $.param( {
78 selector: selector,
79 prop: prop,
80 val: val
81 } )
82 );
83 }
84
85 QUnit.test( 'Basic', function ( assert ) {
86 var isAwesomeDone;
87
88 mw.loader.testCallback = function () {
89 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
90 isAwesomeDone = true;
91 };
92
93 mw.loader.implement( 'test.callback', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ] );
94
95 return mw.loader.using( 'test.callback', function () {
96 assert.strictEqual( isAwesomeDone, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
97 delete mw.loader.testCallback;
98
99 }, function () {
100 assert.ok( false, 'Error callback fired while loader.using "test.callback" module' );
101 } );
102 } );
103
104 QUnit.test( 'Object method as module name', function ( assert ) {
105 var isAwesomeDone;
106
107 mw.loader.testCallback = function () {
108 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
109 isAwesomeDone = true;
110 };
111
112 mw.loader.implement( 'hasOwnProperty', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ], {}, {} );
113
114 return mw.loader.using( 'hasOwnProperty', function () {
115 assert.strictEqual( isAwesomeDone, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
116 delete mw.loader.testCallback;
117
118 }, function () {
119 assert.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
120 } );
121 } );
122
123 QUnit.test( '.using( .. ) Promise', function ( assert ) {
124 var isAwesomeDone;
125
126 mw.loader.testCallback = function () {
127 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
128 isAwesomeDone = true;
129 };
130
131 mw.loader.implement( 'test.promise', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ] );
132
133 return mw.loader.using( 'test.promise' )
134 .done( function () {
135 assert.strictEqual( isAwesomeDone, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
136 delete mw.loader.testCallback;
137 } )
138 .fail( function () {
139 assert.ok( false, 'Error callback fired while loader.using "test.promise" module' );
140 } );
141 } );
142
143 // Covers mw.loader#sortDependencies (with native Set if available)
144 QUnit.test( '.using() Error: Circular dependency [StringSet default]', function ( assert ) {
145 var done = assert.async();
146
147 mw.loader.register( [
148 [ 'test.circle1', '0', [ 'test.circle2' ] ],
149 [ 'test.circle2', '0', [ 'test.circle3' ] ],
150 [ 'test.circle3', '0', [ 'test.circle1' ] ]
151 ] );
152 mw.loader.using( 'test.circle3' ).then(
153 function done() {
154 assert.ok( false, 'Unexpected resolution, expected error.' );
155 },
156 function fail( e ) {
157 assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
158 }
159 )
160 .always( done );
161 } );
162
163 // @covers mw.loader#sortDependencies (with fallback shim)
164 QUnit.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert ) {
165 var done = assert.async();
166
167 if ( !window.Set ) {
168 assert.expect( 0 );
169 done();
170 return;
171 }
172
173 this.nativeSet = window.Set;
174 window.Set = undefined;
175 mw.redefineFallbacksForTest();
176
177 mw.loader.register( [
178 [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
179 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
180 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
181 ] );
182 mw.loader.using( 'test.shim.circle3' ).then(
183 function done() {
184 assert.ok( false, 'Unexpected resolution, expected error.' );
185 },
186 function fail( e ) {
187 assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
188 }
189 )
190 .always( done );
191 } );
192
193 QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
194 var capture = [];
195 mw.loader.register( [
196 [ 'test.circleA', '0', [ 'test.circleB' ] ],
197 [ 'test.circleB', '0', [ 'test.circleC' ] ],
198 [ 'test.circleC', '0', [ 'test.circleA' ] ]
199 ] );
200 this.sandbox.stub( mw, 'track', function ( topic, data ) {
201 capture.push( {
202 topic: topic,
203 error: data.exception && data.exception.message,
204 source: data.source
205 } );
206 } );
207
208 mw.loader.load( 'test.circleC' );
209 assert.deepEqual(
210 [ {
211 topic: 'resourceloader.exception',
212 error: 'Circular reference detected: test.circleB -> test.circleC',
213 source: 'resolve'
214 } ],
215 capture,
216 'Detect circular dependency'
217 );
218 } );
219
220 QUnit.test( '.using() - Error: Unregistered', function ( assert ) {
221 var done = assert.async();
222
223 mw.loader.using( 'test.using.unreg' ).then(
224 function done() {
225 assert.ok( false, 'Unexpected resolution, expected error.' );
226 },
227 function fail( e ) {
228 assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' );
229 }
230 ).always( done );
231 } );
232
233 QUnit.test( '.load() - Error: Unregistered (ignored)', function ( assert ) {
234 assert.expect( 0 );
235 mw.loader.load( 'test.using.unreg2' );
236 } );
237
238 // Regression test for T36853
239 QUnit.test( '.load() - Error: Missing dependency', function ( assert ) {
240 var capture = [];
241 this.sandbox.stub( mw, 'track', function ( topic, data ) {
242 capture.push( {
243 topic: topic,
244 error: data.exception && data.exception.message,
245 source: data.source
246 } );
247 } );
248
249 mw.loader.register( [
250 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
251 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
252 ] );
253 mw.loader.load( 'test.load.missingdep' );
254 assert.deepEqual(
255 [ {
256 topic: 'resourceloader.exception',
257 error: 'Unknown dependency: test.load.missingdep2',
258 source: 'resolve'
259 } ],
260 capture
261 );
262 } );
263
264 QUnit.test( '.implement( styles={ "css": [text, ..] } )', function ( assert ) {
265 var $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
266
267 assert.notEqual(
268 $element.css( 'float' ),
269 'right',
270 'style is clear'
271 );
272
273 mw.loader.implement(
274 'test.implement.a',
275 function () {
276 assert.equal(
277 $element.css( 'float' ),
278 'right',
279 'style is applied'
280 );
281 },
282 {
283 all: '.mw-test-implement-a { float: right; }'
284 }
285 );
286
287 return mw.loader.using( 'test.implement.a' );
288 } );
289
290 QUnit.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert ) {
291 var $element1 = $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
292 $element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
293 $element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
294 done = assert.async();
295
296 assert.notEqual(
297 $element1.css( 'text-align' ),
298 'center',
299 'style is clear'
300 );
301 assert.notEqual(
302 $element2.css( 'float' ),
303 'left',
304 'style is clear'
305 );
306 assert.notEqual(
307 $element3.css( 'text-align' ),
308 'right',
309 'style is clear'
310 );
311
312 mw.loader.implement(
313 'test.implement.b',
314 function () {
315 // Note: done() must only be called when the entire test is
316 // complete. So, make sure that we don't start until *both*
317 // assertStyleAsync calls have completed.
318 var pending = 2;
319 assertStyleAsync( assert, $element2, 'float', 'left', function () {
320 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
321
322 pending--;
323 if ( pending === 0 ) {
324 done();
325 }
326 } );
327 assertStyleAsync( assert, $element3, 'float', 'right', function () {
328 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
329
330 pending--;
331 if ( pending === 0 ) {
332 done();
333 }
334 } );
335 },
336 {
337 url: {
338 print: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
339 screen: [
340 // T42834: Make sure it actually works with more than 1 stylesheet reference
341 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
342 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
343 ]
344 }
345 }
346 );
347
348 mw.loader.load( 'test.implement.b' );
349 } );
350
351 // Backwards compatibility
352 QUnit.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert ) {
353 var $element = $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
354
355 assert.notEqual(
356 $element.css( 'float' ),
357 'right',
358 'style is clear'
359 );
360
361 mw.loader.implement(
362 'test.implement.c',
363 function () {
364 assert.equal(
365 $element.css( 'float' ),
366 'right',
367 'style is applied'
368 );
369 },
370 {
371 all: '.mw-test-implement-c { float: right; }'
372 }
373 );
374
375 return mw.loader.using( 'test.implement.c' );
376 } );
377
378 // Backwards compatibility
379 QUnit.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert ) {
380 var $element = $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
381 $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
382 done = assert.async();
383
384 assert.notEqual(
385 $element.css( 'float' ),
386 'right',
387 'style is clear'
388 );
389 assert.notEqual(
390 $element2.css( 'text-align' ),
391 'center',
392 'style is clear'
393 );
394
395 mw.loader.implement(
396 'test.implement.d',
397 function () {
398 assertStyleAsync( assert, $element, 'float', 'right', function () {
399 assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
400 done();
401 } );
402 },
403 {
404 all: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
405 print: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
406 }
407 );
408
409 mw.loader.load( 'test.implement.d' );
410 } );
411
412 // @import (T33676)
413 QUnit.test( '.implement( styles has @import )', function ( assert ) {
414 var isJsExecuted, $element,
415 done = assert.async();
416
417 mw.loader.implement(
418 'test.implement.import',
419 function () {
420 assert.strictEqual( isJsExecuted, undefined, 'script not executed multiple times' );
421 isJsExecuted = true;
422
423 assert.equal( mw.loader.getState( 'test.implement.import' ), 'executing', 'module state during implement() script execution' );
424
425 $element = $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
426
427 assert.equal( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
428
429 assertStyleAsync( assert, $element, 'float', 'right', function () {
430 assert.equal( $element.css( 'text-align' ), 'center',
431 'CSS styles after the @import rule are working'
432 );
433
434 done();
435 } );
436 },
437 {
438 css: [
439 '@import url(\''
440 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
441 + '\');\n'
442 + '.mw-test-implement-import { text-align: center; }'
443 ]
444 },
445 {
446 'test-foobar': 'Hello Foobar, $1!'
447 }
448 );
449
450 mw.loader.using( 'test.implement.import' ).always( function () {
451 assert.strictEqual( isJsExecuted, true, 'script executed' );
452 assert.equal( mw.loader.getState( 'test.implement.import' ), 'ready', 'module state after script execution' );
453 } );
454 } );
455
456 QUnit.test( '.implement( dependency with styles )', function ( assert ) {
457 var $element = $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
458 $element2 = $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
459
460 assert.notEqual(
461 $element.css( 'float' ),
462 'right',
463 'style is clear'
464 );
465 assert.notEqual(
466 $element2.css( 'float' ),
467 'left',
468 'style is clear'
469 );
470
471 mw.loader.register( [
472 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
473 [ 'test.implement.e2', '0' ]
474 ] );
475
476 mw.loader.implement(
477 'test.implement.e',
478 function () {
479 assert.equal(
480 $element.css( 'float' ),
481 'right',
482 'Depending module\'s style is applied'
483 );
484 },
485 {
486 all: '.mw-test-implement-e { float: right; }'
487 }
488 );
489
490 mw.loader.implement(
491 'test.implement.e2',
492 function () {
493 assert.equal(
494 $element2.css( 'float' ),
495 'left',
496 'Dependency\'s style is applied'
497 );
498 },
499 {
500 all: '.mw-test-implement-e2 { float: left; }'
501 }
502 );
503
504 return mw.loader.using( 'test.implement.e' );
505 } );
506
507 QUnit.test( '.implement( only scripts )', function ( assert ) {
508 mw.loader.implement( 'test.onlyscripts', function () {} );
509 assert.strictEqual( mw.loader.getState( 'test.onlyscripts' ), 'ready' );
510 } );
511
512 QUnit.test( '.implement( only messages )', function ( assert ) {
513 assert.assertFalse( mw.messages.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
514
515 mw.loader.implement( 'test.implement.msgs', [], {}, { T31107: 'loaded' } );
516
517 return mw.loader.using( 'test.implement.msgs', function () {
518 assert.ok( mw.messages.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
519 }, function () {
520 assert.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
521 } );
522 } );
523
524 QUnit.test( '.implement( empty )', function ( assert ) {
525 mw.loader.implement( 'test.empty' );
526 assert.strictEqual( mw.loader.getState( 'test.empty' ), 'ready' );
527 } );
528
529 QUnit.test( 'Broken indirect dependency', function ( assert ) {
530 // don't emit an error event
531 this.sandbox.stub( mw, 'track' );
532
533 mw.loader.register( [
534 [ 'test.module1', '0' ],
535 [ 'test.module2', '0', [ 'test.module1' ] ],
536 [ 'test.module3', '0', [ 'test.module2' ] ]
537 ] );
538 mw.loader.implement( 'test.module1', function () {
539 throw new Error( 'expected' );
540 }, {}, {} );
541 assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
542 assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
543 assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
544
545 assert.strictEqual( mw.track.callCount, 1 );
546 } );
547
548 QUnit.test( 'Out-of-order implementation', function ( assert ) {
549 mw.loader.register( [
550 [ 'test.module4', '0' ],
551 [ 'test.module5', '0', [ 'test.module4' ] ],
552 [ 'test.module6', '0', [ 'test.module5' ] ]
553 ] );
554 mw.loader.implement( 'test.module4', function () {} );
555 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
556 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
557 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
558 mw.loader.implement( 'test.module6', function () {} );
559 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
560 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
561 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
562 mw.loader.implement( 'test.module5', function () {} );
563 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
564 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
565 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
566 } );
567
568 QUnit.test( 'Missing dependency', function ( assert ) {
569 mw.loader.register( [
570 [ 'test.module7', '0' ],
571 [ 'test.module8', '0', [ 'test.module7' ] ],
572 [ 'test.module9', '0', [ 'test.module8' ] ]
573 ] );
574 mw.loader.implement( 'test.module8', function () {} );
575 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
576 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
577 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
578 mw.loader.state( 'test.module7', 'missing' );
579 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
580 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
581 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
582 mw.loader.implement( 'test.module9', function () {} );
583 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
584 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
585 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
586 mw.loader.using(
587 [ 'test.module7' ],
588 function () {
589 assert.ok( false, 'Success fired despite missing dependency' );
590 assert.ok( true, 'QUnit expected() count dummy' );
591 },
592 function ( e, dependencies ) {
593 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
594 assert.deepEqual( dependencies, [ 'test.module7' ], 'Error callback called with module test.module7' );
595 }
596 );
597 mw.loader.using(
598 [ 'test.module9' ],
599 function () {
600 assert.ok( false, 'Success fired despite missing dependency' );
601 assert.ok( true, 'QUnit expected() count dummy' );
602 },
603 function ( e, dependencies ) {
604 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
605 dependencies.sort();
606 assert.deepEqual(
607 dependencies,
608 [ 'test.module7', 'test.module8', 'test.module9' ],
609 'Error callback called with all three modules as dependencies'
610 );
611 }
612 );
613 } );
614
615 QUnit.test( 'Dependency handling', function ( assert ) {
616 var done = assert.async();
617 mw.loader.register( [
618 // [module, version, dependencies, group, source]
619 [ 'testMissing', '1', [], null, 'testloader' ],
620 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
621 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
622 ] );
623
624 function verifyModuleStates() {
625 assert.equal( mw.loader.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
626 assert.equal( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
627 assert.equal( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
628 }
629
630 mw.loader.using( [ 'testUsesNestedMissing' ],
631 function () {
632 assert.ok( false, 'Error handler should be invoked.' );
633 assert.ok( true ); // Dummy to reach QUnit expect()
634
635 verifyModuleStates();
636
637 done();
638 },
639 function ( e, badmodules ) {
640 assert.ok( true, 'Error handler should be invoked.' );
641 // As soon as server spits out state('testMissing', 'missing');
642 // it will bubble up and trigger the error callback.
643 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
644 assert.deepEqual( badmodules, [ 'testMissing' ], 'Bad modules as expected.' );
645
646 verifyModuleStates();
647
648 done();
649 }
650 );
651 } );
652
653 QUnit.test( 'Skip-function handling', function ( assert ) {
654 mw.loader.register( [
655 // [module, version, dependencies, group, source, skip]
656 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
657 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
658 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
659 ] );
660
661 function verifyModuleStates() {
662 assert.equal( mw.loader.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
663 assert.equal( mw.loader.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
664 assert.equal( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
665 }
666
667 return mw.loader.using( [ 'testUsesSkippable' ],
668 function () {
669 assert.ok( true, 'Success handler should be invoked.' );
670 assert.ok( true ); // Dummy to match error handler and reach QUnit expect()
671
672 verifyModuleStates();
673 },
674 function ( e, badmodules ) {
675 assert.ok( false, 'Error handler should not be invoked.' );
676 assert.deepEqual( badmodules, [], 'Bad modules as expected.' );
677
678 verifyModuleStates();
679 }
680 );
681 } );
682
683 QUnit.asyncTest( '.load( "//protocol-relative" ) - T32825', function ( assert ) {
684 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
685 // Test is for regressions!
686
687 // Forge a URL to the test callback script
688 var target = QUnit.fixurl(
689 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
690 );
691
692 // Confirm that mw.loader.load() works with protocol-relative URLs
693 target = target.replace( /https?:/, '' );
694
695 assert.equal( target.slice( 0, 2 ), '//',
696 'URL must be relative to test relative URLs!'
697 );
698
699 // Async!
700 // The target calls QUnit.start
701 mw.loader.load( target );
702 } );
703
704 QUnit.asyncTest( '.load( "/absolute-path" )', function ( assert ) {
705 // Forge a URL to the test callback script
706 var target = QUnit.fixurl(
707 mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
708 );
709
710 // Confirm that mw.loader.load() works with absolute-paths (relative to current hostname)
711 assert.equal( target.slice( 0, 1 ), '/', 'URL is relative to document root' );
712
713 // Async!
714 // The target calls QUnit.start
715 mw.loader.load( target );
716 } );
717
718 QUnit.test( 'Empty string module name - T28804', function ( assert ) {
719 var done = false;
720
721 assert.strictEqual( mw.loader.getState( '' ), null, 'State (unregistered)' );
722
723 mw.loader.register( '', 'v1' );
724 assert.strictEqual( mw.loader.getState( '' ), 'registered', 'State (registered)' );
725 assert.strictEqual( mw.loader.getVersion( '' ), 'v1', 'Version' );
726
727 mw.loader.implement( '', function () {
728 done = true;
729 } );
730
731 return mw.loader.using( '', function () {
732 assert.strictEqual( done, true, 'script ran' );
733 assert.strictEqual( mw.loader.getState( '' ), 'ready', 'State (ready)' );
734 } );
735 } );
736
737 QUnit.test( 'Executing race - T112232', function ( assert ) {
738 var done = false;
739
740 // The red herring schedules its CSS buffer first. In T112232, a bug in the
741 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
742 mw.loader.implement(
743 'testRaceRedHerring',
744 function () {},
745 { css: [ '.mw-testRaceRedHerring {}' ] }
746 );
747 mw.loader.implement(
748 'testRaceLoadMe',
749 function () {
750 done = true;
751 },
752 { css: [ '.mw-testRaceLoadMe { float: left; }' ] }
753 );
754
755 mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
756 return mw.loader.using( 'testRaceLoadMe', function () {
757 assert.strictEqual( done, true, 'script ran' );
758 assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' );
759 } );
760 } );
761
762 QUnit.test( 'Stale response caching - T117587', function ( assert ) {
763 var count = 0;
764 mw.loader.store.enabled = true;
765 mw.loader.register( 'test.stale', 'v2' );
766 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
767
768 mw.loader.implement( 'test.stale@v1', function () {
769 count++;
770 } );
771
772 return mw.loader.using( 'test.stale' )
773 .then( function () {
774 assert.strictEqual( count, 1 );
775 // After implementing, registry contains version as implemented by the response.
776 assert.strictEqual( mw.loader.getVersion( 'test.stale' ), 'v1', 'Override version' );
777 assert.strictEqual( mw.loader.getState( 'test.stale' ), 'ready' );
778 assert.ok( mw.loader.store.get( 'test.stale' ), 'In store' );
779 } )
780 .then( function () {
781 // Reset run time, but keep mw.loader.store
782 mw.loader.moduleRegistry[ 'test.stale' ].script = undefined;
783 mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered';
784 mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2';
785
786 // Module was stored correctly as v1
787 // On future navigations, it will be ignored until evicted
788 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
789 } );
790 } );
791
792 QUnit.test( 'Stale response caching - backcompat', function ( assert ) {
793 var count = 0;
794 mw.loader.store.enabled = true;
795 mw.loader.register( 'test.stalebc', 'v2' );
796 assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' );
797
798 mw.loader.implement( 'test.stalebc', function () {
799 count++;
800 } );
801
802 return mw.loader.using( 'test.stalebc' )
803 .then( function () {
804 assert.strictEqual( count, 1 );
805 assert.strictEqual( mw.loader.getState( 'test.stalebc' ), 'ready' );
806 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
807 } )
808 .then( function () {
809 // Reset run time, but keep mw.loader.store
810 mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined;
811 mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered';
812 mw.loader.moduleRegistry[ 'test.stalebc' ].version = 'v2';
813
814 // Legacy behaviour is storing under the expected version,
815 // which woudl lead to whitewashing and stale values (T117587).
816 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
817 } );
818 } );
819
820 QUnit.test( 'require()', function ( assert ) {
821 mw.loader.register( [
822 [ 'test.require1', '0' ],
823 [ 'test.require2', '0' ],
824 [ 'test.require3', '0' ],
825 [ 'test.require4', '0', [ 'test.require3' ] ]
826 ] );
827 mw.loader.implement( 'test.require1', function () {} );
828 mw.loader.implement( 'test.require2', function ( $, jQuery, require, module ) {
829 module.exports = 1;
830 } );
831 mw.loader.implement( 'test.require3', function ( $, jQuery, require, module ) {
832 module.exports = function () {
833 return 'hello world';
834 };
835 } );
836 mw.loader.implement( 'test.require4', function ( $, jQuery, require, module ) {
837 var other = require( 'test.require3' );
838 module.exports = {
839 pizza: function () {
840 return other();
841 }
842 };
843 } );
844 return mw.loader.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] )
845 .then( function ( require ) {
846 var module1, module2, module3, module4;
847
848 module1 = require( 'test.require1' );
849 module2 = require( 'test.require2' );
850 module3 = require( 'test.require3' );
851 module4 = require( 'test.require4' );
852
853 assert.strictEqual( typeof module1, 'object', 'export of module with no export' );
854 assert.strictEqual( module2, 1, 'export a number' );
855 assert.strictEqual( module3(), 'hello world', 'export a function' );
856 assert.strictEqual( typeof module4.pizza, 'function', 'export an object' );
857 assert.strictEqual( module4.pizza(), 'hello world', 'module can require other modules' );
858
859 assert.throws( function () {
860 require( '_badmodule' );
861 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
862 } );
863 } );
864
865 QUnit.test( 'require() in debug mode', function ( assert ) {
866 var path = mw.config.get( 'wgScriptPath' );
867 mw.loader.register( [
868 [ 'test.require.define', '0' ],
869 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
870 ] );
871 mw.loader.implement( 'test.require.callback', [ QUnit.fixurl( path + '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
872 mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
873
874 return mw.loader.using( 'test.require.callback' ).then( function ( require ) {
875 var cb = require( 'test.require.callback' );
876 assert.strictEqual( cb.immediate, 'Defined.', 'module.exports and require work in debug mode' );
877 // Must use try-catch because cb.later() will throw if require is undefined,
878 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
879 try {
880 assert.strictEqual( cb.later(), 'Defined.', 'require works asynchrously in debug mode' );
881 } catch ( e ) {
882 assert.equal( null, String( e ), 'require works asynchrously in debug mode' );
883 }
884 }, function () {
885 assert.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
886 } );
887 } );
888
889 }( mediaWiki, jQuery ) );