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 // Teardown for StringSet shim test
19 if ( this.nativeSet
) {
20 window
.Set
= this.nativeSet
;
21 mw
.redefineFallbacksForTest();
23 // Remove any remaining temporary statics
24 // exposed for cross-file mocks.
25 delete mw
.loader
.testCallback
;
26 delete mw
.loader
.testFail
;
30 mw
.loader
.addSource( {
32 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
36 * The sync style load test, for @import. This is, in a way, also an open bug for
37 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
38 * way to get a callback from when a stylesheet is loaded (that is, including any
39 * `@import` rules inside). To work around this, we'll have a little time loop to check
40 * if the styles apply.
42 * Note: This test originally used new Image() and onerror to get a callback
43 * when the url is loaded, but that is fragile since it doesn't monitor the
44 * same request as the css @import, and Safari 4 has issues with
45 * onerror/onload not being fired at all in weird cases like this.
47 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
49 el
= $element
.get( 0 ),
50 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
52 function isCssImportApplied() {
53 // Trigger reflow, repaint, redraw, whatever (cross-browser)
54 $element
.css( 'height' );
55 // eslint-disable-next-line no-unused-expressions
57 // eslint-disable-next-line no-self-assign
58 el
.className
= el
.className
;
59 // eslint-disable-next-line no-unused-expressions
60 document
.documentElement
.clientHeight
;
62 return $element
.css( prop
) === val
;
65 function styleTestLoop() {
66 var styleTestSince
= new Date().getTime() - styleTestStart
;
67 // If it is passing or if we timed out, run the real test and stop the loop
68 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
69 assert
.strictEqual( $element
.css( prop
), val
,
70 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
79 // Otherwise, keep polling
80 setTimeout( styleTestLoop
);
84 styleTestStart
= new Date().getTime();
88 function urlStyleTest( selector
, prop
, val
) {
90 mw
.config
.get( 'wgScriptPath' ) +
91 '/tests/qunit/data/styleTest.css.php?' +
100 QUnit
.test( '.using( .., Function callback ) Promise', function ( assert
) {
101 var script
= 0, callback
= 0;
102 mw
.loader
.testCallback = function () {
105 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
107 return mw
.loader
.using( 'test.promise', function () {
109 } ).then( function () {
110 assert
.strictEqual( script
, 1, 'module script ran' );
111 assert
.strictEqual( callback
, 1, 'using() callback ran' );
115 QUnit
.test( 'Prototype method as module name', function ( assert
) {
117 mw
.loader
.testCallback = function () {
120 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
122 return mw
.loader
.using( 'hasOwnProperty', function () {
123 assert
.strictEqual( call
, 1, 'module script ran' );
127 // Covers mw.loader#sortDependencies (with native Set if available)
128 QUnit
.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert
) {
129 var done
= assert
.async();
131 mw
.loader
.register( [
132 [ 'test.set.circleA', '0', [ 'test.set.circleB' ] ],
133 [ 'test.set.circleB', '0', [ 'test.set.circleC' ] ],
134 [ 'test.set.circleC', '0', [ 'test.set.circleA' ] ]
136 mw
.loader
.using( 'test.set.circleC' ).then(
138 assert
.ok( false, 'Unexpected resolution, expected error.' );
141 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
147 // @covers mw.loader#sortDependencies (with fallback shim)
148 QUnit
.test( '.using() - Error: Circular dependency [StringSet shim]', function ( assert
) {
149 var done
= assert
.async();
157 this.nativeSet
= window
.Set
;
158 window
.Set
= undefined;
159 mw
.redefineFallbacksForTest();
161 mw
.loader
.register( [
162 [ 'test.shim.circleA', '0', [ 'test.shim.circleB' ] ],
163 [ 'test.shim.circleB', '0', [ 'test.shim.circleC' ] ],
164 [ 'test.shim.circleC', '0', [ 'test.shim.circleA' ] ]
166 mw
.loader
.using( 'test.shim.circleC' ).then(
168 assert
.ok( false, 'Unexpected resolution, expected error.' );
171 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
177 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
179 mw
.loader
.register( [
180 [ 'test.load.circleA', '0', [ 'test.load.circleB' ] ],
181 [ 'test.load.circleB', '0', [ 'test.load.circleC' ] ],
182 [ 'test.load.circleC', '0', [ 'test.load.circleA' ] ]
184 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
187 error
: data
.exception
&& data
.exception
.message
,
192 mw
.loader
.load( 'test.load.circleC' );
195 topic
: 'resourceloader.exception',
196 error
: 'Circular reference detected: test.load.circleB -> test.load.circleC',
200 'Detect circular dependency'
204 QUnit
.test( '.load() - Error: Circular dependency (direct)', function ( assert
) {
206 mw
.loader
.register( [
207 [ 'test.load.circleDirect', '0', [ 'test.load.circleDirect' ] ]
209 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
212 error
: data
.exception
&& data
.exception
.message
,
217 mw
.loader
.load( 'test.load.circleDirect' );
220 topic
: 'resourceloader.exception',
221 error
: 'Circular reference detected: test.load.circleDirect -> test.load.circleDirect',
225 'Detect a direct self-dependency'
229 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
230 var done
= assert
.async();
232 mw
.loader
.using( 'test.using.unreg' ).then(
234 assert
.ok( false, 'Unexpected resolution, expected error.' );
237 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
242 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
244 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
247 error
: data
.exception
&& data
.exception
.message
,
252 mw
.loader
.load( 'test.load.unreg' );
255 topic
: 'resourceloader.exception',
256 error
: 'Unknown dependency: test.load.unreg',
263 // Regression test for T36853
264 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
266 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
269 error
: data
.exception
&& data
.exception
.message
,
274 mw
.loader
.register( [
275 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
276 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
278 mw
.loader
.load( 'test.load.missingdep' );
281 topic
: 'resourceloader.exception',
282 error
: 'Unknown dependency: test.load.missingdep2',
289 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
290 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
293 $element
.css( 'float' ),
302 $element
.css( 'float' ),
308 all
: '.mw-test-implement-a { float: right; }'
312 return mw
.loader
.using( 'test.implement.a' );
315 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
316 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
317 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
318 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
319 done
= assert
.async();
322 $element1
.css( 'text-align' ),
327 $element2
.css( 'float' ),
332 $element3
.css( 'text-align' ),
340 // Note: done() must only be called when the entire test is
341 // complete. So, make sure that we don't start until *both*
342 // assertStyleAsync calls have completed.
344 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
345 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
348 if ( pending
=== 0 ) {
352 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
353 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
356 if ( pending
=== 0 ) {
363 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
365 // T42834: Make sure it actually works with more than 1 stylesheet reference
366 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
367 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
373 mw
.loader
.load( 'test.implement.b' );
376 // Backwards compatibility
377 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
378 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
381 $element
.css( 'float' ),
390 $element
.css( 'float' ),
396 all
: '.mw-test-implement-c { float: right; }'
400 return mw
.loader
.using( 'test.implement.c' );
403 // Backwards compatibility
404 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
405 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
406 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
407 done
= assert
.async();
410 $element
.css( 'float' ),
415 $element2
.css( 'text-align' ),
423 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
424 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
429 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
430 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
434 mw
.loader
.load( 'test.implement.d' );
437 QUnit
.test( '.implement( messages before script )', function ( assert
) {
439 'test.implement.order',
441 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'executing', 'state during script execution' );
442 assert
.strictEqual( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
446 'test-foobar': 'Hello Foobar, $1!'
450 return mw
.loader
.using( 'test.implement.order' ).then( function () {
451 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'ready', 'final success state' );
456 QUnit
.test( '.implement( styles with @import )', function ( assert
) {
458 done
= assert
.async();
461 'test.implement.import',
463 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
465 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
466 assert
.strictEqual( $element
.css( 'text-align' ), 'center',
467 'CSS styles after the @import rule are working'
476 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
478 + '.mw-test-implement-import { text-align: center; }'
483 return mw
.loader
.using( 'test.implement.import' );
486 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
487 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
488 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
491 $element
.css( 'float' ),
496 $element2
.css( 'float' ),
501 mw
.loader
.register( [
502 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
503 [ 'test.implement.e2', '0' ]
510 $element
.css( 'float' ),
512 'Depending module\'s style is applied'
516 all
: '.mw-test-implement-e { float: right; }'
524 $element2
.css( 'float' ),
526 'Dependency\'s style is applied'
530 all
: '.mw-test-implement-e2 { float: left; }'
534 return mw
.loader
.using( 'test.implement.e' );
537 QUnit
.test( '.implement( only scripts )', function ( assert
) {
538 mw
.loader
.implement( 'test.onlyscripts', function () {} );
539 return mw
.loader
.using( 'test.onlyscripts', function () {
540 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
544 QUnit
.test( '.implement( only messages )', function ( assert
) {
545 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
547 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
549 return mw
.loader
.using( 'test.implement.msgs', function () {
550 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
554 QUnit
.test( '.implement( empty )', function ( assert
) {
555 mw
.loader
.implement( 'test.empty' );
556 return mw
.loader
.using( 'test.empty', function () {
557 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
561 QUnit
.test( '.addSource()', function ( assert
) {
562 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
564 assert
.throws( function () {
565 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
566 }, /already registered/, 'duplicate pair from addSource(Object)' );
568 assert
.throws( function () {
569 mw
.loader
.addSource( { testsource1
: 'https://1.test/src-diff' } );
570 }, /already registered/, 'duplicate ID from addSource(Object)' );
573 // @covers mw.loader#batchRequest
574 // This is a regression test because in the past we called getCombinedVersion()
575 // for all requested modules, before url splitting took place.
576 // Discovered as part of T188076, but not directly related.
577 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
578 mw
.loader
.register( [
579 // [module, version, dependencies, group, source]
580 [ 'testUrlInc', 'url', [], null, 'testloader' ],
581 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
584 mw
.config
.set( 'wgResourceLoaderMaxQueryLength', 10 );
586 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
588 require( 'testUrlIncDump' ).query
,
590 modules
: 'testUrlIncDump',
591 // Expected: Wrapped hash just for this one module
592 // $hash = hash( 'fnv132', 'dump');
593 // base_convert( $hash, 16, 36 ); // "13e9zzn"
594 // Previously: Wrapped hash for both modules, despite being in separate requests
595 // $hash = hash( 'fnv132', 'urldump' );
596 // base_convert( $hash, 16, 36 ); // "18kz9ca"
602 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
606 // @covers mw.loader#batchRequest
607 // @covers mw.loader#buildModulesString
608 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
609 mw
.loader
.register( [
610 // [module, version, dependencies, group, source]
611 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
612 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
613 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
614 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
617 return mw
.loader
.using( [
622 ] ).then( function ( require
) {
624 require( 'testUrlOrderDump' ).query
,
626 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
627 // Expected: Combined in order after string packing
628 // $hash = hash( 'fnv132', 'urldump12' );
629 // base_convert( $hash, 16, 36 ); // "1knqzan"
630 // Previously: Combined in order of before string packing
631 // $hash = hash( 'fnv132', 'url12dump' );
632 // base_convert( $hash, 16, 36 ); // "11eo3in"
640 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
643 // Don't actually emit an error event
644 this.sandbox
.stub( mw
, 'track' );
646 mw
.loader
.register( [
647 [ 'test.module1', '0' ],
648 [ 'test.module2', '0', [ 'test.module1' ] ],
649 [ 'test.module3', '0', [ 'test.module2' ] ]
651 mw
.loader
.implement( 'test.module1', function () {
652 throw new Error( 'expected' );
656 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
657 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
658 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
660 assert
.strictEqual( mw
.track
.callCount
, 1 );
663 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
666 mw
.loader
.register( [
667 [ 'test.module4', '0' ],
668 [ 'test.module5', '0', [ 'test.module4' ] ],
669 [ 'test.module6', '0', [ 'test.module5' ] ]
672 mw
.loader
.implement( 'test.module4', function () {} );
674 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
675 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
676 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
678 mw
.loader
.implement( 'test.module6', function () {} );
680 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
681 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
682 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
684 mw
.loader
.implement( 'test.module5', function () {} );
686 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
687 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
688 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
691 QUnit
.test( 'Missing dependency', function ( assert
) {
694 mw
.loader
.register( [
695 [ 'test.module7', '0' ],
696 [ 'test.module8', '0', [ 'test.module7' ] ],
697 [ 'test.module9', '0', [ 'test.module8' ] ]
700 mw
.loader
.implement( 'test.module8', function () {} );
702 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
703 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
704 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
706 mw
.loader
.state( { 'test.module7': 'missing' } );
708 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
709 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
710 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
712 mw
.loader
.implement( 'test.module9', function () {} );
714 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
715 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
716 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
718 // Restore clock for QUnit and $.Deferred internals
719 this.clock
.restore();
720 return mw
.loader
.using( [ 'test.module7' ] ).then(
722 throw new Error( 'Success fired despite missing dependency' );
724 function ( e
, dependencies
) {
725 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
728 [ 'jquery', 'mediawiki.base', 'test.module7' ],
729 'Error callback called with module test.module7'
732 ).then( function () {
733 return mw
.loader
.using( [ 'test.module9' ] );
736 throw new Error( 'Success fired despite missing dependency' );
738 function ( e
, dependencies
) {
739 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
743 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
744 'Error callback called with all three modules as dependencies'
750 QUnit
.test( 'Dependency handling', function ( assert
) {
751 mw
.loader
.register( [
752 // [module, version, dependencies, group, source]
753 [ 'testMissing', '1', [], null, 'testloader' ],
754 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
755 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
758 function verifyModuleStates() {
759 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
760 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
761 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
764 return mw
.loader
.using( [ 'testUsesNestedMissing' ] ).then(
766 verifyModuleStates();
767 throw new Error( 'Error handler should be invoked.' );
769 function ( e
, modules
) {
770 // When the server sets state of 'testMissing' to 'missing'
771 // it should bubble up and trigger the error callback of the job for 'testUsesNestedMissing'.
772 assert
.strictEqual( modules
.indexOf( 'testMissing' ) !== -1, true, 'Triggered by testMissing.' );
774 verifyModuleStates();
779 QUnit
.test( 'Skip-function handling', function ( assert
) {
780 mw
.loader
.register( [
781 // [module, version, dependencies, group, source, skip]
782 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
783 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
784 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
787 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
789 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
790 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
791 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
793 function ( e
, badmodules
) {
794 // Should not fail and QUnit would already catch this,
795 // but add a handler anyway to report details from 'badmodules
796 assert
.deepEqual( badmodules
, [], 'Bad modules' );
801 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
802 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
804 done
= assert
.async();
806 // URL to the callback script
807 target
= QUnit
.fixurl(
808 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
810 // Ensure a protocol-relative URL for this test
811 target
= target
.replace( /https?:/, '' );
812 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
814 mw
.loader
.testCallback = function () {
815 // Ensure once, delete now
816 delete mw
.loader
.testCallback
;
817 assert
.ok( true, 'callback' );
822 mw
.loader
.load( target
);
825 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
827 done
= assert
.async();
829 // URL to the callback script
830 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
831 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
833 mw
.loader
.testCallback = function () {
834 // Ensure once, delete now
835 delete mw
.loader
.testCallback
;
836 assert
.ok( true, 'callback' );
841 mw
.loader
.load( target
);
844 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
847 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
849 mw
.loader
.register( '', 'v1' );
850 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
851 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
853 mw
.loader
.implement( '', function () {
857 return mw
.loader
.using( '', function () {
858 assert
.strictEqual( done
, true, 'script ran' );
859 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
863 QUnit
.test( 'Executing race - T112232', function ( assert
) {
866 // The red herring schedules its CSS buffer first. In T112232, a bug in the
867 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
869 'testRaceRedHerring',
871 { css
: [ '.mw-testRaceRedHerring {}' ] }
878 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
881 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
882 return mw
.loader
.using( 'testRaceLoadMe', function () {
883 assert
.strictEqual( done
, true, 'script ran' );
884 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
888 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
890 // Enable store and stub timeout/idle scheduling
891 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
892 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
895 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
899 mw
.loader
.register( 'test.stale', 'v2' );
900 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
902 mw
.loader
.implement( 'test.stale@v1', function () {
906 return mw
.loader
.using( 'test.stale' )
908 assert
.strictEqual( count
, 1 );
909 // After implementing, registry contains version as implemented by the response.
910 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
911 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
912 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stale' ), 'string', 'In store' );
915 // Reset run time, but keep mw.loader.store
916 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
917 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
918 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
920 // Module was stored correctly as v1
921 // On future navigations, it will be ignored until evicted
922 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
926 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
928 // Enable store and stub timeout/idle scheduling
929 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
930 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
933 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
937 mw
.loader
.register( 'test.stalebc', 'v2' );
938 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
940 mw
.loader
.implement( 'test.stalebc', function () {
944 return mw
.loader
.using( 'test.stalebc' )
946 assert
.strictEqual( script
, 1, 'module script ran' );
947 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
948 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stalebc' ), 'string', 'In store' );
951 // Reset run time, but keep mw.loader.store
952 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
953 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
954 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
956 // Legacy behaviour is storing under the expected version,
957 // which woudl lead to whitewashing and stale values (T117587).
958 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
962 QUnit
.test( 'require()', function ( assert
) {
963 mw
.loader
.register( [
964 [ 'test.require1', '0' ],
965 [ 'test.require2', '0' ],
966 [ 'test.require3', '0' ],
967 [ 'test.require4', '0', [ 'test.require3' ] ]
969 mw
.loader
.implement( 'test.require1', function () {} );
970 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
973 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
974 module
.exports = function () {
975 return 'hello world';
978 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
979 var other
= require( 'test.require3' );
986 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
987 var module1
, module2
, module3
, module4
;
989 module1
= require( 'test.require1' );
990 module2
= require( 'test.require2' );
991 module3
= require( 'test.require3' );
992 module4
= require( 'test.require4' );
994 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
995 assert
.strictEqual( module2
, 1, 'export a number' );
996 assert
.strictEqual( module3(), 'hello world', 'export a function' );
997 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
998 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
1000 assert
.throws( function () {
1001 require( '_badmodule' );
1002 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
1006 QUnit
.test( 'require() in debug mode', function ( assert
) {
1007 var path
= mw
.config
.get( 'wgScriptPath' );
1008 mw
.loader
.register( [
1009 [ 'test.require.define', '0' ],
1010 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
1012 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
1013 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
1015 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
1016 var cb
= require( 'test.require.callback' );
1017 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
1018 // Must use try-catch because cb.later() will throw if require is undefined,
1019 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
1021 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
1023 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
1028 QUnit
.test( 'Implicit dependencies', function ( assert
) {
1033 mw
.loader
.implement(
1039 mw
.loader
.implement(
1043 siteFromUser
= site
;
1047 return mw
.loader
.using( 'user', function () {
1048 assert
.strictEqual( site
, 1, 'site module' );
1049 assert
.strictEqual( user
, 1, 'user module' );
1050 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
1051 } ).always( function () {
1053 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
1054 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
1058 }( mediaWiki
, jQuery
) );