( function ( mw, $ ) {
- QUnit.module( 'mediawiki (mw.loader)' );
+ QUnit.module( 'mediawiki (mw.loader)', QUnit.newMwEnvironment( {
+ setup: function () {
+ mw.loader.store.enabled = false;
+ },
+ teardown: function () {
+ mw.loader.store.enabled = false;
+ }
+ } ) );
mw.loader.addSource(
'testloader',
} );
} );
+ QUnit.test( '.using() Error: Circular dependency', function ( assert ) {
+ mw.loader.register( [
+ [ 'test.circle1', '0', [ 'test.circle2' ] ],
+ [ 'test.circle2', '0', [ 'test.circle3' ] ],
+ [ 'test.circle3', '0', [ 'test.circle1' ] ]
+ ] );
+ mw.loader.using( 'test.circle3' ).then(
+ function done() {
+ assert.ok( false, 'Unexpected resolution, expected error.' );
+ },
+ function fail( e ) {
+ assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
+ }
+ );
+ } );
+
+ QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
+ mw.loader.register( [
+ [ 'test.circleA', '0', [ 'test.circleB' ] ],
+ [ 'test.circleB', '0', [ 'test.circleC' ] ],
+ [ 'test.circleC', '0', [ 'test.circleA' ] ]
+ ] );
+ assert.throws( function () {
+ mw.loader.load( 'test.circleC' );
+ }, /Circular/, 'Detect circular dependency' );
+ } );
+
+ QUnit.test( '.using() - Error: Unregistered', function ( assert ) {
+ mw.loader.using( 'test.using.unreg' ).then(
+ function done() {
+ assert.ok( false, 'Unexpected resolution, expected error.' );
+ },
+ function fail( e ) {
+ assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' );
+ }
+ );
+ } );
+
+ QUnit.test( '.load() - Error: Unregistered (ignored)', 0, function ( assert ) {
+ mw.loader.load( 'test.using.unreg2' );
+ } );
+
QUnit.test( '.implement( styles={ "css": [text, ..] } )', 2, function ( assert ) {
var $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
assert.strictEqual( mw.track.callCount, 1 );
} );
- QUnit.test( 'Circular dependency', 1, function ( assert ) {
- mw.loader.register( [
- [ 'test.circle1', '0', [ 'test.circle2' ] ],
- [ 'test.circle2', '0', [ 'test.circle3' ] ],
- [ 'test.circle3', '0', [ 'test.circle1' ] ]
- ] );
- assert.throws( function () {
- mw.loader.using( 'test.circle3' );
- }, /Circular/, 'Detect circular dependency' );
- } );
-
QUnit.test( 'Out-of-order implementation', 9, function ( assert ) {
mw.loader.register( [
[ 'test.module4', '0' ],
mw.loader.load( target );
} );
+ QUnit.test( 'Empty string module name - T28804', function ( assert ) {
+ var done = false;
+
+ assert.strictEqual( mw.loader.getState( '' ), null, 'State (unregistered)' );
+
+ mw.loader.register( '', 'v1' );
+ assert.strictEqual( mw.loader.getState( '' ), 'registered', 'State (registered)' );
+ assert.strictEqual( mw.loader.getVersion( '' ), 'v1', 'Version' );
+
+ mw.loader.implement( '', function () {
+ done = true;
+ } );
+
+ return mw.loader.using( '', function () {
+ assert.strictEqual( done, true, 'script ran' );
+ assert.strictEqual( mw.loader.getState( '' ), 'ready', 'State (ready)' );
+ } );
+ } );
+
QUnit.test( 'Executing race - T112232', 2, function ( assert ) {
var done = false;
} );
} );
+ QUnit.test( 'Stale response caching - T117587', function ( assert ) {
+ var count = 0;
+ mw.loader.store.enabled = true;
+ mw.loader.register( 'test.stale', 'v2' );
+ assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
+
+ mw.loader.implement( 'test.stale@v1', function () {
+ count++;
+ } );
+
+ return mw.loader.using( 'test.stale' )
+ .then( function () {
+ assert.strictEqual( count, 1 );
+ // After implementing, registry contains version as implemented by the response.
+ assert.strictEqual( mw.loader.getVersion( 'test.stale' ), 'v1', 'Override version' );
+ assert.strictEqual( mw.loader.getState( 'test.stale' ), 'ready' );
+ assert.ok( mw.loader.store.get( 'test.stale' ), 'In store' );
+ } )
+ .then( function () {
+ // Reset run time, but keep mw.loader.store
+ mw.loader.moduleRegistry[ 'test.stale' ].script = undefined;
+ mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered';
+ mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2';
+
+ // Module was stored correctly as v1
+ // On future navigations, it will be ignored until evicted
+ assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
+ } );
+ } );
+
+ QUnit.test( 'Stale response caching - backcompat', function ( assert ) {
+ var count = 0;
+ mw.loader.store.enabled = true;
+ mw.loader.register( 'test.stalebc', 'v2' );
+ assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' );
+
+ mw.loader.implement( 'test.stalebc', function () {
+ count++;
+ } );
+
+ return mw.loader.using( 'test.stalebc' )
+ .then( function () {
+ assert.strictEqual( count, 1 );
+ assert.strictEqual( mw.loader.getState( 'test.stalebc' ), 'ready' );
+ assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
+ } )
+ .then( function () {
+ // Reset run time, but keep mw.loader.store
+ mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined;
+ mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered';
+ mw.loader.moduleRegistry[ 'test.stalebc' ].version = 'v2';
+
+ // Legacy behaviour is storing under the expected version,
+ // which woudl lead to whitewashing and stale values (T117587).
+ assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
+ } );
+ } );
+
QUnit.test( 'require()', 6, function ( assert ) {
mw.loader.register( [
[ 'test.require1', '0' ],
} );
} );
- QUnit.test( 'require() in debug mode', 1, function ( assert ) {
+ QUnit.test( 'require() in debug mode', function ( assert ) {
var path = mw.config.get( 'wgScriptPath' );
mw.loader.register( [
[ 'test.require.define', '0' ],
mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
return mw.loader.using( 'test.require.callback' ).then( function ( require ) {
- var exported = require( 'test.require.callback' );
- assert.strictEqual( exported, 'Require worked.Define worked.',
- 'module.exports worked in debug mode' );
+ var cb = require( 'test.require.callback' );
+ assert.strictEqual( cb.immediate, 'Defined.', 'module.exports and require work in debug mode' );
+ // Must use try-catch because cb.later() will throw if require is undefined,
+ // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
+ try {
+ assert.strictEqual( cb.later(), 'Defined.', 'require works asynchrously in debug mode' );
+ } catch ( e ) {
+ assert.equal( null, String( e ), 'require works asynchrously in debug mode' );
+ }
}, function () {
assert.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
} );