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 // Remove any remaining temporary statics
25 // exposed for cross-file mocks.
26 delete mw
.loader
.testCallback
;
27 delete mw
.loader
.testFail
;
28 delete mw
.getScriptExampleScriptLoaded
;
32 mw
.loader
.addSource( {
34 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
38 * The sync style load test, for @import. This is, in a way, also an open bug for
39 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
40 * way to get a callback from when a stylesheet is loaded (that is, including any
41 * `@import` rules inside). To work around this, we'll have a little time loop to check
42 * if the styles apply.
44 * Note: This test originally used new Image() and onerror to get a callback
45 * when the url is loaded, but that is fragile since it doesn't monitor the
46 * same request as the css @import, and Safari 4 has issues with
47 * onerror/onload not being fired at all in weird cases like this.
49 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
51 el
= $element
.get( 0 ),
52 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
54 function isCssImportApplied() {
55 // Trigger reflow, repaint, redraw, whatever (cross-browser)
56 $element
.css( 'height' );
57 // eslint-disable-next-line no-unused-expressions
59 // eslint-disable-next-line no-self-assign
60 el
.className
= el
.className
;
61 // eslint-disable-next-line no-unused-expressions
62 document
.documentElement
.clientHeight
;
64 return $element
.css( prop
) === val
;
67 function styleTestLoop() {
68 var styleTestSince
= new Date().getTime() - styleTestStart
;
69 // If it is passing or if we timed out, run the real test and stop the loop
70 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
71 assert
.strictEqual( $element
.css( prop
), val
,
72 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
81 // Otherwise, keep polling
82 setTimeout( styleTestLoop
);
86 styleTestStart
= new Date().getTime();
90 function urlStyleTest( selector
, prop
, val
) {
92 mw
.config
.get( 'wgScriptPath' ) +
93 '/tests/qunit/data/styleTest.css.php?' +
102 QUnit
.test( '.using( .., Function callback ) Promise', function ( assert
) {
103 var script
= 0, callback
= 0;
104 mw
.loader
.testCallback = function () {
107 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
109 return mw
.loader
.using( 'test.promise', function () {
111 } ).then( function () {
112 assert
.strictEqual( script
, 1, 'module script ran' );
113 assert
.strictEqual( callback
, 1, 'using() callback ran' );
117 QUnit
.test( 'Prototype method as module name', function ( assert
) {
119 mw
.loader
.testCallback = function () {
122 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
124 return mw
.loader
.using( 'hasOwnProperty', function () {
125 assert
.strictEqual( call
, 1, 'module script ran' );
129 // Covers mw.loader#sortDependencies (with native Set if available)
130 QUnit
.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert
) {
131 var done
= assert
.async();
133 mw
.loader
.register( [
134 [ 'test.set.circleA', '0', [ 'test.set.circleB' ] ],
135 [ 'test.set.circleB', '0', [ 'test.set.circleC' ] ],
136 [ 'test.set.circleC', '0', [ 'test.set.circleA' ] ]
138 mw
.loader
.using( 'test.set.circleC' ).then(
140 assert
.ok( false, 'Unexpected resolution, expected error.' );
143 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
149 // @covers mw.loader#sortDependencies (with fallback shim)
150 QUnit
.test( '.using() - Error: Circular dependency [StringSet shim]', function ( assert
) {
151 var done
= assert
.async();
159 this.nativeSet
= window
.Set
;
160 window
.Set
= undefined;
161 mw
.redefineFallbacksForTest();
163 mw
.loader
.register( [
164 [ 'test.shim.circleA', '0', [ 'test.shim.circleB' ] ],
165 [ 'test.shim.circleB', '0', [ 'test.shim.circleC' ] ],
166 [ 'test.shim.circleC', '0', [ 'test.shim.circleA' ] ]
168 mw
.loader
.using( 'test.shim.circleC' ).then(
170 assert
.ok( false, 'Unexpected resolution, expected error.' );
173 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
179 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
181 mw
.loader
.register( [
182 [ 'test.load.circleA', '0', [ 'test.load.circleB' ] ],
183 [ 'test.load.circleB', '0', [ 'test.load.circleC' ] ],
184 [ 'test.load.circleC', '0', [ 'test.load.circleA' ] ]
186 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
189 error
: data
.exception
&& data
.exception
.message
,
194 mw
.loader
.load( 'test.load.circleC' );
197 topic
: 'resourceloader.exception',
198 error
: 'Circular reference detected: test.load.circleB -> test.load.circleC',
202 'Detect circular dependency'
206 QUnit
.test( '.load() - Error: Circular dependency (direct)', function ( assert
) {
208 mw
.loader
.register( [
209 [ 'test.load.circleDirect', '0', [ 'test.load.circleDirect' ] ]
211 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
214 error
: data
.exception
&& data
.exception
.message
,
219 mw
.loader
.load( 'test.load.circleDirect' );
222 topic
: 'resourceloader.exception',
223 error
: 'Circular reference detected: test.load.circleDirect -> test.load.circleDirect',
227 'Detect a direct self-dependency'
231 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
232 var done
= assert
.async();
234 mw
.loader
.using( 'test.using.unreg' ).then(
236 assert
.ok( false, 'Unexpected resolution, expected error.' );
239 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
244 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
246 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
249 error
: data
.exception
&& data
.exception
.message
,
254 mw
.loader
.load( 'test.load.unreg' );
257 topic
: 'resourceloader.exception',
258 error
: 'Unknown dependency: test.load.unreg',
265 // Regression test for T36853
266 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
268 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
271 error
: data
.exception
&& data
.exception
.message
,
276 mw
.loader
.register( [
277 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
278 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
280 mw
.loader
.load( 'test.load.missingdep' );
283 topic
: 'resourceloader.exception',
284 error
: 'Unknown dependency: test.load.missingdep2',
291 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
292 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
295 $element
.css( 'float' ),
304 $element
.css( 'float' ),
310 all
: '.mw-test-implement-a { float: right; }'
314 return mw
.loader
.using( 'test.implement.a' );
317 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
318 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
319 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
320 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
321 done
= assert
.async();
324 $element1
.css( 'text-align' ),
329 $element2
.css( 'float' ),
334 $element3
.css( 'text-align' ),
342 // Note: done() must only be called when the entire test is
343 // complete. So, make sure that we don't start until *both*
344 // assertStyleAsync calls have completed.
346 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
347 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
350 if ( pending
=== 0 ) {
354 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
355 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
358 if ( pending
=== 0 ) {
365 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
367 // T42834: Make sure it actually works with more than 1 stylesheet reference
368 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
369 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
375 mw
.loader
.load( 'test.implement.b' );
378 // Backwards compatibility
379 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
380 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
383 $element
.css( 'float' ),
392 $element
.css( 'float' ),
398 all
: '.mw-test-implement-c { float: right; }'
402 return mw
.loader
.using( 'test.implement.c' );
405 // Backwards compatibility
406 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
407 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
408 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
409 done
= assert
.async();
412 $element
.css( 'float' ),
417 $element2
.css( 'text-align' ),
425 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
426 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
431 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
432 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
436 mw
.loader
.load( 'test.implement.d' );
439 QUnit
.test( '.implement( messages before script )', function ( assert
) {
441 'test.implement.order',
443 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'executing', 'state during script execution' );
444 assert
.strictEqual( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
448 'test-foobar': 'Hello Foobar, $1!'
452 return mw
.loader
.using( 'test.implement.order' ).then( function () {
453 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'ready', 'final success state' );
458 QUnit
.test( '.implement( styles with @import )', function ( assert
) {
460 done
= assert
.async();
463 'test.implement.import',
465 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
467 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
468 assert
.strictEqual( $element
.css( 'text-align' ), 'center',
469 'CSS styles after the @import rule are working'
478 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
480 + '.mw-test-implement-import { text-align: center; }'
485 return mw
.loader
.using( 'test.implement.import' );
488 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
489 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
490 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
493 $element
.css( 'float' ),
498 $element2
.css( 'float' ),
503 mw
.loader
.register( [
504 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
505 [ 'test.implement.e2', '0' ]
512 $element
.css( 'float' ),
514 'Depending module\'s style is applied'
518 all
: '.mw-test-implement-e { float: right; }'
526 $element2
.css( 'float' ),
528 'Dependency\'s style is applied'
532 all
: '.mw-test-implement-e2 { float: left; }'
536 return mw
.loader
.using( 'test.implement.e' );
539 QUnit
.test( '.implement( only scripts )', function ( assert
) {
540 mw
.loader
.implement( 'test.onlyscripts', function () {} );
541 return mw
.loader
.using( 'test.onlyscripts', function () {
542 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
546 QUnit
.test( '.implement( only messages )', function ( assert
) {
547 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
549 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
551 return mw
.loader
.using( 'test.implement.msgs', function () {
552 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
556 QUnit
.test( '.implement( empty )', function ( assert
) {
557 mw
.loader
.implement( 'test.empty' );
558 return mw
.loader
.using( 'test.empty', function () {
559 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
563 QUnit
.test( '.implement( package files )', function ( assert
) {
564 var done
= assert
.async(),
567 'test.implement.packageFiles',
569 main
: 'resources/src/foo/init.js',
571 'resources/src/foo/data/hello.json': { hello
: 'world' },
572 'resources/src/foo/foo.js': function ( require
, module
) {
573 window
.mwTestFooJsCounter
= window
.mwTestFooJsCounter
|| 41;
574 window
.mwTestFooJsCounter
++;
575 module
.exports
= { answer
: window
.mwTestFooJsCounter
};
577 'resources/src/bar/bar.js': function ( require
, module
) {
578 var core
= require( './core.js' );
579 module
.exports
= { data
: core
.sayHello( 'Alice' ) };
581 'resources/src/bar/core.js': function ( require
, module
) {
582 module
.exports
= { sayHello: function ( name
) {
583 return 'Hello ' + name
;
586 'resources/src/foo/init.js': function ( require
) {
588 assert
.deepEqual( require( './data/hello.json' ), { hello
: 'world' }, 'require() with .json file' );
589 assert
.deepEqual( require( './foo.js' ), { answer
: 42 }, 'require() with .js file in same directory' );
590 assert
.deepEqual( require( '../bar/bar.js' ), { data
: 'Hello Alice' }, 'require() with ../ of a file that uses same-directory require()' );
591 assert
.deepEqual( require( './foo.js' ), { answer
: 42 }, 'require()ing the same script twice only runs it once' );
599 mw
.loader
.using( 'test.implement.packageFiles' ).done( function () {
600 assert
.ok( initJsRan
, 'main JS file is executed' );
605 QUnit
.test( '.addSource()', function ( assert
) {
606 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
608 assert
.throws( function () {
609 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
610 }, /already registered/, 'duplicate pair from addSource(Object)' );
612 assert
.throws( function () {
613 mw
.loader
.addSource( { testsource1
: 'https://1.test/src-diff' } );
614 }, /already registered/, 'duplicate ID from addSource(Object)' );
617 // @covers mw.loader#batchRequest
618 // This is a regression test because in the past we called getCombinedVersion()
619 // for all requested modules, before url splitting took place.
620 // Discovered as part of T188076, but not directly related.
621 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
622 mw
.loader
.register( [
623 // [module, version, dependencies, group, source]
624 [ 'testUrlInc', 'url', [], null, 'testloader' ],
625 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
628 mw
.loader
.maxQueryLength
= 10;
630 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
632 require( 'testUrlIncDump' ).query
,
634 modules
: 'testUrlIncDump',
635 // Expected: Wrapped hash just for this one module
636 // $hash = hash( 'fnv132', 'dump');
637 // base_convert( $hash, 16, 36 ); // "13e9zzn"
638 // Previously: Wrapped hash for both modules, despite being in separate requests
639 // $hash = hash( 'fnv132', 'urldump' );
640 // base_convert( $hash, 16, 36 ); // "18kz9ca"
646 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
650 // @covers mw.loader#batchRequest
651 // @covers mw.loader#buildModulesString
652 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
653 mw
.loader
.register( [
654 // [module, version, dependencies, group, source]
655 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
656 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
657 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
658 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
661 return mw
.loader
.using( [
666 ] ).then( function ( require
) {
668 require( 'testUrlOrderDump' ).query
,
670 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
671 // Expected: Combined in order after string packing
672 // $hash = hash( 'fnv132', 'urldump12' );
673 // base_convert( $hash, 16, 36 ); // "1knqzan"
674 // Previously: Combined in order of before string packing
675 // $hash = hash( 'fnv132', 'url12dump' );
676 // base_convert( $hash, 16, 36 ); // "11eo3in"
684 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
687 // Don't actually emit an error event
688 this.sandbox
.stub( mw
, 'track' );
690 mw
.loader
.register( [
691 [ 'test.module1', '0' ],
692 [ 'test.module2', '0', [ 'test.module1' ] ],
693 [ 'test.module3', '0', [ 'test.module2' ] ]
695 mw
.loader
.implement( 'test.module1', function () {
696 throw new Error( 'expected' );
700 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
701 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
702 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
704 assert
.strictEqual( mw
.track
.callCount
, 1 );
707 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
710 mw
.loader
.register( [
711 [ 'test.module4', '0' ],
712 [ 'test.module5', '0', [ 'test.module4' ] ],
713 [ 'test.module6', '0', [ 'test.module5' ] ]
716 mw
.loader
.implement( 'test.module4', function () {} );
718 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
719 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
720 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
722 mw
.loader
.implement( 'test.module6', function () {} );
724 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
725 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
726 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
728 mw
.loader
.implement( 'test.module5', function () {} );
730 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
731 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
732 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
735 QUnit
.test( 'Missing dependency', function ( assert
) {
738 mw
.loader
.register( [
739 [ 'test.module7', '0' ],
740 [ 'test.module8', '0', [ 'test.module7' ] ],
741 [ 'test.module9', '0', [ 'test.module8' ] ]
744 mw
.loader
.implement( 'test.module8', function () {} );
746 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
747 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
748 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
750 mw
.loader
.state( { 'test.module7': 'missing' } );
752 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
753 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
754 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
756 mw
.loader
.implement( 'test.module9', function () {} );
758 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
759 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
760 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
762 // Restore clock for QUnit and $.Deferred internals
763 this.clock
.restore();
764 return mw
.loader
.using( [ 'test.module7' ] ).then(
766 throw new Error( 'Success fired despite missing dependency' );
768 function ( e
, dependencies
) {
769 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
772 [ 'jquery', 'mediawiki.base', 'test.module7' ],
773 'Error callback called with module test.module7'
776 ).then( function () {
777 return mw
.loader
.using( [ 'test.module9' ] );
780 throw new Error( 'Success fired despite missing dependency' );
782 function ( e
, dependencies
) {
783 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
787 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
788 'Error callback called with all three modules as dependencies'
794 QUnit
.test( 'Dependency handling', function ( assert
) {
795 mw
.loader
.register( [
796 // [module, version, dependencies, group, source]
797 [ 'testMissing', '1', [], null, 'testloader' ],
798 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
799 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
802 function verifyModuleStates() {
803 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
804 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
805 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
808 return mw
.loader
.using( [ 'testUsesNestedMissing' ] ).then(
810 verifyModuleStates();
811 throw new Error( 'Error handler should be invoked.' );
813 function ( e
, modules
) {
814 // When the server sets state of 'testMissing' to 'missing'
815 // it should bubble up and trigger the error callback of the job for 'testUsesNestedMissing'.
816 assert
.strictEqual( modules
.indexOf( 'testMissing' ) !== -1, true, 'Triggered by testMissing.' );
818 verifyModuleStates();
823 QUnit
.test( 'Skip-function handling', function ( assert
) {
824 mw
.loader
.register( [
825 // [module, version, dependencies, group, source, skip]
826 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
827 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
828 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
831 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
833 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
834 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
835 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
837 function ( e
, badmodules
) {
838 // Should not fail and QUnit would already catch this,
839 // but add a handler anyway to report details from 'badmodules
840 assert
.deepEqual( badmodules
, [], 'Bad modules' );
845 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
846 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
848 done
= assert
.async();
850 // URL to the callback script
851 target
= QUnit
.fixurl(
852 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
854 // Ensure a protocol-relative URL for this test
855 target
= target
.replace( /https?:/, '' );
856 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
858 mw
.loader
.testCallback = function () {
859 // Ensure once, delete now
860 delete mw
.loader
.testCallback
;
861 assert
.ok( true, 'callback' );
866 mw
.loader
.load( target
);
869 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
871 done
= assert
.async();
873 // URL to the callback script
874 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
875 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
877 mw
.loader
.testCallback = function () {
878 // Ensure once, delete now
879 delete mw
.loader
.testCallback
;
880 assert
.ok( true, 'callback' );
885 mw
.loader
.load( target
);
888 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
891 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
893 mw
.loader
.register( '', 'v1' );
894 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
895 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
897 mw
.loader
.implement( '', function () {
901 return mw
.loader
.using( '', function () {
902 assert
.strictEqual( done
, true, 'script ran' );
903 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
907 QUnit
.test( 'Executing race - T112232', function ( assert
) {
910 // The red herring schedules its CSS buffer first. In T112232, a bug in the
911 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
913 'testRaceRedHerring',
915 { css
: [ '.mw-testRaceRedHerring {}' ] }
922 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
925 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
926 return mw
.loader
.using( 'testRaceLoadMe', function () {
927 assert
.strictEqual( done
, true, 'script ran' );
928 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
932 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
934 // Enable store and stub timeout/idle scheduling
935 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
936 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
939 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
943 mw
.loader
.register( 'test.stale', 'v2' );
944 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
946 mw
.loader
.implement( 'test.stale@v1', function () {
950 return mw
.loader
.using( 'test.stale' )
952 assert
.strictEqual( count
, 1 );
953 // After implementing, registry contains version as implemented by the response.
954 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
955 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
956 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stale' ), 'string', 'In store' );
959 // Reset run time, but keep mw.loader.store
960 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
961 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
962 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
964 // Module was stored correctly as v1
965 // On future navigations, it will be ignored until evicted
966 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
970 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
972 // Enable store and stub timeout/idle scheduling
973 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
974 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
977 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
981 mw
.loader
.register( 'test.stalebc', 'v2' );
982 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
984 mw
.loader
.implement( 'test.stalebc', function () {
988 return mw
.loader
.using( 'test.stalebc' )
990 assert
.strictEqual( script
, 1, 'module script ran' );
991 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
992 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stalebc' ), 'string', 'In store' );
995 // Reset run time, but keep mw.loader.store
996 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
997 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
998 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
1000 // Legacy behaviour is storing under the expected version,
1001 // which woudl lead to whitewashing and stale values (T117587).
1002 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
1006 QUnit
.test( 'require()', function ( assert
) {
1007 mw
.loader
.register( [
1008 [ 'test.require1', '0' ],
1009 [ 'test.require2', '0' ],
1010 [ 'test.require3', '0' ],
1011 [ 'test.require4', '0', [ 'test.require3' ] ]
1013 mw
.loader
.implement( 'test.require1', function () {} );
1014 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
1017 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
1018 module
.exports = function () {
1019 return 'hello world';
1022 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
1023 var other
= require( 'test.require3' );
1025 pizza: function () {
1030 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
1031 var module1
, module2
, module3
, module4
;
1033 module1
= require( 'test.require1' );
1034 module2
= require( 'test.require2' );
1035 module3
= require( 'test.require3' );
1036 module4
= require( 'test.require4' );
1038 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
1039 assert
.strictEqual( module2
, 1, 'export a number' );
1040 assert
.strictEqual( module3(), 'hello world', 'export a function' );
1041 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
1042 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
1044 assert
.throws( function () {
1045 require( '_badmodule' );
1046 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
1050 QUnit
.test( 'require() in debug mode', function ( assert
) {
1051 var path
= mw
.config
.get( 'wgScriptPath' );
1052 mw
.loader
.register( [
1053 [ 'test.require.define', '0' ],
1054 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
1056 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
1057 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
1059 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
1060 var cb
= require( 'test.require.callback' );
1061 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
1062 // Must use try-catch because cb.later() will throw if require is undefined,
1063 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
1065 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
1067 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
1072 QUnit
.test( 'Implicit dependencies', function ( assert
) {
1077 mw
.loader
.implement(
1083 mw
.loader
.implement(
1087 siteFromUser
= site
;
1091 return mw
.loader
.using( 'user', function () {
1092 assert
.strictEqual( site
, 1, 'site module' );
1093 assert
.strictEqual( user
, 1, 'user module' );
1094 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
1095 } ).always( function () {
1097 mw
.loader
.moduleRegistry
.site
.state
= 'registered';
1098 mw
.loader
.moduleRegistry
.user
.state
= 'registered';
1102 QUnit
.test( '.getScript() - success', function ( assert
) {
1103 var scriptUrl
= QUnit
.fixurl(
1104 mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mediawiki.loader.getScript.example.js'
1107 return mw
.loader
.getScript( scriptUrl
).then(
1109 assert
.strictEqual( mw
.getScriptExampleScriptLoaded
, true, 'Data attached to a global object is available' );
1114 QUnit
.test( '.getScript() - failure', function ( assert
) {
1116 mw
.loader
.getScript( 'https://example.test/not-found' ),
1117 /Failed to load script/,
1118 'Descriptive error message'