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.circle1', '0', [ 'test.circle2' ] ],
128 [ 'test.circle2', '0', [ 'test.circle3' ] ],
129 [ 'test.circle3', '0', [ 'test.circle1' ] ]
131 mw
.loader
.using( 'test.circle3' ).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.circle1', '0', [ 'test.shim.circle2' ] ],
158 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
159 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
161 mw
.loader
.using( 'test.shim.circle3' ).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.circleA', '0', [ 'test.circleB' ] ],
176 [ 'test.circleB', '0', [ 'test.circleC' ] ],
177 [ 'test.circleC', '0', [ 'test.circleA' ] ]
179 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
182 error
: data
.exception
&& data
.exception
.message
,
187 mw
.loader
.load( 'test.circleC' );
190 topic
: 'resourceloader.exception',
191 error
: 'Circular reference detected: test.circleB -> test.circleC',
195 'Detect circular dependency'
199 QUnit
.test( '.using() - Error: Unregistered', function ( assert
) {
200 var done
= assert
.async();
202 mw
.loader
.using( 'test.using.unreg' ).then(
204 assert
.ok( false, 'Unexpected resolution, expected error.' );
207 assert
.ok( /Unknown/.test( String( e
) ), 'Detect unknown dependency' );
212 QUnit
.test( '.load() - Error: Unregistered', function ( assert
) {
214 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
217 error
: data
.exception
&& data
.exception
.message
,
222 mw
.loader
.load( 'test.load.unreg' );
225 topic
: 'resourceloader.exception',
226 error
: 'Unknown dependency: test.load.unreg',
233 // Regression test for T36853
234 QUnit
.test( '.load() - Error: Missing dependency', function ( assert
) {
236 this.sandbox
.stub( mw
, 'track', function ( topic
, data
) {
239 error
: data
.exception
&& data
.exception
.message
,
244 mw
.loader
.register( [
245 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
246 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
248 mw
.loader
.load( 'test.load.missingdep' );
251 topic
: 'resourceloader.exception',
252 error
: 'Unknown dependency: test.load.missingdep2',
259 QUnit
.test( '.implement( styles={ "css": [text, ..] } )', function ( assert
) {
260 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
263 $element
.css( 'float' ),
272 $element
.css( 'float' ),
278 all
: '.mw-test-implement-a { float: right; }'
282 return mw
.loader
.using( 'test.implement.a' );
285 QUnit
.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert
) {
286 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
287 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
288 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
289 done
= assert
.async();
292 $element1
.css( 'text-align' ),
297 $element2
.css( 'float' ),
302 $element3
.css( 'text-align' ),
310 // Note: done() must only be called when the entire test is
311 // complete. So, make sure that we don't start until *both*
312 // assertStyleAsync calls have completed.
314 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
315 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
318 if ( pending
=== 0 ) {
322 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
323 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
326 if ( pending
=== 0 ) {
333 print
: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
335 // T42834: Make sure it actually works with more than 1 stylesheet reference
336 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
337 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
343 mw
.loader
.load( 'test.implement.b' );
346 // Backwards compatibility
347 QUnit
.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert
) {
348 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
351 $element
.css( 'float' ),
360 $element
.css( 'float' ),
366 all
: '.mw-test-implement-c { float: right; }'
370 return mw
.loader
.using( 'test.implement.c' );
373 // Backwards compatibility
374 QUnit
.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert
) {
375 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
376 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
377 done
= assert
.async();
380 $element
.css( 'float' ),
385 $element2
.css( 'text-align' ),
393 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
394 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
399 all
: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
400 print
: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
404 mw
.loader
.load( 'test.implement.d' );
407 QUnit
.test( '.implement( messages before script )', function ( assert
) {
409 'test.implement.order',
411 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'executing', 'state during script execution' );
412 assert
.strictEqual( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
416 'test-foobar': 'Hello Foobar, $1!'
420 return mw
.loader
.using( 'test.implement.order' ).then( function () {
421 assert
.strictEqual( mw
.loader
.getState( 'test.implement.order' ), 'ready', 'final success state' );
426 QUnit
.test( '.implement( styles with @import )', function ( assert
) {
428 done
= assert
.async();
431 'test.implement.import',
433 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
435 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
436 assert
.strictEqual( $element
.css( 'text-align' ), 'center',
437 'CSS styles after the @import rule are working'
446 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
448 + '.mw-test-implement-import { text-align: center; }'
453 return mw
.loader
.using( 'test.implement.import' );
456 QUnit
.test( '.implement( dependency with styles )', function ( assert
) {
457 var $element
= $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
458 $element2
= $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
461 $element
.css( 'float' ),
466 $element2
.css( 'float' ),
471 mw
.loader
.register( [
472 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
473 [ 'test.implement.e2', '0' ]
480 $element
.css( 'float' ),
482 'Depending module\'s style is applied'
486 all
: '.mw-test-implement-e { float: right; }'
494 $element2
.css( 'float' ),
496 'Dependency\'s style is applied'
500 all
: '.mw-test-implement-e2 { float: left; }'
504 return mw
.loader
.using( 'test.implement.e' );
507 QUnit
.test( '.implement( only scripts )', function ( assert
) {
508 mw
.loader
.implement( 'test.onlyscripts', function () {} );
509 assert
.strictEqual( mw
.loader
.getState( 'test.onlyscripts' ), 'ready' );
512 QUnit
.test( '.implement( only messages )', function ( assert
) {
513 assert
.assertFalse( mw
.messages
.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
515 mw
.loader
.implement( 'test.implement.msgs', [], {}, { T31107
: 'loaded' } );
517 return mw
.loader
.using( 'test.implement.msgs', function () {
518 assert
.ok( mw
.messages
.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
522 QUnit
.test( '.implement( empty )', function ( assert
) {
523 mw
.loader
.implement( 'test.empty' );
524 assert
.strictEqual( mw
.loader
.getState( 'test.empty' ), 'ready' );
527 // @covers mw.loader#batchRequest
528 // This is a regression test because in the past we called getCombinedVersion()
529 // for all requested modules, before url splitting took place.
530 // Discovered as part of T188076, but not directly related.
531 QUnit
.test( 'Url composition (modules considered for version)', function ( assert
) {
532 mw
.loader
.register( [
533 // [module, version, dependencies, group, source]
534 [ 'testUrlInc', 'url', [], null, 'testloader' ],
535 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
538 mw
.config
.set( 'wgResourceLoaderMaxQueryLength', 10 );
540 return mw
.loader
.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require
) {
542 require( 'testUrlIncDump' ).query
,
544 modules
: 'testUrlIncDump',
545 // Expected: Wrapped hash just for this one module
546 // $hash = hash( 'fnv132', 'dump');
547 // base_convert( $hash, 16, 36 ); // "13e9zzn"
548 // Previously: Wrapped hash for both modules, despite being in separate requests
549 // $hash = hash( 'fnv132', 'urldump' );
550 // base_convert( $hash, 16, 36 ); // "18kz9ca"
556 assert
.strictEqual( mw
.loader
.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
560 // @covers mw.loader#batchRequest
561 // @covers mw.loader#buildModulesString
562 QUnit
.test( 'Url composition (order of modules for version) – T188076', function ( assert
) {
563 mw
.loader
.register( [
564 // [module, version, dependencies, group, source]
565 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
566 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
567 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
568 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
571 return mw
.loader
.using( [
576 ] ).then( function ( require
) {
578 require( 'testUrlOrderDump' ).query
,
580 modules
: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
581 // Expected: Combined in order after string packing
582 // $hash = hash( 'fnv132', 'urldump12' );
583 // base_convert( $hash, 16, 36 ); // "1knqzan"
584 // Previously: Combined in order of before string packing
585 // $hash = hash( 'fnv132', 'url12dump' );
586 // base_convert( $hash, 16, 36 ); // "11eo3in"
594 QUnit
.test( 'Broken indirect dependency', function ( assert
) {
595 // don't emit an error event
596 this.sandbox
.stub( mw
, 'track' );
598 mw
.loader
.register( [
599 [ 'test.module1', '0' ],
600 [ 'test.module2', '0', [ 'test.module1' ] ],
601 [ 'test.module3', '0', [ 'test.module2' ] ]
603 mw
.loader
.implement( 'test.module1', function () {
604 throw new Error( 'expected' );
606 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
607 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
608 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
610 assert
.strictEqual( mw
.track
.callCount
, 1 );
613 QUnit
.test( 'Out-of-order implementation', function ( assert
) {
614 mw
.loader
.register( [
615 [ 'test.module4', '0' ],
616 [ 'test.module5', '0', [ 'test.module4' ] ],
617 [ 'test.module6', '0', [ 'test.module5' ] ]
619 mw
.loader
.implement( 'test.module4', function () {} );
620 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
621 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
622 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
623 mw
.loader
.implement( 'test.module6', function () {} );
624 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
625 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
626 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
627 mw
.loader
.implement( 'test.module5', function () {} );
628 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
629 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
630 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
633 QUnit
.test( 'Missing dependency', function ( assert
) {
634 mw
.loader
.register( [
635 [ 'test.module7', '0' ],
636 [ 'test.module8', '0', [ 'test.module7' ] ],
637 [ 'test.module9', '0', [ 'test.module8' ] ]
639 mw
.loader
.implement( 'test.module8', function () {} );
640 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
641 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
642 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
643 mw
.loader
.state( 'test.module7', 'missing' );
644 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
645 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
646 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
647 mw
.loader
.implement( 'test.module9', function () {} );
648 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
649 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
650 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
654 assert
.ok( false, 'Success fired despite missing dependency' );
655 assert
.ok( true, 'QUnit expected() count dummy' );
657 function ( e
, dependencies
) {
658 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
661 [ 'jquery', 'mediawiki.base', 'test.module7' ],
662 'Error callback called with module test.module7'
669 assert
.ok( false, 'Success fired despite missing dependency' );
670 assert
.ok( true, 'QUnit expected() count dummy' );
672 function ( e
, dependencies
) {
673 assert
.strictEqual( Array
.isArray( dependencies
), true, 'Expected array of dependencies' );
677 [ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ],
678 'Error callback called with all three modules as dependencies'
684 QUnit
.test( 'Dependency handling', function ( assert
) {
685 var done
= assert
.async();
686 mw
.loader
.register( [
687 // [module, version, dependencies, group, source]
688 [ 'testMissing', '1', [], null, 'testloader' ],
689 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
690 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
693 function verifyModuleStates() {
694 assert
.strictEqual( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
695 assert
.strictEqual( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
696 assert
.strictEqual( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
699 mw
.loader
.using( [ 'testUsesNestedMissing' ],
701 assert
.ok( false, 'Error handler should be invoked.' );
702 assert
.ok( true ); // Dummy to reach QUnit expect()
704 verifyModuleStates();
708 function ( e
, badmodules
) {
709 assert
.ok( true, 'Error handler should be invoked.' );
710 // As soon as server spits out state('testMissing', 'missing');
711 // it will bubble up and trigger the error callback.
712 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
713 assert
.deepEqual( badmodules
, [ 'testMissing' ], 'Bad modules as expected.' );
715 verifyModuleStates();
722 QUnit
.test( 'Skip-function handling', function ( assert
) {
723 mw
.loader
.register( [
724 // [module, version, dependencies, group, source, skip]
725 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
726 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
727 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
730 return mw
.loader
.using( [ 'testUsesSkippable' ] ).then(
732 assert
.strictEqual( mw
.loader
.getState( 'testSkipped' ), 'ready', 'Skipped module' );
733 assert
.strictEqual( mw
.loader
.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
734 assert
.strictEqual( mw
.loader
.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
736 function ( e
, badmodules
) {
737 // Should not fail and QUnit would already catch this,
738 // but add a handler anyway to report details from 'badmodules
739 assert
.deepEqual( badmodules
, [], 'Bad modules' );
744 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
745 QUnit
.test( '.load( "//protocol-relative" ) - T32825', function ( assert
) {
747 done
= assert
.async();
749 // URL to the callback script
750 target
= QUnit
.fixurl(
751 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
753 // Ensure a protocol-relative URL for this test
754 target
= target
.replace( /https?:/, '' );
755 assert
.strictEqual( target
.slice( 0, 2 ), '//', 'URL is protocol-relative' );
757 mw
.loader
.testCallback = function () {
758 // Ensure once, delete now
759 delete mw
.loader
.testCallback
;
760 assert
.ok( true, 'callback' );
765 mw
.loader
.load( target
);
768 QUnit
.test( '.load( "/absolute-path" )', function ( assert
) {
770 done
= assert
.async();
772 // URL to the callback script
773 target
= QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
774 assert
.strictEqual( target
.slice( 0, 1 ), '/', 'URL is relative to document root' );
776 mw
.loader
.testCallback = function () {
777 // Ensure once, delete now
778 delete mw
.loader
.testCallback
;
779 assert
.ok( true, 'callback' );
784 mw
.loader
.load( target
);
787 QUnit
.test( 'Empty string module name - T28804', function ( assert
) {
790 assert
.strictEqual( mw
.loader
.getState( '' ), null, 'State (unregistered)' );
792 mw
.loader
.register( '', 'v1' );
793 assert
.strictEqual( mw
.loader
.getState( '' ), 'registered', 'State (registered)' );
794 assert
.strictEqual( mw
.loader
.getVersion( '' ), 'v1', 'Version' );
796 mw
.loader
.implement( '', function () {
800 return mw
.loader
.using( '', function () {
801 assert
.strictEqual( done
, true, 'script ran' );
802 assert
.strictEqual( mw
.loader
.getState( '' ), 'ready', 'State (ready)' );
806 QUnit
.test( 'Executing race - T112232', function ( assert
) {
809 // The red herring schedules its CSS buffer first. In T112232, a bug in the
810 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
812 'testRaceRedHerring',
814 { css
: [ '.mw-testRaceRedHerring {}' ] }
821 { css
: [ '.mw-testRaceLoadMe { float: left; }' ] }
824 mw
.loader
.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
825 return mw
.loader
.using( 'testRaceLoadMe', function () {
826 assert
.strictEqual( done
, true, 'script ran' );
827 assert
.strictEqual( mw
.loader
.getState( 'testRaceLoadMe' ), 'ready', 'state' );
831 QUnit
.test( 'Stale response caching - T117587', function ( assert
) {
833 mw
.loader
.store
.enabled
= true;
834 mw
.loader
.register( 'test.stale', 'v2' );
835 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
837 mw
.loader
.implement( 'test.stale@v1', function () {
841 return mw
.loader
.using( 'test.stale' )
843 assert
.strictEqual( count
, 1 );
844 // After implementing, registry contains version as implemented by the response.
845 assert
.strictEqual( mw
.loader
.getVersion( 'test.stale' ), 'v1', 'Override version' );
846 assert
.strictEqual( mw
.loader
.getState( 'test.stale' ), 'ready' );
847 assert
.ok( mw
.loader
.store
.get( 'test.stale' ), 'In store' );
850 // Reset run time, but keep mw.loader.store
851 mw
.loader
.moduleRegistry
[ 'test.stale' ].script
= undefined;
852 mw
.loader
.moduleRegistry
[ 'test.stale' ].state
= 'registered';
853 mw
.loader
.moduleRegistry
[ 'test.stale' ].version
= 'v2';
855 // Module was stored correctly as v1
856 // On future navigations, it will be ignored until evicted
857 assert
.strictEqual( mw
.loader
.store
.get( 'test.stale' ), false, 'Not in store' );
861 QUnit
.test( 'Stale response caching - backcompat', function ( assert
) {
863 mw
.loader
.store
.enabled
= true;
864 mw
.loader
.register( 'test.stalebc', 'v2' );
865 assert
.strictEqual( mw
.loader
.store
.get( 'test.stalebc' ), false, 'Not in store' );
867 mw
.loader
.implement( 'test.stalebc', function () {
871 return mw
.loader
.using( 'test.stalebc' )
873 assert
.strictEqual( script
, 1, 'module script ran' );
874 assert
.strictEqual( mw
.loader
.getState( 'test.stalebc' ), 'ready' );
875 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
878 // Reset run time, but keep mw.loader.store
879 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].script
= undefined;
880 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].state
= 'registered';
881 mw
.loader
.moduleRegistry
[ 'test.stalebc' ].version
= 'v2';
883 // Legacy behaviour is storing under the expected version,
884 // which woudl lead to whitewashing and stale values (T117587).
885 assert
.ok( mw
.loader
.store
.get( 'test.stalebc' ), 'In store' );
889 QUnit
.test( 'require()', function ( assert
) {
890 mw
.loader
.register( [
891 [ 'test.require1', '0' ],
892 [ 'test.require2', '0' ],
893 [ 'test.require3', '0' ],
894 [ 'test.require4', '0', [ 'test.require3' ] ]
896 mw
.loader
.implement( 'test.require1', function () {} );
897 mw
.loader
.implement( 'test.require2', function ( $, jQuery
, require
, module
) {
900 mw
.loader
.implement( 'test.require3', function ( $, jQuery
, require
, module
) {
901 module
.exports = function () {
902 return 'hello world';
905 mw
.loader
.implement( 'test.require4', function ( $, jQuery
, require
, module
) {
906 var other
= require( 'test.require3' );
913 return mw
.loader
.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require
) {
914 var module1
, module2
, module3
, module4
;
916 module1
= require( 'test.require1' );
917 module2
= require( 'test.require2' );
918 module3
= require( 'test.require3' );
919 module4
= require( 'test.require4' );
921 assert
.strictEqual( typeof module1
, 'object', 'export of module with no export' );
922 assert
.strictEqual( module2
, 1, 'export a number' );
923 assert
.strictEqual( module3(), 'hello world', 'export a function' );
924 assert
.strictEqual( typeof module4
.pizza
, 'function', 'export an object' );
925 assert
.strictEqual( module4
.pizza(), 'hello world', 'module can require other modules' );
927 assert
.throws( function () {
928 require( '_badmodule' );
929 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
933 QUnit
.test( 'require() in debug mode', function ( assert
) {
934 var path
= mw
.config
.get( 'wgScriptPath' );
935 mw
.loader
.register( [
936 [ 'test.require.define', '0' ],
937 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
939 mw
.loader
.implement( 'test.require.callback', [ QUnit
.fixurl( path
+ '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
940 mw
.loader
.implement( 'test.require.define', [ QUnit
.fixurl( path
+ '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
942 return mw
.loader
.using( 'test.require.callback' ).then( function ( require
) {
943 var cb
= require( 'test.require.callback' );
944 assert
.strictEqual( cb
.immediate
, 'Defined.', 'module.exports and require work in debug mode' );
945 // Must use try-catch because cb.later() will throw if require is undefined,
946 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
948 assert
.strictEqual( cb
.later(), 'Defined.', 'require works asynchrously in debug mode' );
950 assert
.strictEqual( String( e
), null, 'require works asynchrously in debug mode' );
955 QUnit
.test( 'Implicit dependencies', function ( assert
) {
974 return mw
.loader
.using( 'user', function () {
975 assert
.strictEqual( site
, 1, 'site module' );
976 assert
.strictEqual( user
, 1, 'user module' );
977 assert
.strictEqual( siteFromUser
, 1, 'site ran before user' );
978 } ).always( function () {
980 mw
.loader
.moduleRegistry
[ 'site' ].state
= 'registered';
981 mw
.loader
.moduleRegistry
[ 'user' ].state
= 'registered';
985 }( mediaWiki
, jQuery
) );