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