2 QUnit
.module( 'mediawiki.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 delete mw
.loader
.testCallback
;
21 delete mw
.loader
.testFail
;
27 QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
31 * The sync style load test, for @import. This is, in a way, also an open bug for
32 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
33 * way to get a callback from when a stylesheet is loaded (that is, including any
34 * `@import` rules inside). To work around this, we'll have a little time loop to check
35 * if the styles apply.
37 * Note: This test originally used new Image() and onerror to get a callback
38 * when the url is loaded, but that is fragile since it doesn't monitor the
39 * same request as the css @import, and Safari 4 has issues with
40 * onerror/onload not being fired at all in weird cases like this.
42 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
44 el
= $element
.get( 0 ),
45 styleTestTimeout
= ( QUnit
.config
.testTimeout
|| 5000 ) - 200;
47 function isCssImportApplied() {
48 // Trigger reflow, repaint, redraw, whatever (cross-browser)
49 $element
.css( 'height' );
50 // eslint-disable-next-line no-unused-expressions
52 // eslint-disable-next-line no-self-assign
53 el
.className
= el
.className
;
54 // eslint-disable-next-line no-unused-expressions
55 document
.documentElement
.clientHeight
;
57 return $element
.css( prop
) === val
;
60 function styleTestLoop() {
61 var styleTestSince
= new Date().getTime() - styleTestStart
;
62 // If it is passing or if we timed out, run the real test and stop the loop
63 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
64 assert
.strictEqual( $element
.css( prop
), val
,
65 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
74 // Otherwise, keep polling
75 setTimeout( styleTestLoop
);
79 styleTestStart
= new Date().getTime();
83 function urlStyleTest( selector
, prop
, val
) {
85 mw
.config
.get( 'wgScriptPath' ) +
86 '/tests/qunit/data/styleTest.css.php?' +
95 QUnit
.test( '.using( .., Function callback ) Promise', function ( assert
) {
96 var script
= 0, callback
= 0;
97 mw
.loader
.testCallback = function () {
100 mw
.loader
.implement( 'test.promise', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
102 return mw
.loader
.using( 'test.promise', function () {
104 } ).then( function () {
105 assert
.strictEqual( script
, 1, 'module script ran' );
106 assert
.strictEqual( callback
, 1, 'using() callback ran' );
110 QUnit
.test( 'Prototype method as module name', function ( assert
) {
112 mw
.loader
.testCallback = function () {
115 mw
.loader
.implement( 'hasOwnProperty', [ QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
117 return mw
.loader
.using( 'hasOwnProperty', function () {
118 assert
.strictEqual( call
, 1, 'module script ran' );
122 // Covers mw.loader#sortDependencies (with native Set if available)
123 QUnit
.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert
) {
124 var done
= assert
.async();
126 mw
.loader
.register( [
127 [ 'test.set.circleA', '0', [ 'test.set.circleB' ] ],
128 [ 'test.set.circleB', '0', [ 'test.set.circleC' ] ],
129 [ 'test.set.circleC', '0', [ 'test.set.circleA' ] ]
131 mw
.loader
.using( 'test.set.circleC' ).then(
133 assert
.ok( false, 'Unexpected resolution, expected error.' );
136 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
142 // @covers mw.loader#sortDependencies (with fallback shim)
143 QUnit
.test( '.using() - Error: Circular dependency [StringSet shim]', function ( assert
) {
144 var done
= assert
.async();
152 this.nativeSet
= window
.Set
;
153 window
.Set
= undefined;
154 mw
.redefineFallbacksForTest();
156 mw
.loader
.register( [
157 [ 'test.shim.circleA', '0', [ 'test.shim.circleB' ] ],
158 [ 'test.shim.circleB', '0', [ 'test.shim.circleC' ] ],
159 [ 'test.shim.circleC', '0', [ 'test.shim.circleA' ] ]
161 mw
.loader
.using( 'test.shim.circleC' ).then(
163 assert
.ok( false, 'Unexpected resolution, expected error.' );
166 assert
.ok( /Circular/.test( String( e
) ), 'Detect circular dependency' );
172 QUnit
.test( '.load() - Error: Circular dependency', function ( assert
) {
174 mw
.loader
.register( [
175 [ 'test.load.circleA', '0', [ 'test.load.circleB' ] ],
176 [ 'test.load.circleB', '0', [ 'test.load.circleC' ] ],
177 [ 'test.load.circleC', '0', [ 'test.load.circleA' ] ]
179 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
182 error
: data
.exception
&& data
.exception
.message
,
187 mw
.loader
.load( 'test.load.circleC' );
190 topic
: 'resourceloader.exception',
191 error
: 'Circular reference detected: test.load.circleB -> test.load.circleC',
195 'Detect circular dependency'
199 QUnit
.test( '.load() - Error: Circular dependency (direct)', function ( assert
) {
201 mw
.loader
.register( [
202 [ 'test.load.circleDirect', '0', [ 'test.load.circleDirect' ] ]
204 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
207 error
: data
.exception
&& data
.exception
.message
,
212 mw
.loader
.load( 'test.load.circleDirect' );
215 topic
: 'resourceloader.exception',
216 error
: 'Circular reference detected: test.load.circleDirect -> test.load.circleDirect',
220 'Detect a direct self-dependency'
224 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
225 var done
= assert
.async();
227 mw
.loader
.using( 'test.using.unreg' ).then(
229 assert
.ok( false, 'Unexpected resolution, expected error.' );
232 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
237 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
239 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
242 error
: data
.exception
&& data
.exception
.message
,
247 mw
.loader
.load( 'test.load.unreg' );
250 topic
: 'resourceloader.exception',
251 error
: 'Unknown dependency: test.load.unreg',
258 // Regression test for T36853
259 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
261 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
264 error
: data
.exception
&& data
.exception
.message
,
269 mw
.loader
.register( [
270 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
271 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
273 mw
.loader
.load( 'test.load.missingdep' );
276 topic
: 'resourceloader.exception',
277 error
: 'Unknown dependency: test.load.missingdep2',
284 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
285 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
288 $element
.css( 'float' ),
297 $element
.css( 'float' ),
303 all
: '.mw-test-implement-a { float: right; }'
307 return mw
.loader
.using( 'test.implement.a' );
310 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
311 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
312 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
313 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
314 done
= assert
.async();
317 $element1
.css( 'text-align' ),
322 $element2
.css( 'float' ),
327 $element3
.css( 'text-align' ),
335 // Note: done() must only be called when the entire test is
336 // complete. So, make sure that we don't start until *both*
337 // assertStyleAsync calls have completed.
339 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
340 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
343 if ( pending
=== 0 ) {
347 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
348 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
351 if ( pending
=== 0 ) {
358 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
360 // T42834: Make sure it actually works with more than 1 stylesheet reference
361 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
362 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
368 mw
.loader
.load( 'test.implement.b' );
371 // Backwards compatibility
372 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
373 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
376 $element
.css( 'float' ),
385 $element
.css( 'float' ),
391 all
: '.mw-test-implement-c { float: right; }'
395 return mw
.loader
.using( 'test.implement.c' );
398 // Backwards compatibility
399 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
400 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
401 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
402 done
= assert
.async();
405 $element
.css( 'float' ),
410 $element2
.css( 'text-align' ),
418 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
419 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
424 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
425 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
429 mw
.loader
.load( 'test.implement.d' );
432 QUnit
.test( '.implement( messages before script )', function ( assert
) {
434 'test.implement.order',
436 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'executing', 'state during script execution' );
437 assert
.strictEqual( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
441 'test-foobar': 'Hello Foobar, $1!'
445 return mw
.loader
.using( 'test.implement.order' ).then( function () {
446 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'ready', 'final success state' );
451 QUnit
.test( '.implement( styles with @import )', function ( assert
) {
453 done
= assert
.async();
456 'test.implement.import',
458 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
460 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
461 assert
.strictEqual( $element
.css( 'text-align' ), 'center',
462 'CSS styles after the @import rule are working'
471 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
473 + '.mw-test-implement-import { text-align: center; }'
478 return mw
.loader
.using( 'test.implement.import' );
481 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
482 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
483 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
486 $element
.css( 'float' ),
491 $element2
.css( 'float' ),
496 mw
.loader
.register( [
497 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
498 [ 'test.implement.e2', '0' ]
505 $element
.css( 'float' ),
507 'Depending module\'s style is applied'
511 all
: '.mw-test-implement-e { float: right; }'
519 $element2
.css( 'float' ),
521 'Dependency\'s style is applied'
525 all
: '.mw-test-implement-e2 { float: left; }'
529 return mw
.loader
.using( 'test.implement.e' );
532 QUnit
.test( '.implement( only scripts )', function ( assert
) {
533 mw
.loader
.implement( 'test.onlyscripts', function () {} );
534 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
537 QUnit
.test( '.implement( only messages )', function ( assert
) {
538 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
540 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
542 return mw
.loader
.using( 'test.implement.msgs', function () {
543 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
547 QUnit
.test( '.implement( empty )', function ( assert
) {
548 mw
.loader
.implement( 'test.empty' );
549 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
552 // @covers mw.loader#batchRequest
553 // This is a regression test because in the past we called getCombinedVersion()
554 // for all requested modules, before url splitting took place.
555 // Discovered as part of T188076, but not directly related.
556 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
557 mw
.loader
.register( [
558 // [module, version, dependencies, group, source]
559 [ 'testUrlInc', 'url', [], null, 'testloader' ],
560 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
563 mw
.config
.set( 'wgResourceLoaderMaxQueryLength', 10 );
565 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
567 require( 'testUrlIncDump' ).query
,
569 modules
: 'testUrlIncDump',
570 // Expected: Wrapped hash just for this one module
571 // $hash = hash( 'fnv132', 'dump');
572 // base_convert( $hash, 16, 36 ); // "13e9zzn"
573 // Previously: Wrapped hash for both modules, despite being in separate requests
574 // $hash = hash( 'fnv132', 'urldump' );
575 // base_convert( $hash, 16, 36 ); // "18kz9ca"
581 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
585 // @covers mw.loader#batchRequest
586 // @covers mw.loader#buildModulesString
587 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
588 mw
.loader
.register( [
589 // [module, version, dependencies, group, source]
590 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
591 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
592 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
593 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
596 return mw
.loader
.using( [
601 ] ).then( function ( require
) {
603 require( 'testUrlOrderDump' ).query
,
605 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
606 // Expected: Combined in order after string packing
607 // $hash = hash( 'fnv132', 'urldump12' );
608 // base_convert( $hash, 16, 36 ); // "1knqzan"
609 // Previously: Combined in order of before string packing
610 // $hash = hash( 'fnv132', 'url12dump' );
611 // base_convert( $hash, 16, 36 ); // "11eo3in"
619 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
620 // don't emit an error event
621 this.sandbox
.stub( mw
, 'track' );
623 mw
.loader
.register( [
624 [ 'test.module1', '0' ],
625 [ 'test.module2', '0', [ 'test.module1' ] ],
626 [ 'test.module3', '0', [ 'test.module2' ] ]
628 mw
.loader
.implement( 'test.module1', function () {
629 throw new Error( 'expected' );
631 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
632 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
633 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
635 assert
.strictEqual( mw
.track
.callCount
, 1 );
638 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
639 mw
.loader
.register( [
640 [ 'test.module4', '0' ],
641 [ 'test.module5', '0', [ 'test.module4' ] ],
642 [ 'test.module6', '0', [ 'test.module5' ] ]
644 mw
.loader
.implement( 'test.module4', function () {} );
645 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
646 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
647 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
648 mw
.loader
.implement( 'test.module6', function () {} );
649 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
650 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
651 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
652 mw
.loader
.implement( 'test.module5', function () {} );
653 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
654 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
655 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
658 QUnit
.test( 'Missing dependency', function ( assert
) {
659 mw
.loader
.register( [
660 [ 'test.module7', '0' ],
661 [ 'test.module8', '0', [ 'test.module7' ] ],
662 [ 'test.module9', '0', [ 'test.module8' ] ]
664 mw
.loader
.implement( 'test.module8', function () {} );
665 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
666 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
667 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
668 mw
.loader
.state( { 'test.module7': 'missing' } );
669 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
670 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
671 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
672 mw
.loader
.implement( 'test.module9', function () {} );
673 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
674 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
675 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
679 assert
.ok( false, 'Success fired despite missing dependency' );
680 assert
.ok( true, 'QUnit expected() count dummy' );
682 function ( e
, dependencies
) {
683 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
686 [ 'jquery', 'mediawiki.base', 'test.module7' ],
687 'Error callback called with module test.module7'
694 assert
.ok( false, 'Success fired despite missing dependency' );
695 assert
.ok( true, 'QUnit expected() count dummy' );
697 function ( e
, dependencies
) {
698 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
702 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
703 'Error callback called with all three modules as dependencies'
709 QUnit
.test( 'Dependency handling', function ( assert
) {
710 var done
= assert
.async();
711 mw
.loader
.register( [
712 // [module, version, dependencies, group, source]
713 [ 'testMissing', '1', [], null, 'testloader' ],
714 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
715 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
718 function verifyModuleStates() {
719 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
720 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
721 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
724 mw
.loader
.using( [ 'testUsesNestedMissing' ],
726 assert
.ok( false, 'Error handler should be invoked.' );
727 assert
.ok( true ); // Dummy to reach QUnit expect()
729 verifyModuleStates();
733 function ( e
, badmodules
) {
734 assert
.ok( true, 'Error handler should be invoked.' );
735 // As soon as server sets state of 'testMissing' to 'missing'
736 // it will bubble up and trigger the error callback.
737 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
738 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
740 verifyModuleStates();
747 QUnit
.test( 'Skip-function handling', function ( assert
) {
748 mw
.loader
.register( [
749 // [module, version, dependencies, group, source, skip]
750 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
751 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
752 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
755 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
757 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
758 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
759 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
761 function ( e
, badmodules
) {
762 // Should not fail and QUnit would already catch this,
763 // but add a handler anyway to report details from 'badmodules
764 assert
.deepEqual( badmodules
, [], 'Bad modules' );
769 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
770 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
772 done
= assert
.async();
774 // URL to the callback script
775 target
= QUnit
.fixurl(
776 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
778 // Ensure a protocol-relative URL for this test
779 target
= target
.replace( /https?:/, '' );
780 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
782 mw
.loader
.testCallback = function () {
783 // Ensure once, delete now
784 delete mw
.loader
.testCallback
;
785 assert
.ok( true, 'callback' );
790 mw
.loader
.load( target
);
793 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
795 done
= assert
.async();
797 // URL to the callback script
798 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
799 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
801 mw
.loader
.testCallback = function () {
802 // Ensure once, delete now
803 delete mw
.loader
.testCallback
;
804 assert
.ok( true, 'callback' );
809 mw
.loader
.load( target
);
812 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
815 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
817 mw
.loader
.register( '', 'v1' );
818 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
819 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
821 mw
.loader
.implement( '', function () {
825 return mw
.loader
.using( '', function () {
826 assert
.strictEqual( done
, true, 'script ran' );
827 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
831 QUnit
.test( 'Executing race - T112232', function ( assert
) {
834 // The red herring schedules its CSS buffer first. In T112232, a bug in the
835 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
837 'testRaceRedHerring',
839 { css
: [ '.mw-testRaceRedHerring {}' ] }
846 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
849 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
850 return mw
.loader
.using( 'testRaceLoadMe', function () {
851 assert
.strictEqual( done
, true, 'script ran' );
852 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
856 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
858 mw
.loader
.store
.enabled
= true;
859 mw
.loader
.register( 'test.stale', 'v2' );
860 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
862 mw
.loader
.implement( 'test.stale@v1', function () {
866 return mw
.loader
.using( 'test.stale' )
868 assert
.strictEqual( count
, 1 );
869 // After implementing, registry contains version as implemented by the response.
870 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
871 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
872 assert
.ok( mw
.loader
.store
.get( 'test.stale' ), 'In store' );
875 // Reset run time, but keep mw.loader.store
876 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
877 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
878 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
880 // Module was stored correctly as v1
881 // On future navigations, it will be ignored until evicted
882 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
886 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
888 mw
.loader
.store
.enabled
= true;
889 mw
.loader
.register( 'test.stalebc', 'v2' );
890 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
892 mw
.loader
.implement( 'test.stalebc', function () {
896 return mw
.loader
.using( 'test.stalebc' )
898 assert
.strictEqual( script
, 1, 'module script ran' );
899 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
900 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
903 // Reset run time, but keep mw.loader.store
904 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
905 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
906 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
908 // Legacy behaviour is storing under the expected version,
909 // which woudl lead to whitewashing and stale values (T117587).
910 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
914 QUnit
.test( 'require()', function ( assert
) {
915 mw
.loader
.register( [
916 [ 'test.require1', '0' ],
917 [ 'test.require2', '0' ],
918 [ 'test.require3', '0' ],
919 [ 'test.require4', '0', [ 'test.require3' ] ]
921 mw
.loader
.implement( 'test.require1', function () {} );
922 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
925 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
926 module
.exports = function () {
927 return 'hello world';
930 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
931 var other
= require( 'test.require3' );
938 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
939 var module1
, module2
, module3
, module4
;
941 module1
= require( 'test.require1' );
942 module2
= require( 'test.require2' );
943 module3
= require( 'test.require3' );
944 module4
= require( 'test.require4' );
946 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
947 assert
.strictEqual( module2
, 1, 'export a number' );
948 assert
.strictEqual( module3(), 'hello world', 'export a function' );
949 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
950 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
952 assert
.throws( function () {
953 require( '_badmodule' );
954 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
958 QUnit
.test( 'require() in debug mode', function ( assert
) {
959 var path
= mw
.config
.get( 'wgScriptPath' );
960 mw
.loader
.register( [
961 [ 'test.require.define', '0' ],
962 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
964 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
965 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
967 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
968 var cb
= require( 'test.require.callback' );
969 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
970 // Must use try-catch because cb.later() will throw if require is undefined,
971 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
973 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
975 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
980 QUnit
.test( 'Implicit dependencies', function ( assert
) {
999 return mw
.loader
.using( 'user', function () {
1000 assert
.strictEqual( site
, 1, 'site module' );
1001 assert
.strictEqual( user
, 1, 'user module' );
1002 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
1003 } ).always( function () {
1005 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
1006 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
1010 }( mediaWiki
, jQuery
) );