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
;
22 mw
.loader
.addSource( {
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 QUnit
.test( '.addSource()', function ( assert
) {
550 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
552 assert
.throws( function () {
553 mw
.loader
.addSource( { testsource1
: 'https://1.test/src' } );
554 }, /already registered/, 'duplicate pair from addSource(Object)' );
556 assert
.throws( function () {
557 mw
.loader
.addSource( { testsource1
: 'https://1.test/src-diff' } );
558 }, /already registered/, 'duplicate ID from addSource(Object)' );
561 // @covers mw.loader#batchRequest
562 // This is a regression test because in the past we called getCombinedVersion()
563 // for all requested modules, before url splitting took place.
564 // Discovered as part of T188076, but not directly related.
565 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
566 mw
.loader
.register( [
567 // [module, version, dependencies, group, source]
568 [ 'testUrlInc', 'url', [], null, 'testloader' ],
569 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
572 mw
.config
.set( 'wgResourceLoaderMaxQueryLength', 10 );
574 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
576 require( 'testUrlIncDump' ).query
,
578 modules
: 'testUrlIncDump',
579 // Expected: Wrapped hash just for this one module
580 // $hash = hash( 'fnv132', 'dump');
581 // base_convert( $hash, 16, 36 ); // "13e9zzn"
582 // Previously: Wrapped hash for both modules, despite being in separate requests
583 // $hash = hash( 'fnv132', 'urldump' );
584 // base_convert( $hash, 16, 36 ); // "18kz9ca"
590 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
594 // @covers mw.loader#batchRequest
595 // @covers mw.loader#buildModulesString
596 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
597 mw
.loader
.register( [
598 // [module, version, dependencies, group, source]
599 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
600 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
601 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
602 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
605 return mw
.loader
.using( [
610 ] ).then( function ( require
) {
612 require( 'testUrlOrderDump' ).query
,
614 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
615 // Expected: Combined in order after string packing
616 // $hash = hash( 'fnv132', 'urldump12' );
617 // base_convert( $hash, 16, 36 ); // "1knqzan"
618 // Previously: Combined in order of before string packing
619 // $hash = hash( 'fnv132', 'url12dump' );
620 // base_convert( $hash, 16, 36 ); // "11eo3in"
628 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
629 // don't emit an error event
630 this.sandbox
.stub( mw
, 'track' );
632 mw
.loader
.register( [
633 [ 'test.module1', '0' ],
634 [ 'test.module2', '0', [ 'test.module1' ] ],
635 [ 'test.module3', '0', [ 'test.module2' ] ]
637 mw
.loader
.implement( 'test.module1', function () {
638 throw new Error( 'expected' );
640 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
641 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
642 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
644 assert
.strictEqual( mw
.track
.callCount
, 1 );
647 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
648 mw
.loader
.register( [
649 [ 'test.module4', '0' ],
650 [ 'test.module5', '0', [ 'test.module4' ] ],
651 [ 'test.module6', '0', [ 'test.module5' ] ]
653 mw
.loader
.implement( 'test.module4', function () {} );
654 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
655 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
656 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
657 mw
.loader
.implement( 'test.module6', function () {} );
658 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
659 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
660 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
661 mw
.loader
.implement( 'test.module5', function () {} );
662 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
663 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
664 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
667 QUnit
.test( 'Missing dependency', function ( assert
) {
668 mw
.loader
.register( [
669 [ 'test.module7', '0' ],
670 [ 'test.module8', '0', [ 'test.module7' ] ],
671 [ 'test.module9', '0', [ 'test.module8' ] ]
673 mw
.loader
.implement( 'test.module8', function () {} );
674 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
675 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
676 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
677 mw
.loader
.state( { 'test.module7': 'missing' } );
678 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
679 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
680 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
681 mw
.loader
.implement( 'test.module9', function () {} );
682 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
683 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
684 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
688 assert
.ok( false, 'Success fired despite missing dependency' );
689 assert
.ok( true, 'QUnit expected() count dummy' );
691 function ( e
, dependencies
) {
692 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
695 [ 'jquery', 'mediawiki.base', 'test.module7' ],
696 'Error callback called with module test.module7'
703 assert
.ok( false, 'Success fired despite missing dependency' );
704 assert
.ok( true, 'QUnit expected() count dummy' );
706 function ( e
, dependencies
) {
707 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
711 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
712 'Error callback called with all three modules as dependencies'
718 QUnit
.test( 'Dependency handling', function ( assert
) {
719 var done
= assert
.async();
720 mw
.loader
.register( [
721 // [module, version, dependencies, group, source]
722 [ 'testMissing', '1', [], null, 'testloader' ],
723 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
724 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
727 function verifyModuleStates() {
728 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
729 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
730 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
733 mw
.loader
.using( [ 'testUsesNestedMissing' ],
735 assert
.ok( false, 'Error handler should be invoked.' );
736 assert
.ok( true ); // Dummy to reach QUnit expect()
738 verifyModuleStates();
742 function ( e
, badmodules
) {
743 assert
.ok( true, 'Error handler should be invoked.' );
744 // As soon as server sets state of 'testMissing' to 'missing'
745 // it will bubble up and trigger the error callback.
746 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
747 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
749 verifyModuleStates();
756 QUnit
.test( 'Skip-function handling', function ( assert
) {
757 mw
.loader
.register( [
758 // [module, version, dependencies, group, source, skip]
759 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
760 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
761 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
764 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
766 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
767 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
768 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
770 function ( e
, badmodules
) {
771 // Should not fail and QUnit would already catch this,
772 // but add a handler anyway to report details from 'badmodules
773 assert
.deepEqual( badmodules
, [], 'Bad modules' );
778 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
779 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
781 done
= assert
.async();
783 // URL to the callback script
784 target
= QUnit
.fixurl(
785 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
787 // Ensure a protocol-relative URL for this test
788 target
= target
.replace( /https?:/, '' );
789 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
791 mw
.loader
.testCallback = function () {
792 // Ensure once, delete now
793 delete mw
.loader
.testCallback
;
794 assert
.ok( true, 'callback' );
799 mw
.loader
.load( target
);
802 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
804 done
= assert
.async();
806 // URL to the callback script
807 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
808 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
810 mw
.loader
.testCallback = function () {
811 // Ensure once, delete now
812 delete mw
.loader
.testCallback
;
813 assert
.ok( true, 'callback' );
818 mw
.loader
.load( target
);
821 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
824 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
826 mw
.loader
.register( '', 'v1' );
827 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
828 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
830 mw
.loader
.implement( '', function () {
834 return mw
.loader
.using( '', function () {
835 assert
.strictEqual( done
, true, 'script ran' );
836 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
840 QUnit
.test( 'Executing race - T112232', function ( assert
) {
843 // The red herring schedules its CSS buffer first. In T112232, a bug in the
844 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
846 'testRaceRedHerring',
848 { css
: [ '.mw-testRaceRedHerring {}' ] }
855 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
858 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
859 return mw
.loader
.using( 'testRaceLoadMe', function () {
860 assert
.strictEqual( done
, true, 'script ran' );
861 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
865 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
867 // Enable store and stub timeout/idle scheduling
868 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
869 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
872 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
876 mw
.loader
.register( 'test.stale', 'v2' );
877 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
879 mw
.loader
.implement( 'test.stale@v1', function () {
883 return mw
.loader
.using( 'test.stale' )
885 assert
.strictEqual( count
, 1 );
886 // After implementing, registry contains version as implemented by the response.
887 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
888 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
889 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stale' ), 'string', 'In store' );
892 // Reset run time, but keep mw.loader.store
893 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
894 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
895 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
897 // Module was stored correctly as v1
898 // On future navigations, it will be ignored until evicted
899 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
903 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
905 // Enable store and stub timeout/idle scheduling
906 this.sandbox
.stub( mw
.loader
.store
, 'enabled', true );
907 this.sandbox
.stub( window
, 'setTimeout', function ( fn
) {
910 this.sandbox
.stub( mw
, 'requestIdleCallback', function ( fn
) {
914 mw
.loader
.register( 'test.stalebc', 'v2' );
915 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
917 mw
.loader
.implement( 'test.stalebc', function () {
921 return mw
.loader
.using( 'test.stalebc' )
923 assert
.strictEqual( script
, 1, 'module script ran' );
924 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
925 assert
.strictEqual( typeof mw
.loader
.store
.get( 'test.stalebc' ), 'string', 'In store' );
928 // Reset run time, but keep mw.loader.store
929 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
930 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
931 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
933 // Legacy behaviour is storing under the expected version,
934 // which woudl lead to whitewashing and stale values (T117587).
935 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
939 QUnit
.test( 'require()', function ( assert
) {
940 mw
.loader
.register( [
941 [ 'test.require1', '0' ],
942 [ 'test.require2', '0' ],
943 [ 'test.require3', '0' ],
944 [ 'test.require4', '0', [ 'test.require3' ] ]
946 mw
.loader
.implement( 'test.require1', function () {} );
947 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
950 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
951 module
.exports = function () {
952 return 'hello world';
955 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
956 var other
= require( 'test.require3' );
963 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
964 var module1
, module2
, module3
, module4
;
966 module1
= require( 'test.require1' );
967 module2
= require( 'test.require2' );
968 module3
= require( 'test.require3' );
969 module4
= require( 'test.require4' );
971 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
972 assert
.strictEqual( module2
, 1, 'export a number' );
973 assert
.strictEqual( module3(), 'hello world', 'export a function' );
974 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
975 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
977 assert
.throws( function () {
978 require( '_badmodule' );
979 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
983 QUnit
.test( 'require() in debug mode', function ( assert
) {
984 var path
= mw
.config
.get( 'wgScriptPath' );
985 mw
.loader
.register( [
986 [ 'test.require.define', '0' ],
987 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
989 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
990 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
992 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
993 var cb
= require( 'test.require.callback' );
994 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
995 // Must use try-catch because cb.later() will throw if require is undefined,
996 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
998 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
1000 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
1005 QUnit
.test( 'Implicit dependencies', function ( assert
) {
1010 mw
.loader
.implement(
1016 mw
.loader
.implement(
1020 siteFromUser
= site
;
1024 return mw
.loader
.using( 'user', function () {
1025 assert
.strictEqual( site
, 1, 'site module' );
1026 assert
.strictEqual( user
, 1, 'user module' );
1027 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
1028 } ).always( function () {
1030 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
1031 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
1035 }( mediaWiki
, jQuery
) );