2 QUnit
.module( 'mediawiki (mw.loader)', QUnit
.newMwEnvironment( {
4 mw
.loader
.store
.enabled
= false;
6 teardown: function () {
7 mw
.loader
.store
.enabled
= false;
8 // Teardown for StringSet shim test
9 if ( this.nativeSet
) {
10 window
.Set
= this.nativeSet
;
11 mw
.redefineFallbacksForTest();
18 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
22 * The sync style load test (for @import). This is, in a way, also an open bug for
23 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
24 * way to get a callback from when a stylesheet is loaded (that is, including any
25 * `@import` rules inside). To work around this, we'll have a little time loop to check
26 * if the styles apply.
28 * Note: This test originally used new Image() and onerror to get a callback
29 * when the url is loaded, but that is fragile since it doesn't monitor the
30 * same request as the css @import, and Safari 4 has issues with
31 * onerror/onload not being fired at all in weird cases like this.
33 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
35 el
= $element
.get( 0 ),
36 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
38 function isCssImportApplied() {
39 // Trigger reflow, repaint, redraw, whatever (cross-browser)
40 $element
.css( 'height' );
41 // eslint-disable-next-line no-unused-expressions
43 el
.className
= el
.className
;
44 // eslint-disable-next-line no-unused-expressions
45 document
.documentElement
.clientHeight
;
47 return $element
.css( prop
) === val
;
50 function styleTestLoop() {
51 var styleTestSince
= new Date().getTime() - styleTestStart
;
52 // If it is passing or if we timed out, run the real test and stop the loop
53 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
54 assert
.equal( $element
.css( prop
), val
,
55 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
64 // Otherwise, keep polling
65 setTimeout( styleTestLoop
);
69 styleTestStart
= new Date().getTime();
73 function urlStyleTest( selector
, prop
, val
) {
75 mw
.config
.get( 'wgScriptPath' ) +
76 '/tests/qunit/data/styleTest.css.php?' +
85 QUnit
.test( 'Basic', function ( assert
) {
88 mw
.loader
.testCallback = function () {
89 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
93 mw
.loader
.implement( 'test.callback', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ] );
95 return mw
.loader
.using( 'test.callback', function () {
96 assert
.strictEqual( isAwesomeDone
, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
97 delete mw
.loader
.testCallback
;
100 assert
.ok( false, 'Error callback fired while loader.using "test.callback" module' );
104 QUnit
.test( 'Object method as module name', function ( assert
) {
107 mw
.loader
.testCallback = function () {
108 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
109 isAwesomeDone
= true;
112 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ], {}, {} );
114 return mw
.loader
.using( 'hasOwnProperty', function () {
115 assert
.strictEqual( isAwesomeDone
, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
116 delete mw
.loader
.testCallback
;
119 assert
.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
123 QUnit
.test( '.using( .. ) Promise', function ( assert
) {
126 mw
.loader
.testCallback = function () {
127 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
128 isAwesomeDone
= true;
131 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' ) ] );
133 return mw
.loader
.using( 'test.promise' )
135 assert
.strictEqual( isAwesomeDone
, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
136 delete mw
.loader
.testCallback
;
139 assert
.ok( false, 'Error callback fired while loader.using "test.promise" module' );
143 // Covers mw.loader#sortDependencies (with native Set if available)
144 QUnit
.test( '.using() Error: Circular dependency [StringSet default]', function ( assert
) {
145 var done
= assert
.async();
147 mw
.loader
.register( [
148 [ 'test.circle1', '0', [ 'test.circle2' ] ],
149 [ 'test.circle2', '0', [ 'test.circle3' ] ],
150 [ 'test.circle3', '0', [ 'test.circle1' ] ]
152 mw
.loader
.using( 'test.circle3' ).then(
154 assert
.ok( false, 'Unexpected resolution, expected error.' );
157 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
163 // @covers mw.loader#sortDependencies (with fallback shim)
164 QUnit
.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert
) {
165 var done
= assert
.async();
173 this.nativeSet
= window
.Set
;
174 window
.Set
= undefined;
175 mw
.redefineFallbacksForTest();
177 mw
.loader
.register( [
178 [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
179 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
180 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
182 mw
.loader
.using( 'test.shim.circle3' ).then(
184 assert
.ok( false, 'Unexpected resolution, expected error.' );
187 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
193 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
194 mw
.loader
.register( [
195 [ 'test.circleA', '0', [ 'test.circleB' ] ],
196 [ 'test.circleB', '0', [ 'test.circleC' ] ],
197 [ 'test.circleC', '0', [ 'test.circleA' ] ]
199 assert
.throws( function () {
200 mw
.loader
.load( 'test.circleC' );
201 }, /Circular/, 'Detect circular dependency' );
204 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
205 var done
= assert
.async();
207 mw
.loader
.using( 'test.using.unreg' ).then(
209 assert
.ok( false, 'Unexpected resolution, expected error.' );
212 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
217 QUnit
.test( '.load() - Error: Unregistered (ignored)', function ( assert
) {
219 mw
.loader
.load( 'test.using.unreg2' );
222 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
223 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
226 $element
.css( 'float' ),
235 $element
.css( 'float' ),
241 all
: '.mw-test-implement-a { float: right; }'
245 return mw
.loader
.using( 'test.implement.a' );
248 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
249 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
250 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
251 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
252 done
= assert
.async();
255 $element1
.css( 'text-align' ),
260 $element2
.css( 'float' ),
265 $element3
.css( 'text-align' ),
273 // Note: done() must only be called when the entire test is
274 // complete. So, make sure that we don't start until *both*
275 // assertStyleAsync calls have completed.
277 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
278 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
281 if ( pending
=== 0 ) {
285 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
286 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
289 if ( pending
=== 0 ) {
296 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
298 // T42834: Make sure it actually works with more than 1 stylesheet reference
299 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
300 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
306 mw
.loader
.load( 'test.implement.b' );
309 // Backwards compatibility
310 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
311 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
314 $element
.css( 'float' ),
323 $element
.css( 'float' ),
329 all
: '.mw-test-implement-c { float: right; }'
333 return mw
.loader
.using( 'test.implement.c' );
336 // Backwards compatibility
337 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
338 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
339 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
340 done
= assert
.async();
343 $element
.css( 'float' ),
348 $element2
.css( 'text-align' ),
356 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
357 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
362 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
363 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
367 mw
.loader
.load( 'test.implement.d' );
371 QUnit
.test( '.implement( styles has @import )', function ( assert
) {
372 var isJsExecuted
, $element
,
373 done
= assert
.async();
376 'test.implement.import',
378 assert
.strictEqual( isJsExecuted
, undefined, 'script not executed multiple times' );
381 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'executing', 'module state during implement() script execution' );
383 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
385 assert
.equal( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
387 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
388 assert
.equal( $element
.css( 'text-align' ), 'center',
389 'CSS styles after the @import rule are working'
398 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
400 + '.mw-test-implement-import { text-align: center; }'
404 'test-foobar': 'Hello Foobar, $1!'
408 mw
.loader
.using( 'test.implement.import' ).always( function () {
409 assert
.strictEqual( isJsExecuted
, true, 'script executed' );
410 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'ready', 'module state after script execution' );
414 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
415 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
416 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
419 $element
.css( 'float' ),
424 $element2
.css( 'float' ),
429 mw
.loader
.register( [
430 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
431 [ 'test.implement.e2', '0' ]
438 $element
.css( 'float' ),
440 'Depending module\'s style is applied'
444 all
: '.mw-test-implement-e { float: right; }'
452 $element2
.css( 'float' ),
454 'Dependency\'s style is applied'
458 all
: '.mw-test-implement-e2 { float: left; }'
462 return mw
.loader
.using( 'test.implement.e' );
465 QUnit
.test( '.implement( only scripts )', function ( assert
) {
466 mw
.loader
.implement( 'test.onlyscripts', function () {} );
467 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
470 QUnit
.test( '.implement( only messages )', function ( assert
) {
471 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
473 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
475 return mw
.loader
.using( 'test.implement.msgs', function () {
476 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
478 assert
.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
482 QUnit
.test( '.implement( empty )', function ( assert
) {
483 mw
.loader
.implement( 'test.empty' );
484 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
487 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
488 // don't emit an error event
489 this.sandbox
.stub( mw
, 'track' );
491 mw
.loader
.register( [
492 [ 'test.module1', '0' ],
493 [ 'test.module2', '0', [ 'test.module1' ] ],
494 [ 'test.module3', '0', [ 'test.module2' ] ]
496 mw
.loader
.implement( 'test.module1', function () {
497 throw new Error( 'expected' );
499 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
500 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
501 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
503 assert
.strictEqual( mw
.track
.callCount
, 1 );
506 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
507 mw
.loader
.register( [
508 [ 'test.module4', '0' ],
509 [ 'test.module5', '0', [ 'test.module4' ] ],
510 [ 'test.module6', '0', [ 'test.module5' ] ]
512 mw
.loader
.implement( 'test.module4', function () {} );
513 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
514 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
515 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
516 mw
.loader
.implement( 'test.module6', function () {} );
517 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
518 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
519 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
520 mw
.loader
.implement( 'test.module5', function () {} );
521 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
522 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
523 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
526 QUnit
.test( 'Missing dependency', function ( assert
) {
527 mw
.loader
.register( [
528 [ 'test.module7', '0' ],
529 [ 'test.module8', '0', [ 'test.module7' ] ],
530 [ 'test.module9', '0', [ 'test.module8' ] ]
532 mw
.loader
.implement( 'test.module8', function () {} );
533 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
534 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
535 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
536 mw
.loader
.state( 'test.module7', 'missing' );
537 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
538 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
539 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
540 mw
.loader
.implement( 'test.module9', function () {} );
541 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
542 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
543 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
547 assert
.ok( false, 'Success fired despite missing dependency' );
548 assert
.ok( true, 'QUnit expected() count dummy' );
550 function ( e
, dependencies
) {
551 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
552 assert
.deepEqual( dependencies
, [ 'test.module7' ], 'Error callback called with module test.module7' );
558 assert
.ok( false, 'Success fired despite missing dependency' );
559 assert
.ok( true, 'QUnit expected() count dummy' );
561 function ( e
, dependencies
) {
562 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
566 [ 'test.module7', 'test.module8', 'test.module9' ],
567 'Error callback called with all three modules as dependencies'
573 QUnit
.test( 'Dependency handling', function ( assert
) {
574 var done
= assert
.async();
575 mw
.loader
.register( [
576 // [module, version, dependencies, group, source]
577 [ 'testMissing', '1', [], null, 'testloader' ],
578 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
579 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
582 function verifyModuleStates() {
583 assert
.equal( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
584 assert
.equal( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
585 assert
.equal( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
588 mw
.loader
.using( [ 'testUsesNestedMissing' ],
590 assert
.ok( false, 'Error handler should be invoked.' );
591 assert
.ok( true ); // Dummy to reach QUnit expect()
593 verifyModuleStates();
597 function ( e
, badmodules
) {
598 assert
.ok( true, 'Error handler should be invoked.' );
599 // As soon as server spits out state('testMissing', 'missing');
600 // it will bubble up and trigger the error callback.
601 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
602 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
604 verifyModuleStates();
611 QUnit
.test( 'Skip-function handling', function ( assert
) {
612 mw
.loader
.register( [
613 // [module, version, dependencies, group, source, skip]
614 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
615 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
616 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
619 function verifyModuleStates() {
620 assert
.equal( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
621 assert
.equal( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
622 assert
.equal( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
625 return mw
.loader
.using( [ 'testUsesSkippable' ],
627 assert
.ok( true, 'Success handler should be invoked.' );
628 assert
.ok( true ); // Dummy to match error handler and reach QUnit expect()
630 verifyModuleStates();
632 function ( e
, badmodules
) {
633 assert
.ok( false, 'Error handler should not be invoked.' );
634 assert
.deepEqual( badmodules
, [], 'Bad modules as expected.' );
636 verifyModuleStates();
641 QUnit
.asyncTest( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
642 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
643 // Test is for regressions!
645 // Forge a URL to the test callback script
646 var target
= QUnit
.fixurl(
647 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
650 // Confirm that mw.loader.load() works with protocol-relative URLs
651 target
= target
.replace( /https?:/, '' );
653 assert
.equal( target
.slice( 0, 2 ), '//',
654 'URL must be relative to test relative URLs!'
658 // The target calls QUnit.start
659 mw
.loader
.load( target
);
662 QUnit
.asyncTest( '.load( "/absolute-path" )', function ( assert
) {
663 // Forge a URL to the test callback script
664 var target
= QUnit
.fixurl(
665 mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
668 // Confirm that mw.loader.load() works with absolute-paths (relative to current hostname)
669 assert
.equal( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
672 // The target calls QUnit.start
673 mw
.loader
.load( target
);
676 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
679 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
681 mw
.loader
.register( '', 'v1' );
682 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
683 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
685 mw
.loader
.implement( '', function () {
689 return mw
.loader
.using( '', function () {
690 assert
.strictEqual( done
, true, 'script ran' );
691 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
695 QUnit
.test( 'Executing race - T112232', function ( assert
) {
698 // The red herring schedules its CSS buffer first. In T112232, a bug in the
699 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
701 'testRaceRedHerring',
703 { css
: [ '.mw-testRaceRedHerring {}' ] }
710 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
713 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
714 return mw
.loader
.using( 'testRaceLoadMe', function () {
715 assert
.strictEqual( done
, true, 'script ran' );
716 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
720 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
722 mw
.loader
.store
.enabled
= true;
723 mw
.loader
.register( 'test.stale', 'v2' );
724 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
726 mw
.loader
.implement( 'test.stale@v1', function () {
730 return mw
.loader
.using( 'test.stale' )
732 assert
.strictEqual( count
, 1 );
733 // After implementing, registry contains version as implemented by the response.
734 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
735 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
736 assert
.ok( mw
.loader
.store
.get( 'test.stale' ), 'In store' );
739 // Reset run time, but keep mw.loader.store
740 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
741 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
742 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
744 // Module was stored correctly as v1
745 // On future navigations, it will be ignored until evicted
746 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
750 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
752 mw
.loader
.store
.enabled
= true;
753 mw
.loader
.register( 'test.stalebc', 'v2' );
754 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
756 mw
.loader
.implement( 'test.stalebc', function () {
760 return mw
.loader
.using( 'test.stalebc' )
762 assert
.strictEqual( count
, 1 );
763 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
764 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
767 // Reset run time, but keep mw.loader.store
768 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
769 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
770 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
772 // Legacy behaviour is storing under the expected version,
773 // which woudl lead to whitewashing and stale values (T117587).
774 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
778 QUnit
.test( 'require()', function ( assert
) {
779 mw
.loader
.register( [
780 [ 'test.require1', '0' ],
781 [ 'test.require2', '0' ],
782 [ 'test.require3', '0' ],
783 [ 'test.require4', '0', [ 'test.require3' ] ]
785 mw
.loader
.implement( 'test.require1', function () {} );
786 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
789 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
790 module
.exports = function () {
791 return 'hello world';
794 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
795 var other
= require( 'test.require3' );
802 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] )
803 .then( function ( require
) {
804 var module1
, module2
, module3
, module4
;
806 module1
= require( 'test.require1' );
807 module2
= require( 'test.require2' );
808 module3
= require( 'test.require3' );
809 module4
= require( 'test.require4' );
811 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
812 assert
.strictEqual( module2
, 1, 'export a number' );
813 assert
.strictEqual( module3(), 'hello world', 'export a function' );
814 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
815 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
817 assert
.throws( function () {
818 require( '_badmodule' );
819 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
823 QUnit
.test( 'require() in debug mode', function ( assert
) {
824 var path
= mw
.config
.get( 'wgScriptPath' );
825 mw
.loader
.register( [
826 [ 'test.require.define', '0' ],
827 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
829 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
830 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
832 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
833 var cb
= require( 'test.require.callback' );
834 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
835 // Must use try-catch because cb.later() will throw if require is undefined,
836 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
838 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
840 assert
.equal( null, String( e
), 'require works asynchrously in debug mode' );
843 assert
.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
847 }( mediaWiki
, jQuery
) );