2 QUnit
.module( 'mediawiki (mw.loader)', QUnit
.newMwEnvironment( {
3 setup: function ( assert
) {
4 mw
.loader
.store
.enabled
= false;
6 // Expose for load.mock.php
7 mw
.loader
.testFail = function ( reason
) {
8 assert
.ok( false, reason
);
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();
18 // Remove any remaining temporary statics
19 // exposed for cross-file mocks.
20 if ( 'testCallback' in mw
.loader
) {
21 delete mw
.loader
.testCallback
;
23 if ( 'testFail' in mw
.loader
) {
24 delete mw
.loader
.testFail
;
31 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
35 * The sync style load test (for @import). This is, in a way, also an open bug for
36 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
37 * way to get a callback from when a stylesheet is loaded (that is, including any
38 * `@import` rules inside). To work around this, we'll have a little time loop to check
39 * if the styles apply.
41 * Note: This test originally used new Image() and onerror to get a callback
42 * when the url is loaded, but that is fragile since it doesn't monitor the
43 * same request as the css @import, and Safari 4 has issues with
44 * onerror/onload not being fired at all in weird cases like this.
46 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
48 el
= $element
.get( 0 ),
49 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
51 function isCssImportApplied() {
52 // Trigger reflow, repaint, redraw, whatever (cross-browser)
53 $element
.css( 'height' );
54 // eslint-disable-next-line no-unused-expressions
56 el
.className
= el
.className
;
57 // eslint-disable-next-line no-unused-expressions
58 document
.documentElement
.clientHeight
;
60 return $element
.css( prop
) === val
;
63 function styleTestLoop() {
64 var styleTestSince
= new Date().getTime() - styleTestStart
;
65 // If it is passing or if we timed out, run the real test and stop the loop
66 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
67 assert
.equal( $element
.css( prop
), val
,
68 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
77 // Otherwise, keep polling
78 setTimeout( styleTestLoop
);
82 styleTestStart
= new Date().getTime();
86 function urlStyleTest( selector
, prop
, val
) {
88 mw
.config
.get( 'wgScriptPath' ) +
89 '/tests/qunit/data/styleTest.css.php?' +
98 QUnit
.test( 'Basic', function ( assert
) {
101 mw
.loader
.testCallback = function () {
102 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
103 isAwesomeDone
= true;
106 mw
.loader
.implement( 'test.callback', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
108 return mw
.loader
.using( 'test.callback', function () {
109 assert
.strictEqual( isAwesomeDone
, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
111 assert
.ok( false, 'Error callback fired while loader.using "test.callback" module' );
115 QUnit
.test( 'Object method as module name', function ( assert
) {
118 mw
.loader
.testCallback = function () {
119 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
120 isAwesomeDone
= true;
123 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
125 return mw
.loader
.using( 'hasOwnProperty', function () {
126 assert
.strictEqual( isAwesomeDone
, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
128 assert
.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
132 QUnit
.test( '.using( .. ) Promise', function ( assert
) {
135 mw
.loader
.testCallback = function () {
136 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
137 isAwesomeDone
= true;
140 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
142 return mw
.loader
.using( 'test.promise' )
144 assert
.strictEqual( isAwesomeDone
, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
147 assert
.ok( false, 'Error callback fired while loader.using "test.promise" module' );
151 // Covers mw.loader#sortDependencies (with native Set if available)
152 QUnit
.test( '.using() Error: Circular dependency [StringSet default]', function ( assert
) {
153 var done
= assert
.async();
155 mw
.loader
.register( [
156 [ 'test.circle1', '0', [ 'test.circle2' ] ],
157 [ 'test.circle2', '0', [ 'test.circle3' ] ],
158 [ 'test.circle3', '0', [ 'test.circle1' ] ]
160 mw
.loader
.using( 'test.circle3' ).then(
162 assert
.ok( false, 'Unexpected resolution, expected error.' );
165 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
171 // @covers mw.loader#sortDependencies (with fallback shim)
172 QUnit
.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert
) {
173 var done
= assert
.async();
181 this.nativeSet
= window
.Set
;
182 window
.Set
= undefined;
183 mw
.redefineFallbacksForTest();
185 mw
.loader
.register( [
186 [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
187 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
188 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
190 mw
.loader
.using( 'test.shim.circle3' ).then(
192 assert
.ok( false, 'Unexpected resolution, expected error.' );
195 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
201 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
203 mw
.loader
.register( [
204 [ 'test.circleA', '0', [ 'test.circleB' ] ],
205 [ 'test.circleB', '0', [ 'test.circleC' ] ],
206 [ 'test.circleC', '0', [ 'test.circleA' ] ]
208 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
211 error
: data
.exception
&& data
.exception
.message
,
216 mw
.loader
.load( 'test.circleC' );
219 topic
: 'resourceloader.exception',
220 error
: 'Circular reference detected: test.circleB -> test.circleC',
224 'Detect circular dependency'
228 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
229 var done
= assert
.async();
231 mw
.loader
.using( 'test.using.unreg' ).then(
233 assert
.ok( false, 'Unexpected resolution, expected error.' );
236 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
241 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
243 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
246 error
: data
.exception
&& data
.exception
.message
,
251 mw
.loader
.load( 'test.load.unreg' );
254 topic
: 'resourceloader.exception',
255 error
: 'Unknown dependency: test.load.unreg',
262 // Regression test for T36853
263 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
265 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
268 error
: data
.exception
&& data
.exception
.message
,
273 mw
.loader
.register( [
274 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
275 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
277 mw
.loader
.load( 'test.load.missingdep' );
280 topic
: 'resourceloader.exception',
281 error
: 'Unknown dependency: test.load.missingdep2',
288 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
289 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
292 $element
.css( 'float' ),
301 $element
.css( 'float' ),
307 all
: '.mw-test-implement-a { float: right; }'
311 return mw
.loader
.using( 'test.implement.a' );
314 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
315 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
316 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
317 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
318 done
= assert
.async();
321 $element1
.css( 'text-align' ),
326 $element2
.css( 'float' ),
331 $element3
.css( 'text-align' ),
339 // Note: done() must only be called when the entire test is
340 // complete. So, make sure that we don't start until *both*
341 // assertStyleAsync calls have completed.
343 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
344 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
347 if ( pending
=== 0 ) {
351 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
352 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
355 if ( pending
=== 0 ) {
362 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
364 // T42834: Make sure it actually works with more than 1 stylesheet reference
365 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
366 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
372 mw
.loader
.load( 'test.implement.b' );
375 // Backwards compatibility
376 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
377 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
380 $element
.css( 'float' ),
389 $element
.css( 'float' ),
395 all
: '.mw-test-implement-c { float: right; }'
399 return mw
.loader
.using( 'test.implement.c' );
402 // Backwards compatibility
403 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
404 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
405 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
406 done
= assert
.async();
409 $element
.css( 'float' ),
414 $element2
.css( 'text-align' ),
422 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
423 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
428 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
429 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
433 mw
.loader
.load( 'test.implement.d' );
437 QUnit
.test( '.implement( styles has @import )', function ( assert
) {
438 var isJsExecuted
, $element
,
439 done
= assert
.async();
442 'test.implement.import',
444 assert
.strictEqual( isJsExecuted
, undefined, 'script not executed multiple times' );
447 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'executing', 'module state during implement() script execution' );
449 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
451 assert
.equal( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
453 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
454 assert
.equal( $element
.css( 'text-align' ), 'center',
455 'CSS styles after the @import rule are working'
464 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
466 + '.mw-test-implement-import { text-align: center; }'
470 'test-foobar': 'Hello Foobar, $1!'
474 mw
.loader
.using( 'test.implement.import' ).always( function () {
475 assert
.strictEqual( isJsExecuted
, true, 'script executed' );
476 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'ready', 'module state after script execution' );
480 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
481 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
482 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
485 $element
.css( 'float' ),
490 $element2
.css( 'float' ),
495 mw
.loader
.register( [
496 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
497 [ 'test.implement.e2', '0' ]
504 $element
.css( 'float' ),
506 'Depending module\'s style is applied'
510 all
: '.mw-test-implement-e { float: right; }'
518 $element2
.css( 'float' ),
520 'Dependency\'s style is applied'
524 all
: '.mw-test-implement-e2 { float: left; }'
528 return mw
.loader
.using( 'test.implement.e' );
531 QUnit
.test( '.implement( only scripts )', function ( assert
) {
532 mw
.loader
.implement( 'test.onlyscripts', function () {} );
533 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
536 QUnit
.test( '.implement( only messages )', function ( assert
) {
537 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
539 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
541 return mw
.loader
.using( 'test.implement.msgs', function () {
542 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
544 assert
.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
548 QUnit
.test( '.implement( empty )', function ( assert
) {
549 mw
.loader
.implement( 'test.empty' );
550 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
553 // @covers mw.loader#batchRequest
554 // This is a regression test because in the past we called getCombinedVersion()
555 // for all requested modules, before url splitting took place.
556 // Discovered as part of T188076, but not directly related.
557 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
558 mw
.loader
.register( [
559 // [module, version, dependencies, group, source]
560 [ 'testUrlInc', 'url', [], null, 'testloader' ],
561 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
564 mw
.config
.set( 'wgResourceLoaderMaxQueryLength', 10 );
566 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
568 require( 'testUrlIncDump' ).query
,
570 modules
: 'testUrlIncDump',
571 // Expected: Wrapped hash just for this one module
572 // $hash = hash( 'fnv132', 'dump');
573 // base_convert( $hash, 16, 36 ); // "13e9zzn"
574 // Previously: Wrapped hash for both modules, despite being in separate requests
575 // $hash = hash( 'fnv132', 'urldump' );
576 // base_convert( $hash, 16, 36 ); // "18kz9ca"
582 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
586 // @covers mw.loader#batchRequest
587 // @covers mw.loader#buildModulesString
588 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
589 mw
.loader
.register( [
590 // [module, version, dependencies, group, source]
591 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
592 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
593 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
594 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
597 return mw
.loader
.using( [
602 ] ).then( function ( require
) {
604 require( 'testUrlOrderDump' ).query
,
606 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
607 // Expected: Combined in order after string packing
608 // $hash = hash( 'fnv132', 'urldump12' );
609 // base_convert( $hash, 16, 36 ); // "1knqzan"
610 // Previously: Combined in order of before string packing
611 // $hash = hash( 'fnv132', 'url12dump' );
612 // base_convert( $hash, 16, 36 ); // "11eo3in"
620 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
621 // don't emit an error event
622 this.sandbox
.stub( mw
, 'track' );
624 mw
.loader
.register( [
625 [ 'test.module1', '0' ],
626 [ 'test.module2', '0', [ 'test.module1' ] ],
627 [ 'test.module3', '0', [ 'test.module2' ] ]
629 mw
.loader
.implement( 'test.module1', function () {
630 throw new Error( 'expected' );
632 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
633 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
634 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
636 assert
.strictEqual( mw
.track
.callCount
, 1 );
639 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
640 mw
.loader
.register( [
641 [ 'test.module4', '0' ],
642 [ 'test.module5', '0', [ 'test.module4' ] ],
643 [ 'test.module6', '0', [ 'test.module5' ] ]
645 mw
.loader
.implement( 'test.module4', function () {} );
646 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
647 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
648 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
649 mw
.loader
.implement( 'test.module6', function () {} );
650 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
651 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
652 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
653 mw
.loader
.implement( 'test.module5', function () {} );
654 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
655 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
656 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
659 QUnit
.test( 'Missing dependency', function ( assert
) {
660 mw
.loader
.register( [
661 [ 'test.module7', '0' ],
662 [ 'test.module8', '0', [ 'test.module7' ] ],
663 [ 'test.module9', '0', [ 'test.module8' ] ]
665 mw
.loader
.implement( 'test.module8', function () {} );
666 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
667 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
668 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
669 mw
.loader
.state( 'test.module7', 'missing' );
670 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
671 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
672 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
673 mw
.loader
.implement( 'test.module9', function () {} );
674 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
675 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
676 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
680 assert
.ok( false, 'Success fired despite missing dependency' );
681 assert
.ok( true, 'QUnit expected() count dummy' );
683 function ( e
, dependencies
) {
684 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
685 assert
.deepEqual( dependencies
, [ 'test.module7' ], 'Error callback called with module test.module7' );
691 assert
.ok( false, 'Success fired despite missing dependency' );
692 assert
.ok( true, 'QUnit expected() count dummy' );
694 function ( e
, dependencies
) {
695 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
699 [ 'test.module7', 'test.module8', 'test.module9' ],
700 'Error callback called with all three modules as dependencies'
706 QUnit
.test( 'Dependency handling', function ( assert
) {
707 var done
= assert
.async();
708 mw
.loader
.register( [
709 // [module, version, dependencies, group, source]
710 [ 'testMissing', '1', [], null, 'testloader' ],
711 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
712 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
715 function verifyModuleStates() {
716 assert
.equal( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
717 assert
.equal( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
718 assert
.equal( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
721 mw
.loader
.using( [ 'testUsesNestedMissing' ],
723 assert
.ok( false, 'Error handler should be invoked.' );
724 assert
.ok( true ); // Dummy to reach QUnit expect()
726 verifyModuleStates();
730 function ( e
, badmodules
) {
731 assert
.ok( true, 'Error handler should be invoked.' );
732 // As soon as server spits out state('testMissing', 'missing');
733 // it will bubble up and trigger the error callback.
734 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
735 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
737 verifyModuleStates();
744 QUnit
.test( 'Skip-function handling', function ( assert
) {
745 mw
.loader
.register( [
746 // [module, version, dependencies, group, source, skip]
747 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
748 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
749 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
752 function verifyModuleStates() {
753 assert
.equal( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
754 assert
.equal( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
755 assert
.equal( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
758 return mw
.loader
.using( [ 'testUsesSkippable' ],
760 assert
.ok( true, 'Success handler should be invoked.' );
761 assert
.ok( true ); // Dummy to match error handler and reach QUnit expect()
763 verifyModuleStates();
765 function ( e
, badmodules
) {
766 assert
.ok( false, 'Error handler should not be invoked.' );
767 assert
.deepEqual( badmodules
, [], 'Bad modules as expected.' );
769 verifyModuleStates();
774 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
775 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
777 done
= assert
.async();
779 // URL to the callback script
780 target
= QUnit
.fixurl(
781 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
783 // Ensure a protocol-relative URL for this test
784 target
= target
.replace( /https?:/, '' );
785 assert
.equal( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
787 mw
.loader
.testCallback = function () {
788 // Ensure once, delete now
789 delete mw
.loader
.testCallback
;
790 assert
.ok( true, 'callback' );
795 mw
.loader
.load( target
);
798 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
800 done
= assert
.async();
802 // URL to the callback script
803 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
804 assert
.equal( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
806 mw
.loader
.testCallback = function () {
807 // Ensure once, delete now
808 delete mw
.loader
.testCallback
;
809 assert
.ok( true, 'callback' );
814 mw
.loader
.load( target
);
817 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
820 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
822 mw
.loader
.register( '', 'v1' );
823 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
824 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
826 mw
.loader
.implement( '', function () {
830 return mw
.loader
.using( '', function () {
831 assert
.strictEqual( done
, true, 'script ran' );
832 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
836 QUnit
.test( 'Executing race - T112232', function ( assert
) {
839 // The red herring schedules its CSS buffer first. In T112232, a bug in the
840 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
842 'testRaceRedHerring',
844 { css
: [ '.mw-testRaceRedHerring {}' ] }
851 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
854 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
855 return mw
.loader
.using( 'testRaceLoadMe', function () {
856 assert
.strictEqual( done
, true, 'script ran' );
857 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
861 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
863 mw
.loader
.store
.enabled
= true;
864 mw
.loader
.register( 'test.stale', 'v2' );
865 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
867 mw
.loader
.implement( 'test.stale@v1', function () {
871 return mw
.loader
.using( 'test.stale' )
873 assert
.strictEqual( count
, 1 );
874 // After implementing, registry contains version as implemented by the response.
875 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
876 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
877 assert
.ok( mw
.loader
.store
.get( 'test.stale' ), 'In store' );
880 // Reset run time, but keep mw.loader.store
881 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
882 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
883 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
885 // Module was stored correctly as v1
886 // On future navigations, it will be ignored until evicted
887 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
891 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
893 mw
.loader
.store
.enabled
= true;
894 mw
.loader
.register( 'test.stalebc', 'v2' );
895 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
897 mw
.loader
.implement( 'test.stalebc', function () {
901 return mw
.loader
.using( 'test.stalebc' )
903 assert
.strictEqual( count
, 1 );
904 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
905 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
908 // Reset run time, but keep mw.loader.store
909 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
910 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
911 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
913 // Legacy behaviour is storing under the expected version,
914 // which woudl lead to whitewashing and stale values (T117587).
915 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
919 QUnit
.test( 'require()', function ( assert
) {
920 mw
.loader
.register( [
921 [ 'test.require1', '0' ],
922 [ 'test.require2', '0' ],
923 [ 'test.require3', '0' ],
924 [ 'test.require4', '0', [ 'test.require3' ] ]
926 mw
.loader
.implement( 'test.require1', function () {} );
927 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
930 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
931 module
.exports = function () {
932 return 'hello world';
935 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
936 var other
= require( 'test.require3' );
943 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
944 var module1
, module2
, module3
, module4
;
946 module1
= require( 'test.require1' );
947 module2
= require( 'test.require2' );
948 module3
= require( 'test.require3' );
949 module4
= require( 'test.require4' );
951 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
952 assert
.strictEqual( module2
, 1, 'export a number' );
953 assert
.strictEqual( module3(), 'hello world', 'export a function' );
954 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
955 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
957 assert
.throws( function () {
958 require( '_badmodule' );
959 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
963 QUnit
.test( 'require() in debug mode', function ( assert
) {
964 var path
= mw
.config
.get( 'wgScriptPath' );
965 mw
.loader
.register( [
966 [ 'test.require.define', '0' ],
967 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
969 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
970 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
972 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
973 var cb
= require( 'test.require.callback' );
974 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
975 // Must use try-catch because cb.later() will throw if require is undefined,
976 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
978 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
980 assert
.equal( null, String( e
), 'require works asynchrously in debug mode' );
983 assert
.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
987 QUnit
.test( 'Implicit dependencies', function ( assert
) {
989 userSeesSite
= false,
1001 userSeesSite
= ranSite
;
1006 assert
.strictEqual( ranSite
, false, 'verify site module not yet loaded' );
1007 assert
.strictEqual( ranUser
, false, 'verify user module not yet loaded' );
1008 return mw
.loader
.using( 'user', function () {
1009 assert
.strictEqual( ranSite
, true, 'ran site module' );
1010 assert
.strictEqual( ranUser
, true, 'ran user module' );
1011 assert
.strictEqual( userSeesSite
, true, 'ran site before user module' );
1014 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
1015 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
1019 }( mediaWiki
, jQuery
) );