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 teardown: function () {
10 // Teardown for StringSet shim test
11 if ( this.nativeSet
) {
12 window
.Set
= this.nativeSet
;
13 mw
.redefineFallbacksForTest();
15 // Remove any remaining temporary statics
16 // exposed for cross-file mocks.
17 delete mw
.loader
.testCallback
;
18 delete mw
.loader
.testFail
;
24 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
28 * The sync style load test, for @import. This is, in a way, also an open bug for
29 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
30 * way to get a callback from when a stylesheet is loaded (that is, including any
31 * `@import` rules inside). To work around this, we'll have a little time loop to check
32 * if the styles apply.
34 * Note: This test originally used new Image() and onerror to get a callback
35 * when the url is loaded, but that is fragile since it doesn't monitor the
36 * same request as the css @import, and Safari 4 has issues with
37 * onerror/onload not being fired at all in weird cases like this.
39 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
41 el
= $element
.get( 0 ),
42 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
44 function isCssImportApplied() {
45 // Trigger reflow, repaint, redraw, whatever (cross-browser)
46 $element
.css( 'height' );
47 // eslint-disable-next-line no-unused-expressions
49 // eslint-disable-next-line no-self-assign
50 el
.className
= el
.className
;
51 // eslint-disable-next-line no-unused-expressions
52 document
.documentElement
.clientHeight
;
54 return $element
.css( prop
) === val
;
57 function styleTestLoop() {
58 var styleTestSince
= new Date().getTime() - styleTestStart
;
59 // If it is passing or if we timed out, run the real test and stop the loop
60 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
61 assert
.strictEqual( $element
.css( prop
), val
,
62 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
71 // Otherwise, keep polling
72 setTimeout( styleTestLoop
);
76 styleTestStart
= new Date().getTime();
80 function urlStyleTest( selector
, prop
, val
) {
82 mw
.config
.get( 'wgScriptPath' ) +
83 '/tests/qunit/data/styleTest.css.php?' +
92 QUnit
.test( '.using( .., Function callback ) Promise', function ( assert
) {
93 var script
= 0, callback
= 0;
94 mw
.loader
.testCallback = function () {
97 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
99 return mw
.loader
.using( 'test.promise', function () {
101 } ).then( function () {
102 assert
.strictEqual( script
, 1, 'module script ran' );
103 assert
.strictEqual( callback
, 1, 'using() callback ran' );
107 QUnit
.test( 'Prototype method as module name', function ( assert
) {
109 mw
.loader
.testCallback = function () {
112 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
114 return mw
.loader
.using( 'hasOwnProperty', function () {
115 assert
.strictEqual( call
, 1, 'module script ran' );
119 // Covers mw.loader#sortDependencies (with native Set if available)
120 QUnit
.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert
) {
121 var done
= assert
.async();
123 mw
.loader
.register( [
124 [ 'test.set.circleA', '0', [ 'test.set.circleB' ] ],
125 [ 'test.set.circleB', '0', [ 'test.set.circleC' ] ],
126 [ 'test.set.circleC', '0', [ 'test.set.circleA' ] ]
128 mw
.loader
.using( 'test.set.circleC' ).then(
130 assert
.ok( false, 'Unexpected resolution, expected error.' );
133 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
139 // @covers mw.loader#sortDependencies (with fallback shim)
140 QUnit
.test( '.using() - Error: Circular dependency [StringSet shim]', function ( assert
) {
141 var done
= assert
.async();
149 this.nativeSet
= window
.Set
;
150 window
.Set
= undefined;
151 mw
.redefineFallbacksForTest();
153 mw
.loader
.register( [
154 [ 'test.shim.circleA', '0', [ 'test.shim.circleB' ] ],
155 [ 'test.shim.circleB', '0', [ 'test.shim.circleC' ] ],
156 [ 'test.shim.circleC', '0', [ 'test.shim.circleA' ] ]
158 mw
.loader
.using( 'test.shim.circleC' ).then(
160 assert
.ok( false, 'Unexpected resolution, expected error.' );
163 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
169 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
171 mw
.loader
.register( [
172 [ 'test.load.circleA', '0', [ 'test.load.circleB' ] ],
173 [ 'test.load.circleB', '0', [ 'test.load.circleC' ] ],
174 [ 'test.load.circleC', '0', [ 'test.load.circleA' ] ]
176 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
179 error
: data
.exception
&& data
.exception
.message
,
184 mw
.loader
.load( 'test.load.circleC' );
187 topic
: 'resourceloader.exception',
188 error
: 'Circular reference detected: test.load.circleB -> test.load.circleC',
192 'Detect circular dependency'
196 QUnit
.test( '.load() - Error: Circular dependency (direct)', function ( assert
) {
198 mw
.loader
.register( [
199 [ 'test.load.circleDirect', '0', [ 'test.load.circleDirect' ] ]
201 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
204 error
: data
.exception
&& data
.exception
.message
,
209 mw
.loader
.load( 'test.load.circleDirect' );
212 topic
: 'resourceloader.exception',
213 error
: 'Circular reference detected: test.load.circleDirect -> test.load.circleDirect',
217 'Detect a direct self-dependency'
221 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
222 var done
= assert
.async();
224 mw
.loader
.using( 'test.using.unreg' ).then(
226 assert
.ok( false, 'Unexpected resolution, expected error.' );
229 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
234 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
236 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
239 error
: data
.exception
&& data
.exception
.message
,
244 mw
.loader
.load( 'test.load.unreg' );
247 topic
: 'resourceloader.exception',
248 error
: 'Unknown dependency: test.load.unreg',
255 // Regression test for T36853
256 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
258 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
261 error
: data
.exception
&& data
.exception
.message
,
266 mw
.loader
.register( [
267 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
268 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
270 mw
.loader
.load( 'test.load.missingdep' );
273 topic
: 'resourceloader.exception',
274 error
: 'Unknown dependency: test.load.missingdep2',
281 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
282 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
285 $element
.css( 'float' ),
294 $element
.css( 'float' ),
300 all
: '.mw-test-implement-a { float: right; }'
304 return mw
.loader
.using( 'test.implement.a' );
307 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
308 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
309 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
310 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
311 done
= assert
.async();
314 $element1
.css( 'text-align' ),
319 $element2
.css( 'float' ),
324 $element3
.css( 'text-align' ),
332 // Note: done() must only be called when the entire test is
333 // complete. So, make sure that we don't start until *both*
334 // assertStyleAsync calls have completed.
336 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
337 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
340 if ( pending
=== 0 ) {
344 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
345 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
348 if ( pending
=== 0 ) {
355 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
357 // T42834: Make sure it actually works with more than 1 stylesheet reference
358 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
359 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
365 mw
.loader
.load( 'test.implement.b' );
368 // Backwards compatibility
369 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
370 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
373 $element
.css( 'float' ),
382 $element
.css( 'float' ),
388 all
: '.mw-test-implement-c { float: right; }'
392 return mw
.loader
.using( 'test.implement.c' );
395 // Backwards compatibility
396 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
397 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
398 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
399 done
= assert
.async();
402 $element
.css( 'float' ),
407 $element2
.css( 'text-align' ),
415 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
416 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
421 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
422 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
426 mw
.loader
.load( 'test.implement.d' );
429 QUnit
.test( '.implement( messages before script )', function ( assert
) {
431 'test.implement.order',
433 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'executing', 'state during script execution' );
434 assert
.strictEqual( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
438 'test-foobar': 'Hello Foobar, $1!'
442 return mw
.loader
.using( 'test.implement.order' ).then( function () {
443 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'ready', 'final success state' );
448 QUnit
.test( '.implement( styles with @import )', function ( assert
) {
450 done
= assert
.async();
453 'test.implement.import',
455 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
457 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
458 assert
.strictEqual( $element
.css( 'text-align' ), 'center',
459 'CSS styles after the @import rule are working'
468 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
470 + '.mw-test-implement-import { text-align: center; }'
475 return mw
.loader
.using( 'test.implement.import' );
478 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
479 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
480 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
483 $element
.css( 'float' ),
488 $element2
.css( 'float' ),
493 mw
.loader
.register( [
494 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
495 [ 'test.implement.e2', '0' ]
502 $element
.css( 'float' ),
504 'Depending module\'s style is applied'
508 all
: '.mw-test-implement-e { float: right; }'
516 $element2
.css( 'float' ),
518 'Dependency\'s style is applied'
522 all
: '.mw-test-implement-e2 { float: left; }'
526 return mw
.loader
.using( 'test.implement.e' );
529 QUnit
.test( '.implement( only scripts )', function ( assert
) {
530 mw
.loader
.implement( 'test.onlyscripts', function () {} );
531 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
534 QUnit
.test( '.implement( only messages )', function ( assert
) {
535 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
537 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
539 return mw
.loader
.using( 'test.implement.msgs', function () {
540 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
544 QUnit
.test( '.implement( empty )', function ( assert
) {
545 mw
.loader
.implement( 'test.empty' );
546 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
549 // @covers mw.loader#batchRequest
550 // This is a regression test because in the past we called getCombinedVersion()
551 // for all requested modules, before url splitting took place.
552 // Discovered as part of T188076, but not directly related.
553 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
554 mw
.loader
.register( [
555 // [module, version, dependencies, group, source]
556 [ 'testUrlInc', 'url', [], null, 'testloader' ],
557 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
560 mw
.config
.set( 'wgResourceLoaderMaxQueryLength', 10 );
562 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
564 require( 'testUrlIncDump' ).query
,
566 modules
: 'testUrlIncDump',
567 // Expected: Wrapped hash just for this one module
568 // $hash = hash( 'fnv132', 'dump');
569 // base_convert( $hash, 16, 36 ); // "13e9zzn"
570 // Previously: Wrapped hash for both modules, despite being in separate requests
571 // $hash = hash( 'fnv132', 'urldump' );
572 // base_convert( $hash, 16, 36 ); // "18kz9ca"
578 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
582 // @covers mw.loader#batchRequest
583 // @covers mw.loader#buildModulesString
584 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
585 mw
.loader
.register( [
586 // [module, version, dependencies, group, source]
587 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
588 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
589 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
590 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
593 return mw
.loader
.using( [
598 ] ).then( function ( require
) {
600 require( 'testUrlOrderDump' ).query
,
602 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
603 // Expected: Combined in order after string packing
604 // $hash = hash( 'fnv132', 'urldump12' );
605 // base_convert( $hash, 16, 36 ); // "1knqzan"
606 // Previously: Combined in order of before string packing
607 // $hash = hash( 'fnv132', 'url12dump' );
608 // base_convert( $hash, 16, 36 ); // "11eo3in"
616 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
617 // don't emit an error event
618 this.sandbox
.stub( mw
, 'track' );
620 mw
.loader
.register( [
621 [ 'test.module1', '0' ],
622 [ 'test.module2', '0', [ 'test.module1' ] ],
623 [ 'test.module3', '0', [ 'test.module2' ] ]
625 mw
.loader
.implement( 'test.module1', function () {
626 throw new Error( 'expected' );
628 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
629 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
630 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
632 assert
.strictEqual( mw
.track
.callCount
, 1 );
635 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
636 mw
.loader
.register( [
637 [ 'test.module4', '0' ],
638 [ 'test.module5', '0', [ 'test.module4' ] ],
639 [ 'test.module6', '0', [ 'test.module5' ] ]
641 mw
.loader
.implement( 'test.module4', function () {} );
642 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
643 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
644 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
645 mw
.loader
.implement( 'test.module6', function () {} );
646 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
647 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
648 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
649 mw
.loader
.implement( 'test.module5', function () {} );
650 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
651 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
652 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
655 QUnit
.test( 'Missing dependency', function ( assert
) {
656 mw
.loader
.register( [
657 [ 'test.module7', '0' ],
658 [ 'test.module8', '0', [ 'test.module7' ] ],
659 [ 'test.module9', '0', [ 'test.module8' ] ]
661 mw
.loader
.implement( 'test.module8', function () {} );
662 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
663 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
664 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
665 mw
.loader
.state( { 'test.module7': 'missing' } );
666 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
667 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
668 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
669 mw
.loader
.implement( 'test.module9', function () {} );
670 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
671 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
672 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
676 assert
.ok( false, 'Success fired despite missing dependency' );
677 assert
.ok( true, 'QUnit expected() count dummy' );
679 function ( e
, dependencies
) {
680 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
683 [ 'jquery', 'mediawiki.base', 'test.module7' ],
684 'Error callback called with module test.module7'
691 assert
.ok( false, 'Success fired despite missing dependency' );
692 assert
.ok( true, 'QUnit expected() count dummy' );
694 function ( e
, dependencies
) {
695 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
699 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
700 'Error callback called with all three modules as dependencies'
706 QUnit
.test( 'Dependency handling', function ( assert
) {
707 var done
= assert
.async();
708 mw
.loader
.register( [
709 // [module, version, dependencies, group, source]
710 [ 'testMissing', '1', [], null, 'testloader' ],
711 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
712 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
715 function verifyModuleStates() {
716 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
717 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
718 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
721 mw
.loader
.using( [ 'testUsesNestedMissing' ],
723 assert
.ok( false, 'Error handler should be invoked.' );
724 assert
.ok( true ); // Dummy to reach QUnit expect()
726 verifyModuleStates();
730 function ( e
, badmodules
) {
731 assert
.ok( true, 'Error handler should be invoked.' );
732 // As soon as server sets state of 'testMissing' to 'missing'
733 // it will bubble up and trigger the error callback.
734 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
735 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
737 verifyModuleStates();
744 QUnit
.test( 'Skip-function handling', function ( assert
) {
745 mw
.loader
.register( [
746 // [module, version, dependencies, group, source, skip]
747 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
748 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
749 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
752 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
754 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
755 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
756 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
758 function ( e
, badmodules
) {
759 // Should not fail and QUnit would already catch this,
760 // but add a handler anyway to report details from 'badmodules
761 assert
.deepEqual( badmodules
, [], 'Bad modules' );
766 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
767 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
769 done
= assert
.async();
771 // URL to the callback script
772 target
= QUnit
.fixurl(
773 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
775 // Ensure a protocol-relative URL for this test
776 target
= target
.replace( /https?:/, '' );
777 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
779 mw
.loader
.testCallback = function () {
780 // Ensure once, delete now
781 delete mw
.loader
.testCallback
;
782 assert
.ok( true, 'callback' );
787 mw
.loader
.load( target
);
790 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
792 done
= assert
.async();
794 // URL to the callback script
795 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
796 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
798 mw
.loader
.testCallback = function () {
799 // Ensure once, delete now
800 delete mw
.loader
.testCallback
;
801 assert
.ok( true, 'callback' );
806 mw
.loader
.load( target
);
809 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
812 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
814 mw
.loader
.register( '', 'v1' );
815 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
816 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
818 mw
.loader
.implement( '', function () {
822 return mw
.loader
.using( '', function () {
823 assert
.strictEqual( done
, true, 'script ran' );
824 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
828 QUnit
.test( 'Executing race - T112232', function ( assert
) {
831 // The red herring schedules its CSS buffer first. In T112232, a bug in the
832 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
834 'testRaceRedHerring',
836 { css
: [ '.mw-testRaceRedHerring {}' ] }
843 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
846 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
847 return mw
.loader
.using( 'testRaceLoadMe', function () {
848 assert
.strictEqual( done
, true, 'script ran' );
849 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
853 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
855 // Enable store and stub timeout/idle scheduling
856 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
857 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
860 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
864 mw
.loader
.register( 'test.stale', 'v2' );
865 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
867 mw
.loader
.implement( 'test.stale@v1', function () {
871 return mw
.loader
.using( 'test.stale' )
873 assert
.strictEqual( count
, 1 );
874 // After implementing, registry contains version as implemented by the response.
875 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
876 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
877 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stale' ), 'string', 'In store' );
880 // Reset run time, but keep mw.loader.store
881 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
882 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
883 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
885 // Module was stored correctly as v1
886 // On future navigations, it will be ignored until evicted
887 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
891 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
893 // Enable store and stub timeout/idle scheduling
894 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
895 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
898 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
902 mw
.loader
.register( 'test.stalebc', 'v2' );
903 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
905 mw
.loader
.implement( 'test.stalebc', function () {
909 return mw
.loader
.using( 'test.stalebc' )
911 assert
.strictEqual( script
, 1, 'module script ran' );
912 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
913 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stalebc' ), 'string', 'In store' );
916 // Reset run time, but keep mw.loader.store
917 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
918 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
919 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
921 // Legacy behaviour is storing under the expected version,
922 // which woudl lead to whitewashing and stale values (T117587).
923 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
927 QUnit
.test( 'require()', function ( assert
) {
928 mw
.loader
.register( [
929 [ 'test.require1', '0' ],
930 [ 'test.require2', '0' ],
931 [ 'test.require3', '0' ],
932 [ 'test.require4', '0', [ 'test.require3' ] ]
934 mw
.loader
.implement( 'test.require1', function () {} );
935 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
938 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
939 module
.exports = function () {
940 return 'hello world';
943 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
944 var other
= require( 'test.require3' );
951 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
952 var module1
, module2
, module3
, module4
;
954 module1
= require( 'test.require1' );
955 module2
= require( 'test.require2' );
956 module3
= require( 'test.require3' );
957 module4
= require( 'test.require4' );
959 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
960 assert
.strictEqual( module2
, 1, 'export a number' );
961 assert
.strictEqual( module3(), 'hello world', 'export a function' );
962 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
963 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
965 assert
.throws( function () {
966 require( '_badmodule' );
967 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
971 QUnit
.test( 'require() in debug mode', function ( assert
) {
972 var path
= mw
.config
.get( 'wgScriptPath' );
973 mw
.loader
.register( [
974 [ 'test.require.define', '0' ],
975 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
977 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
978 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
980 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
981 var cb
= require( 'test.require.callback' );
982 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
983 // Must use try-catch because cb.later() will throw if require is undefined,
984 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
986 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
988 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
993 QUnit
.test( 'Implicit dependencies', function ( assert
) {
1004 mw
.loader
.implement(
1008 siteFromUser
= site
;
1012 return mw
.loader
.using( 'user', function () {
1013 assert
.strictEqual( site
, 1, 'site module' );
1014 assert
.strictEqual( user
, 1, 'user module' );
1015 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
1016 } ).always( function () {
1018 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
1019 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
1023 }( mediaWiki
, jQuery
) );