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
);
9 this.useStubClock = function () {
10 this.clock
= this.sandbox
.useFakeTimers();
11 this.tick = function ( forward
) {
12 return this.clock
.tick( forward
|| 1 );
14 this.sandbox
.stub( mw
, 'requestIdleCallback', mw
.requestIdleCallbackInternal
);
17 teardown: function () {
18 mw
.loader
.maxQueryLength
= 2000;
19 // Teardown for StringSet shim test
20 if ( this.nativeSet
) {
21 window
.Set
= this.nativeSet
;
22 mw
.redefineFallbacksForTest();
24 if ( this.resetStoreKey
) {
25 localStorage
.removeItem( mw
.loader
.store
.key
);
27 // Remove any remaining temporary statics
28 // exposed for cross-file mocks.
29 delete mw
.loader
.testCallback
;
30 delete mw
.loader
.testFail
;
31 delete mw
.getScriptExampleScriptLoaded
;
35 mw
.loader
.addSource( {
37 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
41 * The sync style load test, for @import. This is, in a way, also an open bug for
42 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
43 * way to get a callback from when a stylesheet is loaded (that is, including any
44 * `@import` rules inside). To work around this, we'll have a little time loop to check
45 * if the styles apply.
47 * Note: This test originally used new Image() and onerror to get a callback
48 * when the url is loaded, but that is fragile since it doesn't monitor the
49 * same request as the css @import, and Safari 4 has issues with
50 * onerror/onload not being fired at all in weird cases like this.
52 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
54 el
= $element
.get( 0 ),
55 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
57 function isCssImportApplied() {
58 // Trigger reflow, repaint, redraw, whatever (cross-browser)
59 $element
.css( 'height' );
60 // eslint-disable-next-line no-unused-expressions
62 // eslint-disable-next-line no-self-assign
63 el
.className
= el
.className
;
64 // eslint-disable-next-line no-unused-expressions
65 document
.documentElement
.clientHeight
;
67 return $element
.css( prop
) === val
;
70 function styleTestLoop() {
71 var styleTestSince
= new Date().getTime() - styleTestStart
;
72 // If it is passing or if we timed out, run the real test and stop the loop
73 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
74 assert
.strictEqual( $element
.css( prop
), val
,
75 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
84 // Otherwise, keep polling
85 setTimeout( styleTestLoop
);
89 styleTestStart
= new Date().getTime();
93 function urlStyleTest( selector
, prop
, val
) {
95 mw
.config
.get( 'wgScriptPath' ) +
96 '/tests/qunit/data/styleTest.css.php?' +
105 QUnit
.test( '.using( .., Function callback ) Promise', function ( assert
) {
106 var script
= 0, callback
= 0;
107 mw
.loader
.testCallback = function () {
110 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
112 return mw
.loader
.using( 'test.promise', function () {
114 } ).then( function () {
115 assert
.strictEqual( script
, 1, 'module script ran' );
116 assert
.strictEqual( callback
, 1, 'using() callback ran' );
120 QUnit
.test( 'Prototype method as module name', function ( assert
) {
122 mw
.loader
.testCallback = function () {
125 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
127 return mw
.loader
.using( 'hasOwnProperty', function () {
128 assert
.strictEqual( call
, 1, 'module script ran' );
132 // Covers mw.loader#sortDependencies (with native Set if available)
133 QUnit
.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert
) {
134 var done
= assert
.async();
136 mw
.loader
.register( [
137 [ 'test.set.circleA', '0', [ 'test.set.circleB' ] ],
138 [ 'test.set.circleB', '0', [ 'test.set.circleC' ] ],
139 [ 'test.set.circleC', '0', [ 'test.set.circleA' ] ]
141 mw
.loader
.using( 'test.set.circleC' ).then(
143 assert
.ok( false, 'Unexpected resolution, expected error.' );
146 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
152 // @covers mw.loader#sortDependencies (with fallback shim)
153 QUnit
.test( '.using() - Error: Circular dependency [StringSet shim]', function ( assert
) {
154 var done
= assert
.async();
162 this.nativeSet
= window
.Set
;
163 window
.Set
= undefined;
164 mw
.redefineFallbacksForTest();
166 mw
.loader
.register( [
167 [ 'test.shim.circleA', '0', [ 'test.shim.circleB' ] ],
168 [ 'test.shim.circleB', '0', [ 'test.shim.circleC' ] ],
169 [ 'test.shim.circleC', '0', [ 'test.shim.circleA' ] ]
171 mw
.loader
.using( 'test.shim.circleC' ).then(
173 assert
.ok( false, 'Unexpected resolution, expected error.' );
176 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
182 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
184 mw
.loader
.register( [
185 [ 'test.load.circleA', '0', [ 'test.load.circleB' ] ],
186 [ 'test.load.circleB', '0', [ 'test.load.circleC' ] ],
187 [ 'test.load.circleC', '0', [ 'test.load.circleA' ] ]
189 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
192 error
: data
.exception
&& data
.exception
.message
,
197 mw
.loader
.load( 'test.load.circleC' );
200 topic
: 'resourceloader.exception',
201 error
: 'Circular reference detected: test.load.circleB -> test.load.circleC',
205 'Detect circular dependency'
209 QUnit
.test( '.load() - Error: Circular dependency (direct)', function ( assert
) {
211 mw
.loader
.register( [
212 [ 'test.load.circleDirect', '0', [ 'test.load.circleDirect' ] ]
214 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
217 error
: data
.exception
&& data
.exception
.message
,
222 mw
.loader
.load( 'test.load.circleDirect' );
225 topic
: 'resourceloader.exception',
226 error
: 'Circular reference detected: test.load.circleDirect -> test.load.circleDirect',
230 'Detect a direct self-dependency'
234 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
235 var done
= assert
.async();
237 mw
.loader
.using( 'test.using.unreg' ).then(
239 assert
.ok( false, 'Unexpected resolution, expected error.' );
242 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
247 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
249 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
252 error
: data
.exception
&& data
.exception
.message
,
257 mw
.loader
.load( 'test.load.unreg' );
260 topic
: 'resourceloader.exception',
261 error
: 'Unknown module: test.load.unreg',
268 // Regression test for T36853
269 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
271 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
274 error
: data
.exception
&& data
.exception
.message
,
279 mw
.loader
.register( [
280 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
281 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
283 mw
.loader
.load( 'test.load.missingdep' );
286 topic
: 'resourceloader.exception',
287 error
: 'Unknown module: test.load.missingdep2',
294 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
295 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
298 $element
.css( 'float' ),
307 $element
.css( 'float' ),
313 all
: '.mw-test-implement-a { float: right; }'
317 return mw
.loader
.using( 'test.implement.a' );
320 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
321 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
322 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
323 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
324 done
= assert
.async();
327 $element1
.css( 'text-align' ),
332 $element2
.css( 'float' ),
337 $element3
.css( 'text-align' ),
345 // Note: done() must only be called when the entire test is
346 // complete. So, make sure that we don't start until *both*
347 // assertStyleAsync calls have completed.
349 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
350 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
353 if ( pending
=== 0 ) {
357 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
358 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
361 if ( pending
=== 0 ) {
368 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
370 // T42834: Make sure it actually works with more than 1 stylesheet reference
371 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
372 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
378 mw
.loader
.load( 'test.implement.b' );
381 // Backwards compatibility
382 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
383 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
386 $element
.css( 'float' ),
395 $element
.css( 'float' ),
401 all
: '.mw-test-implement-c { float: right; }'
405 return mw
.loader
.using( 'test.implement.c' );
408 // Backwards compatibility
409 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
410 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
411 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
412 done
= assert
.async();
415 $element
.css( 'float' ),
420 $element2
.css( 'text-align' ),
428 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
429 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
434 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
435 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
439 mw
.loader
.load( 'test.implement.d' );
442 QUnit
.test( '.implement( messages before script )', function ( assert
) {
444 'test.implement.order',
446 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'executing', 'state during script execution' );
447 assert
.strictEqual( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
451 'test-foobar': 'Hello Foobar, $1!'
455 return mw
.loader
.using( 'test.implement.order' ).then( function () {
456 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'ready', 'final success state' );
461 QUnit
.test( '.implement( styles with @import )', function ( assert
) {
463 done
= assert
.async();
466 'test.implement.import',
468 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
470 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
471 assert
.strictEqual( $element
.css( 'text-align' ), 'center',
472 'CSS styles after the @import rule are working'
481 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
483 + '.mw-test-implement-import { text-align: center; }'
488 return mw
.loader
.using( 'test.implement.import' );
491 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
492 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
493 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
496 $element
.css( 'float' ),
501 $element2
.css( 'float' ),
506 mw
.loader
.register( [
507 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
508 [ 'test.implement.e2', '0' ]
515 $element
.css( 'float' ),
517 'Depending module\'s style is applied'
521 all
: '.mw-test-implement-e { float: right; }'
529 $element2
.css( 'float' ),
531 'Dependency\'s style is applied'
535 all
: '.mw-test-implement-e2 { float: left; }'
539 return mw
.loader
.using( 'test.implement.e' );
542 QUnit
.test( '.implement( only scripts )', function ( assert
) {
543 mw
.loader
.implement( 'test.onlyscripts', function () {} );
544 return mw
.loader
.using( 'test.onlyscripts', function () {
545 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
549 QUnit
.test( '.implement( only messages )', function ( assert
) {
550 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
552 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
554 return mw
.loader
.using( 'test.implement.msgs', function () {
555 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
559 QUnit
.test( '.implement( empty )', function ( assert
) {
560 mw
.loader
.implement( 'test.empty' );
561 return mw
.loader
.using( 'test.empty', function () {
562 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
566 QUnit
.test( '.implement( package files )', function ( assert
) {
567 var done
= assert
.async(),
571 'test.implement.packageFiles',
573 main
: 'resources/src/foo/init.js',
575 'resources/src/foo/data/hello.json': { hello
: 'world' },
576 'resources/src/foo/foo.js': function ( require
, module
) {
578 module
.exports
= { answer
: counter
};
580 'resources/src/bar/bar.js': function ( require
, module
) {
581 var core
= require( './core.js' );
582 module
.exports
= { data
: core
.sayHello( 'Alice' ) };
584 'resources/src/bar/core.js': function ( require
, module
) {
585 module
.exports
= { sayHello: function ( name
) {
586 return 'Hello ' + name
;
589 'resources/src/foo/init.js': function ( require
) {
591 assert
.deepEqual( require( './data/hello.json' ), { hello
: 'world' }, 'require() with .json file' );
592 assert
.deepEqual( require( './foo.js' ), { answer
: 42 }, 'require() with .js file in same directory' );
593 assert
.deepEqual( require( '../bar/bar.js' ), { data
: 'Hello Alice' }, 'require() with ../ of a file that uses same-directory require()' );
594 assert
.deepEqual( require( './foo.js' ), { answer
: 42 }, 'require()ing the same script twice only runs it once' );
602 mw
.loader
.using( 'test.implement.packageFiles' ).done( function () {
603 assert
.ok( initJsRan
, 'main JS file is executed' );
608 QUnit
.test( '.addSource()', function ( assert
) {
609 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
611 assert
.throws( function () {
612 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
613 }, /already registered/, 'duplicate pair from addSource(Object)' );
615 assert
.throws( function () {
616 mw
.loader
.addSource( { testsource1
: 'https://1.test/src-diff' } );
617 }, /already registered/, 'duplicate ID from addSource(Object)' );
620 // @covers mw.loader#batchRequest
621 // This is a regression test because in the past we called getCombinedVersion()
622 // for all requested modules, before url splitting took place.
623 // Discovered as part of T188076, but not directly related.
624 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
625 mw
.loader
.register( [
626 // [module, version, dependencies, group, source]
627 [ 'testUrlInc', 'url', [], null, 'testloader' ],
628 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
631 mw
.loader
.maxQueryLength
= 10;
633 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
635 require( 'testUrlIncDump' ).query
,
637 modules
: 'testUrlIncDump',
638 // Expected: Combine hashes only for the module in the specific HTTP request
639 // hash fnv132 => "13e9zzn"
640 // Wrong: Combine hashes for all requested modules, before request-splitting
641 // hash fnv132 => "18kz9ca"
647 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
651 // @covers mw.loader#batchRequest
652 // @covers mw.loader#buildModulesString
653 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
654 mw
.loader
.register( [
655 // [module, version, dependencies, group, source]
656 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
657 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
658 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
659 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
662 return mw
.loader
.using( [
667 ] ).then( function ( require
) {
669 require( 'testUrlOrderDump' ).query
,
671 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
672 // Expected: Combined by sorting names after string packing
673 // hash fnv132 = "1knqzan"
674 // Wrong: Combined by sorting names before string packing
675 // hash fnv132 => "11eo3in"
683 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
686 // Don't actually emit an error event
687 this.sandbox
.stub( mw
, 'track' );
689 mw
.loader
.register( [
690 [ 'test.module1', '0' ],
691 [ 'test.module2', '0', [ 'test.module1' ] ],
692 [ 'test.module3', '0', [ 'test.module2' ] ]
694 mw
.loader
.implement( 'test.module1', function () {
695 throw new Error( 'expected' );
699 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
700 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
701 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
703 assert
.strictEqual( mw
.track
.callCount
, 1 );
706 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
709 mw
.loader
.register( [
710 [ 'test.module4', '0' ],
711 [ 'test.module5', '0', [ 'test.module4' ] ],
712 [ 'test.module6', '0', [ 'test.module5' ] ]
715 mw
.loader
.implement( 'test.module4', function () {} );
717 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
718 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
719 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
721 mw
.loader
.implement( 'test.module6', function () {} );
723 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
724 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
725 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
727 mw
.loader
.implement( 'test.module5', function () {} );
729 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
730 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
731 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
734 QUnit
.test( 'Missing dependency', function ( assert
) {
737 mw
.loader
.register( [
738 [ 'test.module7', '0' ],
739 [ 'test.module8', '0', [ 'test.module7' ] ],
740 [ 'test.module9', '0', [ 'test.module8' ] ]
743 mw
.loader
.implement( 'test.module8', function () {} );
745 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
746 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
747 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
749 mw
.loader
.state( { 'test.module7': 'missing' } );
751 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
752 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
753 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
755 mw
.loader
.implement( 'test.module9', function () {} );
757 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
758 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
759 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
761 // Restore clock for QUnit and $.Deferred internals
762 this.clock
.restore();
763 return mw
.loader
.using( [ 'test.module7' ] ).then(
765 throw new Error( 'Success fired despite missing dependency' );
767 function ( e
, dependencies
) {
768 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
771 [ 'jquery', 'mediawiki.base', 'test.module7' ],
772 'Error callback called with module test.module7'
775 ).then( function () {
776 return mw
.loader
.using( [ 'test.module9' ] );
779 throw new Error( 'Success fired despite missing dependency' );
781 function ( e
, dependencies
) {
782 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
786 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
787 'Error callback called with all three modules as dependencies'
793 QUnit
.test( 'Dependency handling', function ( assert
) {
794 mw
.loader
.register( [
795 // [module, version, dependencies, group, source]
796 [ 'testMissing', '1', [], null, 'testloader' ],
797 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
798 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
801 function verifyModuleStates() {
802 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
803 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
804 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
807 return mw
.loader
.using( [ 'testUsesNestedMissing' ] ).then(
809 verifyModuleStates();
810 throw new Error( 'Error handler should be invoked.' );
812 function ( e
, modules
) {
813 // When the server sets state of 'testMissing' to 'missing'
814 // it should bubble up and trigger the error callback of the job for 'testUsesNestedMissing'.
815 assert
.strictEqual( modules
.indexOf( 'testMissing' ) !== -1, true, 'Triggered by testMissing.' );
817 verifyModuleStates();
822 QUnit
.test( 'Skip-function handling', function ( assert
) {
823 mw
.loader
.register( [
824 // [module, version, dependencies, group, source, skip]
825 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
826 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
827 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
830 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
832 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
833 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
834 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
836 function ( e
, badmodules
) {
837 // Should not fail and QUnit would already catch this,
838 // but add a handler anyway to report details from 'badmodules
839 assert
.deepEqual( badmodules
, [], 'Bad modules' );
844 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
845 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
847 done
= assert
.async();
849 // URL to the callback script
850 target
= QUnit
.fixurl(
851 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
853 // Ensure a protocol-relative URL for this test
854 target
= target
.replace( /https?:/, '' );
855 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
857 mw
.loader
.testCallback = function () {
858 // Ensure once, delete now
859 delete mw
.loader
.testCallback
;
860 assert
.ok( true, 'callback' );
865 mw
.loader
.load( target
);
868 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
870 done
= assert
.async();
872 // URL to the callback script
873 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
874 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
876 mw
.loader
.testCallback = function () {
877 // Ensure once, delete now
878 delete mw
.loader
.testCallback
;
879 assert
.ok( true, 'callback' );
884 mw
.loader
.load( target
);
887 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
890 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
892 mw
.loader
.register( '', 'v1' );
893 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
894 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
896 mw
.loader
.implement( '', function () {
900 return mw
.loader
.using( '', function () {
901 assert
.strictEqual( done
, true, 'script ran' );
902 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
906 QUnit
.test( 'Executing race - T112232', function ( assert
) {
909 // The red herring schedules its CSS buffer first. In T112232, a bug in the
910 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
912 'testRaceRedHerring',
914 { css
: [ '.mw-testRaceRedHerring {}' ] }
921 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
924 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
925 return mw
.loader
.using( 'testRaceLoadMe', function () {
926 assert
.strictEqual( done
, true, 'script ran' );
927 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
931 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
933 // Enable store and stub timeout/idle scheduling
934 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
935 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
938 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
942 mw
.loader
.register( 'test.stale', 'v2' );
943 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
945 mw
.loader
.implement( 'test.stale@v1', function () {
949 return mw
.loader
.using( 'test.stale' )
951 assert
.strictEqual( count
, 1 );
952 // After implementing, registry contains version as implemented by the response.
953 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
954 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
955 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stale' ), 'string', 'In store' );
958 // Reset run time, but keep mw.loader.store
959 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
960 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
961 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
963 // Module was stored correctly as v1
964 // On future navigations, it will be ignored until evicted
965 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
969 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
971 // Enable store and stub timeout/idle scheduling
972 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
973 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
976 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
980 mw
.loader
.register( 'test.stalebc', 'v2' );
981 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
983 mw
.loader
.implement( 'test.stalebc', function () {
987 return mw
.loader
.using( 'test.stalebc' )
989 assert
.strictEqual( script
, 1, 'module script ran' );
990 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
991 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stalebc' ), 'string', 'In store' );
994 // Reset run time, but keep mw.loader.store
995 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
996 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
997 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
999 // Legacy behaviour is storing under the expected version,
1000 // which woudl lead to whitewashing and stale values (T117587).
1001 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
1005 QUnit
.test( 'No storing of group=private responses', function ( assert
) {
1006 var name
= 'test.group.priv';
1008 // Enable store and stub timeout/idle scheduling
1009 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
1010 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
1013 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
1017 // See ResourceLoaderStartUpModule::$groupIds
1018 mw
.loader
.register( name
, 'x', [], 1 );
1019 assert
.strictEqual( mw
.loader
.store
.get( name
), false, 'Not in store' );
1021 mw
.loader
.implement( name
, function () {} );
1022 return mw
.loader
.using( name
).then( function () {
1023 assert
.strictEqual( mw
.loader
.getState( name
), 'ready' );
1024 assert
.strictEqual( mw
.loader
.store
.get( name
), false, 'Still not in store' );
1028 QUnit
.test( 'No storing of group=user responses', function ( assert
) {
1029 var name
= 'test.group.user';
1031 // Enable store and stub timeout/idle scheduling
1032 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
1033 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
1036 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
1040 // See ResourceLoaderStartUpModule::$groupIds
1041 mw
.loader
.register( name
, 'y', [], 0 );
1042 assert
.strictEqual( mw
.loader
.store
.get( name
), false, 'Not in store' );
1044 mw
.loader
.implement( name
, function () {} );
1045 return mw
.loader
.using( name
).then( function () {
1046 assert
.strictEqual( mw
.loader
.getState( name
), 'ready' );
1047 assert
.strictEqual( mw
.loader
.store
.get( name
), false, 'Still not in store' );
1051 QUnit
.test( 'mw.loader.store.init - Invalid JSON', function ( assert
) {
1053 this.sandbox
.stub( mw
.loader
.store
, 'enabled', null );
1054 this.sandbox
.stub( mw
.loader
.store
, 'items', {} );
1055 this.resetStoreKey
= true;
1056 localStorage
.setItem( mw
.loader
.store
.key
, 'invalid' );
1058 mw
.loader
.store
.init();
1059 assert
.strictEqual( mw
.loader
.store
.enabled
, true, 'Enabled' );
1061 $.isEmptyObject( mw
.loader
.store
.items
),
1063 'Items starts fresh'
1067 QUnit
.test( 'mw.loader.store.init - Wrong JSON', function ( assert
) {
1069 this.sandbox
.stub( mw
.loader
.store
, 'enabled', null );
1070 this.sandbox
.stub( mw
.loader
.store
, 'items', {} );
1071 this.resetStoreKey
= true;
1072 localStorage
.setItem( mw
.loader
.store
.key
, JSON
.stringify( { wrong
: true } ) );
1074 mw
.loader
.store
.init();
1075 assert
.strictEqual( mw
.loader
.store
.enabled
, true, 'Enabled' );
1077 $.isEmptyObject( mw
.loader
.store
.items
),
1079 'Items starts fresh'
1083 QUnit
.test( 'mw.loader.store.init - Expired JSON', function ( assert
) {
1085 this.sandbox
.stub( mw
.loader
.store
, 'enabled', null );
1086 this.sandbox
.stub( mw
.loader
.store
, 'items', {} );
1087 this.resetStoreKey
= true;
1088 localStorage
.setItem( mw
.loader
.store
.key
, JSON
.stringify( {
1089 items
: { use: 'not me' },
1090 vary
: mw
.loader
.store
.vary
,
1091 asOf
: 130161 // 2011-04-01 12:00
1094 mw
.loader
.store
.init();
1095 assert
.strictEqual( mw
.loader
.store
.enabled
, true, 'Enabled' );
1097 $.isEmptyObject( mw
.loader
.store
.items
),
1099 'Items starts fresh'
1103 QUnit
.test( 'mw.loader.store.init - Good JSON', function ( assert
) {
1105 this.sandbox
.stub( mw
.loader
.store
, 'enabled', null );
1106 this.sandbox
.stub( mw
.loader
.store
, 'items', {} );
1107 this.resetStoreKey
= true;
1108 localStorage
.setItem( mw
.loader
.store
.key
, JSON
.stringify( {
1109 items
: { use: 'me' },
1110 vary
: mw
.loader
.store
.vary
,
1111 asOf
: Math
.ceil( Date
.now() / 1e7
) - 5 // ~ 13 hours ago
1114 mw
.loader
.store
.init();
1115 assert
.strictEqual( mw
.loader
.store
.enabled
, true, 'Enabled' );
1117 mw
.loader
.store
.items
,
1119 'Stored items are loaded'
1123 QUnit
.test( 'require()', function ( assert
) {
1124 mw
.loader
.register( [
1125 [ 'test.require1', '0' ],
1126 [ 'test.require2', '0' ],
1127 [ 'test.require3', '0' ],
1128 [ 'test.require4', '0', [ 'test.require3' ] ]
1130 mw
.loader
.implement( 'test.require1', function () {} );
1131 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
1134 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
1135 module
.exports = function () {
1136 return 'hello world';
1139 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
1140 var other
= require( 'test.require3' );
1142 pizza: function () {
1147 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
1148 var module1
, module2
, module3
, module4
;
1150 module1
= require( 'test.require1' );
1151 module2
= require( 'test.require2' );
1152 module3
= require( 'test.require3' );
1153 module4
= require( 'test.require4' );
1155 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
1156 assert
.strictEqual( module2
, 1, 'export a number' );
1157 assert
.strictEqual( module3(), 'hello world', 'export a function' );
1158 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
1159 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
1161 assert
.throws( function () {
1162 require( '_badmodule' );
1163 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
1167 QUnit
.test( 'require() in debug mode', function ( assert
) {
1168 var path
= mw
.config
.get( 'wgScriptPath' );
1169 mw
.loader
.register( [
1170 [ 'test.require.define', '0' ],
1171 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
1173 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
1174 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
1176 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
1177 var cb
= require( 'test.require.callback' );
1178 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
1179 // Must use try-catch because cb.later() will throw if require is undefined,
1180 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
1182 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
1184 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
1189 QUnit
.test( 'Implicit dependencies', function ( assert
) {
1194 mw
.loader
.implement(
1200 mw
.loader
.implement(
1204 siteFromUser
= site
;
1208 return mw
.loader
.using( 'user', function () {
1209 assert
.strictEqual( site
, 1, 'site module' );
1210 assert
.strictEqual( user
, 1, 'user module' );
1211 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
1212 } ).always( function () {
1214 mw
.loader
.moduleRegistry
.site
.state
= 'registered';
1215 mw
.loader
.moduleRegistry
.user
.state
= 'registered';
1219 QUnit
.test( '.getScript() - success', function ( assert
) {
1220 var scriptUrl
= QUnit
.fixurl(
1221 mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mediawiki.loader.getScript.example.js'
1224 return mw
.loader
.getScript( scriptUrl
).then(
1226 assert
.strictEqual( mw
.getScriptExampleScriptLoaded
, true, 'Data attached to a global object is available' );
1231 QUnit
.test( '.getScript() - failure', function ( assert
) {
1233 mw
.loader
.getScript( 'https://example.test/not-found' ),
1234 /Failed to load script/,
1235 'Descriptive error message'