2 QUnit
.module( 'mediawiki (mw.loader)', QUnit
.newMwEnvironment( {
3 setup: function ( assert
) {
4 mw
.loader
.store
.enabled
= false;
6 // Expose for load.mock.php
7 mw
.loader
.testFail = function ( reason
) {
8 assert
.ok( false, reason
);
11 teardown: function () {
12 mw
.loader
.store
.enabled
= false;
13 // Teardown for StringSet shim test
14 if ( this.nativeSet
) {
15 window
.Set
= this.nativeSet
;
16 mw
.redefineFallbacksForTest();
18 // Remove any remaining temporary statics
19 // exposed for cross-file mocks.
20 if ( 'testCallback' in mw
.loader
) {
21 delete mw
.loader
.testCallback
;
23 if ( 'testFail' in mw
.loader
) {
24 delete mw
.loader
.testFail
;
31 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
35 * The sync style load test (for @import). This is, in a way, also an open bug for
36 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
37 * way to get a callback from when a stylesheet is loaded (that is, including any
38 * `@import` rules inside). To work around this, we'll have a little time loop to check
39 * if the styles apply.
41 * Note: This test originally used new Image() and onerror to get a callback
42 * when the url is loaded, but that is fragile since it doesn't monitor the
43 * same request as the css @import, and Safari 4 has issues with
44 * onerror/onload not being fired at all in weird cases like this.
46 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
48 el
= $element
.get( 0 ),
49 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
51 function isCssImportApplied() {
52 // Trigger reflow, repaint, redraw, whatever (cross-browser)
53 $element
.css( 'height' );
54 // eslint-disable-next-line no-unused-expressions
56 el
.className
= el
.className
;
57 // eslint-disable-next-line no-unused-expressions
58 document
.documentElement
.clientHeight
;
60 return $element
.css( prop
) === val
;
63 function styleTestLoop() {
64 var styleTestSince
= new Date().getTime() - styleTestStart
;
65 // If it is passing or if we timed out, run the real test and stop the loop
66 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
67 assert
.equal( $element
.css( prop
), val
,
68 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
77 // Otherwise, keep polling
78 setTimeout( styleTestLoop
);
82 styleTestStart
= new Date().getTime();
86 function urlStyleTest( selector
, prop
, val
) {
88 mw
.config
.get( 'wgScriptPath' ) +
89 '/tests/qunit/data/styleTest.css.php?' +
98 QUnit
.test( 'Basic', function ( assert
) {
101 mw
.loader
.testCallback = function () {
102 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
103 isAwesomeDone
= true;
106 mw
.loader
.implement( 'test.callback', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
108 return mw
.loader
.using( 'test.callback', function () {
109 assert
.strictEqual( isAwesomeDone
, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
111 assert
.ok( false, 'Error callback fired while loader.using "test.callback" module' );
115 QUnit
.test( 'Object method as module name', function ( assert
) {
118 mw
.loader
.testCallback = function () {
119 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
120 isAwesomeDone
= true;
123 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
125 return mw
.loader
.using( 'hasOwnProperty', function () {
126 assert
.strictEqual( isAwesomeDone
, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
128 assert
.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
132 QUnit
.test( '.using( .. ) Promise', function ( assert
) {
135 mw
.loader
.testCallback = function () {
136 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
137 isAwesomeDone
= true;
140 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
142 return mw
.loader
.using( 'test.promise' )
144 assert
.strictEqual( isAwesomeDone
, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
147 assert
.ok( false, 'Error callback fired while loader.using "test.promise" module' );
151 // Covers mw.loader#sortDependencies (with native Set if available)
152 QUnit
.test( '.using() Error: Circular dependency [StringSet default]', function ( assert
) {
153 var done
= assert
.async();
155 mw
.loader
.register( [
156 [ 'test.circle1', '0', [ 'test.circle2' ] ],
157 [ 'test.circle2', '0', [ 'test.circle3' ] ],
158 [ 'test.circle3', '0', [ 'test.circle1' ] ]
160 mw
.loader
.using( 'test.circle3' ).then(
162 assert
.ok( false, 'Unexpected resolution, expected error.' );
165 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
171 // @covers mw.loader#sortDependencies (with fallback shim)
172 QUnit
.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert
) {
173 var done
= assert
.async();
181 this.nativeSet
= window
.Set
;
182 window
.Set
= undefined;
183 mw
.redefineFallbacksForTest();
185 mw
.loader
.register( [
186 [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
187 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
188 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
190 mw
.loader
.using( 'test.shim.circle3' ).then(
192 assert
.ok( false, 'Unexpected resolution, expected error.' );
195 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
201 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
203 mw
.loader
.register( [
204 [ 'test.circleA', '0', [ 'test.circleB' ] ],
205 [ 'test.circleB', '0', [ 'test.circleC' ] ],
206 [ 'test.circleC', '0', [ 'test.circleA' ] ]
208 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
211 error
: data
.exception
&& data
.exception
.message
,
216 mw
.loader
.load( 'test.circleC' );
219 topic
: 'resourceloader.exception',
220 error
: 'Circular reference detected: test.circleB -> test.circleC',
224 'Detect circular dependency'
228 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
229 var done
= assert
.async();
231 mw
.loader
.using( 'test.using.unreg' ).then(
233 assert
.ok( false, 'Unexpected resolution, expected error.' );
236 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
241 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
243 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
246 error
: data
.exception
&& data
.exception
.message
,
251 mw
.loader
.load( 'test.load.unreg' );
254 topic
: 'resourceloader.exception',
255 error
: 'Unknown dependency: test.load.unreg',
262 // Regression test for T36853
263 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
265 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
268 error
: data
.exception
&& data
.exception
.message
,
273 mw
.loader
.register( [
274 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
275 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
277 mw
.loader
.load( 'test.load.missingdep' );
280 topic
: 'resourceloader.exception',
281 error
: 'Unknown dependency: test.load.missingdep2',
288 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
289 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
292 $element
.css( 'float' ),
301 $element
.css( 'float' ),
307 all
: '.mw-test-implement-a { float: right; }'
311 return mw
.loader
.using( 'test.implement.a' );
314 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
315 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
316 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
317 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
318 done
= assert
.async();
321 $element1
.css( 'text-align' ),
326 $element2
.css( 'float' ),
331 $element3
.css( 'text-align' ),
339 // Note: done() must only be called when the entire test is
340 // complete. So, make sure that we don't start until *both*
341 // assertStyleAsync calls have completed.
343 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
344 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
347 if ( pending
=== 0 ) {
351 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
352 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
355 if ( pending
=== 0 ) {
362 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
364 // T42834: Make sure it actually works with more than 1 stylesheet reference
365 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
366 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
372 mw
.loader
.load( 'test.implement.b' );
375 // Backwards compatibility
376 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
377 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
380 $element
.css( 'float' ),
389 $element
.css( 'float' ),
395 all
: '.mw-test-implement-c { float: right; }'
399 return mw
.loader
.using( 'test.implement.c' );
402 // Backwards compatibility
403 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
404 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
405 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
406 done
= assert
.async();
409 $element
.css( 'float' ),
414 $element2
.css( 'text-align' ),
422 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
423 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
428 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
429 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
433 mw
.loader
.load( 'test.implement.d' );
437 QUnit
.test( '.implement( styles has @import )', function ( assert
) {
438 var isJsExecuted
, $element
,
439 done
= assert
.async();
442 'test.implement.import',
444 assert
.strictEqual( isJsExecuted
, undefined, 'script not executed multiple times' );
447 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'executing', 'module state during implement() script execution' );
449 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
451 assert
.equal( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
453 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
454 assert
.equal( $element
.css( 'text-align' ), 'center',
455 'CSS styles after the @import rule are working'
464 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
466 + '.mw-test-implement-import { text-align: center; }'
470 'test-foobar': 'Hello Foobar, $1!'
474 mw
.loader
.using( 'test.implement.import' ).always( function () {
475 assert
.strictEqual( isJsExecuted
, true, 'script executed' );
476 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'ready', 'module state after script execution' );
480 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
481 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
482 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
485 $element
.css( 'float' ),
490 $element2
.css( 'float' ),
495 mw
.loader
.register( [
496 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
497 [ 'test.implement.e2', '0' ]
504 $element
.css( 'float' ),
506 'Depending module\'s style is applied'
510 all
: '.mw-test-implement-e { float: right; }'
518 $element2
.css( 'float' ),
520 'Dependency\'s style is applied'
524 all
: '.mw-test-implement-e2 { float: left; }'
528 return mw
.loader
.using( 'test.implement.e' );
531 QUnit
.test( '.implement( only scripts )', function ( assert
) {
532 mw
.loader
.implement( 'test.onlyscripts', function () {} );
533 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
536 QUnit
.test( '.implement( only messages )', function ( assert
) {
537 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
539 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
541 return mw
.loader
.using( 'test.implement.msgs', function () {
542 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
544 assert
.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
548 QUnit
.test( '.implement( empty )', function ( assert
) {
549 mw
.loader
.implement( 'test.empty' );
550 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
553 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
554 // don't emit an error event
555 this.sandbox
.stub( mw
, 'track' );
557 mw
.loader
.register( [
558 [ 'test.module1', '0' ],
559 [ 'test.module2', '0', [ 'test.module1' ] ],
560 [ 'test.module3', '0', [ 'test.module2' ] ]
562 mw
.loader
.implement( 'test.module1', function () {
563 throw new Error( 'expected' );
565 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
566 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
567 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
569 assert
.strictEqual( mw
.track
.callCount
, 1 );
572 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
573 mw
.loader
.register( [
574 [ 'test.module4', '0' ],
575 [ 'test.module5', '0', [ 'test.module4' ] ],
576 [ 'test.module6', '0', [ 'test.module5' ] ]
578 mw
.loader
.implement( 'test.module4', function () {} );
579 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
580 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
581 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
582 mw
.loader
.implement( 'test.module6', function () {} );
583 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
584 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
585 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
586 mw
.loader
.implement( 'test.module5', function () {} );
587 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
588 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
589 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
592 QUnit
.test( 'Missing dependency', function ( assert
) {
593 mw
.loader
.register( [
594 [ 'test.module7', '0' ],
595 [ 'test.module8', '0', [ 'test.module7' ] ],
596 [ 'test.module9', '0', [ 'test.module8' ] ]
598 mw
.loader
.implement( 'test.module8', function () {} );
599 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
600 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
601 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
602 mw
.loader
.state( 'test.module7', 'missing' );
603 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
604 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
605 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
606 mw
.loader
.implement( 'test.module9', function () {} );
607 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
608 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
609 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
613 assert
.ok( false, 'Success fired despite missing dependency' );
614 assert
.ok( true, 'QUnit expected() count dummy' );
616 function ( e
, dependencies
) {
617 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
618 assert
.deepEqual( dependencies
, [ 'test.module7' ], 'Error callback called with module test.module7' );
624 assert
.ok( false, 'Success fired despite missing dependency' );
625 assert
.ok( true, 'QUnit expected() count dummy' );
627 function ( e
, dependencies
) {
628 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
632 [ 'test.module7', 'test.module8', 'test.module9' ],
633 'Error callback called with all three modules as dependencies'
639 QUnit
.test( 'Dependency handling', function ( assert
) {
640 var done
= assert
.async();
641 mw
.loader
.register( [
642 // [module, version, dependencies, group, source]
643 [ 'testMissing', '1', [], null, 'testloader' ],
644 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
645 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
648 function verifyModuleStates() {
649 assert
.equal( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
650 assert
.equal( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
651 assert
.equal( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
654 mw
.loader
.using( [ 'testUsesNestedMissing' ],
656 assert
.ok( false, 'Error handler should be invoked.' );
657 assert
.ok( true ); // Dummy to reach QUnit expect()
659 verifyModuleStates();
663 function ( e
, badmodules
) {
664 assert
.ok( true, 'Error handler should be invoked.' );
665 // As soon as server spits out state('testMissing', 'missing');
666 // it will bubble up and trigger the error callback.
667 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
668 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
670 verifyModuleStates();
677 QUnit
.test( 'Skip-function handling', function ( assert
) {
678 mw
.loader
.register( [
679 // [module, version, dependencies, group, source, skip]
680 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
681 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
682 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
685 function verifyModuleStates() {
686 assert
.equal( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
687 assert
.equal( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
688 assert
.equal( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
691 return mw
.loader
.using( [ 'testUsesSkippable' ],
693 assert
.ok( true, 'Success handler should be invoked.' );
694 assert
.ok( true ); // Dummy to match error handler and reach QUnit expect()
696 verifyModuleStates();
698 function ( e
, badmodules
) {
699 assert
.ok( false, 'Error handler should not be invoked.' );
700 assert
.deepEqual( badmodules
, [], 'Bad modules as expected.' );
702 verifyModuleStates();
707 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
708 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
710 done
= assert
.async();
712 // URL to the callback script
713 target
= QUnit
.fixurl(
714 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
716 // Ensure a protocol-relative URL for this test
717 target
= target
.replace( /https?:/, '' );
718 assert
.equal( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
720 mw
.loader
.testCallback = function () {
721 // Ensure once, delete now
722 delete mw
.loader
.testCallback
;
723 assert
.ok( true, 'callback' );
728 mw
.loader
.load( target
);
731 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
733 done
= assert
.async();
735 // URL to the callback script
736 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
737 assert
.equal( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
739 mw
.loader
.testCallback = function () {
740 // Ensure once, delete now
741 delete mw
.loader
.testCallback
;
742 assert
.ok( true, 'callback' );
747 mw
.loader
.load( target
);
750 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
753 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
755 mw
.loader
.register( '', 'v1' );
756 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
757 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
759 mw
.loader
.implement( '', function () {
763 return mw
.loader
.using( '', function () {
764 assert
.strictEqual( done
, true, 'script ran' );
765 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
769 QUnit
.test( 'Executing race - T112232', function ( assert
) {
772 // The red herring schedules its CSS buffer first. In T112232, a bug in the
773 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
775 'testRaceRedHerring',
777 { css
: [ '.mw-testRaceRedHerring {}' ] }
784 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
787 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
788 return mw
.loader
.using( 'testRaceLoadMe', function () {
789 assert
.strictEqual( done
, true, 'script ran' );
790 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
794 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
796 mw
.loader
.store
.enabled
= true;
797 mw
.loader
.register( 'test.stale', 'v2' );
798 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
800 mw
.loader
.implement( 'test.stale@v1', function () {
804 return mw
.loader
.using( 'test.stale' )
806 assert
.strictEqual( count
, 1 );
807 // After implementing, registry contains version as implemented by the response.
808 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
809 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
810 assert
.ok( mw
.loader
.store
.get( 'test.stale' ), 'In store' );
813 // Reset run time, but keep mw.loader.store
814 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
815 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
816 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
818 // Module was stored correctly as v1
819 // On future navigations, it will be ignored until evicted
820 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
824 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
826 mw
.loader
.store
.enabled
= true;
827 mw
.loader
.register( 'test.stalebc', 'v2' );
828 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
830 mw
.loader
.implement( 'test.stalebc', function () {
834 return mw
.loader
.using( 'test.stalebc' )
836 assert
.strictEqual( count
, 1 );
837 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
838 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
841 // Reset run time, but keep mw.loader.store
842 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
843 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
844 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
846 // Legacy behaviour is storing under the expected version,
847 // which woudl lead to whitewashing and stale values (T117587).
848 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
852 QUnit
.test( 'require()', function ( assert
) {
853 mw
.loader
.register( [
854 [ 'test.require1', '0' ],
855 [ 'test.require2', '0' ],
856 [ 'test.require3', '0' ],
857 [ 'test.require4', '0', [ 'test.require3' ] ]
859 mw
.loader
.implement( 'test.require1', function () {} );
860 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
863 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
864 module
.exports = function () {
865 return 'hello world';
868 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
869 var other
= require( 'test.require3' );
876 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
877 var module1
, module2
, module3
, module4
;
879 module1
= require( 'test.require1' );
880 module2
= require( 'test.require2' );
881 module3
= require( 'test.require3' );
882 module4
= require( 'test.require4' );
884 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
885 assert
.strictEqual( module2
, 1, 'export a number' );
886 assert
.strictEqual( module3(), 'hello world', 'export a function' );
887 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
888 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
890 assert
.throws( function () {
891 require( '_badmodule' );
892 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
896 QUnit
.test( 'require() in debug mode', function ( assert
) {
897 var path
= mw
.config
.get( 'wgScriptPath' );
898 mw
.loader
.register( [
899 [ 'test.require.define', '0' ],
900 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
902 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
903 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
905 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
906 var cb
= require( 'test.require.callback' );
907 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
908 // Must use try-catch because cb.later() will throw if require is undefined,
909 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
911 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
913 assert
.equal( null, String( e
), 'require works asynchrously in debug mode' );
916 assert
.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
920 QUnit
.test( 'Implicit dependencies', function ( assert
) {
922 userSeesSite
= false,
934 userSeesSite
= ranSite
;
939 assert
.strictEqual( ranSite
, false, 'verify site module not yet loaded' );
940 assert
.strictEqual( ranUser
, false, 'verify user module not yet loaded' );
941 return mw
.loader
.using( 'user', function () {
942 assert
.strictEqual( ranSite
, true, 'ran site module' );
943 assert
.strictEqual( ranUser
, true, 'ran user module' );
944 assert
.strictEqual( userSeesSite
, true, 'ran site before user module' );
947 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
948 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
952 }( mediaWiki
, jQuery
) );