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