"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tests/qunit/suites/resources/mediawiki/mediawiki.loader.test.js" between
mediawiki-1.31.1.tar.gz and mediawiki-1.32.0.tar.gz

About: MediaWiki is a wiki engine (the collaborative editing software that runs for e.g. Wikipedia, the free encyclopedia).

mediawiki.loader.test.js  (mediawiki-1.31.1):mediawiki.loader.test.js  (mediawiki-1.32.0)
( function ( mw, $ ) { ( function () {
QUnit.module( 'mediawiki.loader', QUnit.newMwEnvironment( { QUnit.module( 'mediawiki.loader', QUnit.newMwEnvironment( {
setup: function ( assert ) { setup: function ( assert ) {
mw.loader.store.enabled = false;
// Expose for load.mock.php // Expose for load.mock.php
mw.loader.testFail = function ( reason ) { mw.loader.testFail = function ( reason ) {
assert.ok( false, reason ); assert.ok( false, reason );
}; };
this.useStubClock = function () {
this.clock = this.sandbox.useFakeTimers();
this.tick = function ( forward ) {
return this.clock.tick( forward || 1 );
};
this.sandbox.stub( mw, 'requestIdleCallback', mw.
requestIdleCallbackInternal );
};
}, },
teardown: function () { teardown: function () {
mw.loader.store.enabled = false;
// Teardown for StringSet shim test // Teardown for StringSet shim test
if ( this.nativeSet ) { if ( this.nativeSet ) {
window.Set = this.nativeSet; window.Set = this.nativeSet;
mw.redefineFallbacksForTest(); mw.redefineFallbacksForTest();
} }
// Remove any remaining temporary statics // Remove any remaining temporary statics
// exposed for cross-file mocks. // exposed for cross-file mocks.
delete mw.loader.testCallback; delete mw.loader.testCallback;
delete mw.loader.testFail; delete mw.loader.testFail;
} }
} ) ); } ) );
mw.loader.addSource( mw.loader.addSource( {
'testloader', testloader:
QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/dat QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/q
a/load.mock.php' ) unit/data/load.mock.php' )
); } );
/** /**
* The sync style load test (for @import). This is, in a way, also an ope n bug for * The sync style load test, for @import. This is, in a way, also an open bug for
* ResourceLoader ("execute js after styles are loaded"), but browsers do n't offer a * ResourceLoader ("execute js after styles are loaded"), but browsers do n't offer a
* way to get a callback from when a stylesheet is loaded (that is, inclu ding any * way to get a callback from when a stylesheet is loaded (that is, inclu ding any
* `@import` rules inside). To work around this, we'll have a little time loop to check * `@import` rules inside). To work around this, we'll have a little time loop to check
* if the styles apply. * if the styles apply.
* *
* Note: This test originally used new Image() and onerror to get a callb ack * Note: This test originally used new Image() and onerror to get a callb ack
* when the url is loaded, but that is fragile since it doesn't monitor t he * when the url is loaded, but that is fragile since it doesn't monitor t he
* same request as the css @import, and Safari 4 has issues with * same request as the css @import, and Safari 4 has issues with
* onerror/onload not being fired at all in weird cases like this. * onerror/onload not being fired at all in weird cases like this.
*/ */
function assertStyleAsync( assert, $element, prop, val, fn ) { function assertStyleAsync( assert, $element, prop, val, fn ) {
var styleTestStart, var styleTestStart,
el = $element.get( 0 ), el = $element.get( 0 ),
styleTestTimeout = ( QUnit.config.testTimeout || 5000 ) - 200; styleTestTimeout = ( QUnit.config.testTimeout || 5000 ) - 200;
function isCssImportApplied() { function isCssImportApplied() {
// Trigger reflow, repaint, redraw, whatever (cross-brows er) // Trigger reflow, repaint, redraw, whatever (cross-brows er)
$element.css( 'height' ); $element.css( 'height' );
// eslint-disable-next-line no-unused-expressions // eslint-disable-next-line no-unused-expressions
el.innerHTML; el.innerHTML;
// eslint-disable-next-line no-self-assign
el.className = el.className; el.className = el.className;
// eslint-disable-next-line no-unused-expressions // eslint-disable-next-line no-unused-expressions
document.documentElement.clientHeight; document.documentElement.clientHeight;
return $element.css( prop ) === val; return $element.css( prop ) === val;
} }
function styleTestLoop() { function styleTestLoop() {
var styleTestSince = new Date().getTime() - styleTestStar t; var styleTestSince = new Date().getTime() - styleTestStar t;
// If it is passing or if we timed out, run the real test and stop the loop // If it is passing or if we timed out, run the real test and stop the loop
if ( isCssImportApplied() || styleTestSince > styleTestTi meout ) { if ( isCssImportApplied() || styleTestSince > styleTestTi meout ) {
assert.equal( $element.css( prop ), val, assert.strictEqual( $element.css( prop ), val,
'style "' + prop + ': ' + val + '" from u rl is applied (after ' + styleTestSince + 'ms)' 'style "' + prop + ': ' + val + '" from u rl is applied (after ' + styleTestSince + 'ms)'
); );
if ( fn ) { if ( fn ) {
fn(); fn();
} }
return; return;
} }
// Otherwise, keep polling // Otherwise, keep polling
skipping to change at line 126 skipping to change at line 132
return mw.loader.using( 'hasOwnProperty', function () { return mw.loader.using( 'hasOwnProperty', function () {
assert.strictEqual( call, 1, 'module script ran' ); assert.strictEqual( call, 1, 'module script ran' );
} ); } );
} ); } );
// Covers mw.loader#sortDependencies (with native Set if available) // Covers mw.loader#sortDependencies (with native Set if available)
QUnit.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert ) { QUnit.test( '.using() - Error: Circular dependency [StringSet default]', function ( assert ) {
var done = assert.async(); var done = assert.async();
mw.loader.register( [ mw.loader.register( [
[ 'test.circle1', '0', [ 'test.circle2' ] ], [ 'test.set.circleA', '0', [ 'test.set.circleB' ] ],
[ 'test.circle2', '0', [ 'test.circle3' ] ], [ 'test.set.circleB', '0', [ 'test.set.circleC' ] ],
[ 'test.circle3', '0', [ 'test.circle1' ] ] [ 'test.set.circleC', '0', [ 'test.set.circleA' ] ]
] ); ] );
mw.loader.using( 'test.circle3' ).then( mw.loader.using( 'test.set.circleC' ).then(
function done() { function done() {
assert.ok( false, 'Unexpected resolution, expecte d error.' ); assert.ok( false, 'Unexpected resolution, expecte d error.' );
}, },
function fail( e ) { function fail( e ) {
assert.ok( /Circular/.test( String( e ) ), 'Detec t circular dependency' ); assert.ok( /Circular/.test( String( e ) ), 'Detec t circular dependency' );
} }
) )
.always( done ); .always( done );
} ); } );
skipping to change at line 156 skipping to change at line 162
assert.expect( 0 ); assert.expect( 0 );
done(); done();
return; return;
} }
this.nativeSet = window.Set; this.nativeSet = window.Set;
window.Set = undefined; window.Set = undefined;
mw.redefineFallbacksForTest(); mw.redefineFallbacksForTest();
mw.loader.register( [ mw.loader.register( [
[ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ], [ 'test.shim.circleA', '0', [ 'test.shim.circleB' ] ],
[ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ], [ 'test.shim.circleB', '0', [ 'test.shim.circleC' ] ],
[ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ] [ 'test.shim.circleC', '0', [ 'test.shim.circleA' ] ]
] ); ] );
mw.loader.using( 'test.shim.circle3' ).then( mw.loader.using( 'test.shim.circleC' ).then(
function done() { function done() {
assert.ok( false, 'Unexpected resolution, expecte d error.' ); assert.ok( false, 'Unexpected resolution, expecte d error.' );
}, },
function fail( e ) { function fail( e ) {
assert.ok( /Circular/.test( String( e ) ), 'Detec t circular dependency' ); assert.ok( /Circular/.test( String( e ) ), 'Detec t circular dependency' );
} }
) )
.always( done ); .always( done );
} ); } );
QUnit.test( '.load() - Error: Circular dependency', function ( assert ) { QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
var capture = []; var capture = [];
mw.loader.register( [ mw.loader.register( [
[ 'test.circleA', '0', [ 'test.circleB' ] ], [ 'test.load.circleA', '0', [ 'test.load.circleB' ] ],
[ 'test.circleB', '0', [ 'test.circleC' ] ], [ 'test.load.circleB', '0', [ 'test.load.circleC' ] ],
[ 'test.circleC', '0', [ 'test.circleA' ] ] [ 'test.load.circleC', '0', [ 'test.load.circleA' ] ]
] ); ] );
this.sandbox.stub( mw, 'track', function ( topic, data ) { this.sandbox.stub( mw, 'track', function ( topic, data ) {
capture.push( { capture.push( {
topic: topic, topic: topic,
error: data.exception && data.exception.message, error: data.exception && data.exception.message,
source: data.source source: data.source
} ); } );
} ); } );
mw.loader.load( 'test.circleC' ); mw.loader.load( 'test.load.circleC' );
assert.deepEqual( assert.deepEqual(
[ { [ {
topic: 'resourceloader.exception', topic: 'resourceloader.exception',
error: 'Circular reference detected: test.circleB -> test.circleC', error: 'Circular reference detected: test.load.ci rcleB -> test.load.circleC',
source: 'resolve' source: 'resolve'
} ], } ],
capture, capture,
'Detect circular dependency' 'Detect circular dependency'
); );
} ); } );
QUnit.test( '.load() - Error: Circular dependency (direct)', function ( a
ssert ) {
var capture = [];
mw.loader.register( [
[ 'test.load.circleDirect', '0', [ 'test.load.circleDirec
t' ] ]
] );
this.sandbox.stub( mw, 'track', function ( topic, data ) {
capture.push( {
topic: topic,
error: data.exception && data.exception.message,
source: data.source
} );
} );
mw.loader.load( 'test.load.circleDirect' );
assert.deepEqual(
[ {
topic: 'resourceloader.exception',
error: 'Circular reference detected: test.load.ci
rcleDirect -> test.load.circleDirect',
source: 'resolve'
} ],
capture,
'Detect a direct self-dependency'
);
} );
QUnit.test( '.using() - Error: Unregistered', function ( assert ) { QUnit.test( '.using() - Error: Unregistered', function ( assert ) {
var done = assert.async(); var done = assert.async();
mw.loader.using( 'test.using.unreg' ).then( mw.loader.using( 'test.using.unreg' ).then(
function done() { function done() {
assert.ok( false, 'Unexpected resolution, expecte d error.' ); assert.ok( false, 'Unexpected resolution, expecte d error.' );
}, },
function fail( e ) { function fail( e ) {
assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' ); assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' );
} }
skipping to change at line 270 skipping to change at line 301
assert.notEqual( assert.notEqual(
$element.css( 'float' ), $element.css( 'float' ),
'right', 'right',
'style is clear' 'style is clear'
); );
mw.loader.implement( mw.loader.implement(
'test.implement.a', 'test.implement.a',
function () { function () {
assert.equal( assert.strictEqual(
$element.css( 'float' ), $element.css( 'float' ),
'right', 'right',
'style is applied' 'style is applied'
); );
}, },
{ {
all: '.mw-test-implement-a { float: right; }' all: '.mw-test-implement-a { float: right; }'
} }
); );
skipping to change at line 358 skipping to change at line 389
assert.notEqual( assert.notEqual(
$element.css( 'float' ), $element.css( 'float' ),
'right', 'right',
'style is clear' 'style is clear'
); );
mw.loader.implement( mw.loader.implement(
'test.implement.c', 'test.implement.c',
function () { function () {
assert.equal( assert.strictEqual(
$element.css( 'float' ), $element.css( 'float' ),
'right', 'right',
'style is applied' 'style is applied'
); );
}, },
{ {
all: '.mw-test-implement-c { float: right; }' all: '.mw-test-implement-c { float: right; }'
} }
); );
skipping to change at line 410 skipping to change at line 441
} }
); );
mw.loader.load( 'test.implement.d' ); mw.loader.load( 'test.implement.d' );
} ); } );
QUnit.test( '.implement( messages before script )', function ( assert ) { QUnit.test( '.implement( messages before script )', function ( assert ) {
mw.loader.implement( mw.loader.implement(
'test.implement.order', 'test.implement.order',
function () { function () {
assert.equal( mw.loader.getState( 'test.implement assert.strictEqual( mw.loader.getState( 'test.imp
.order' ), 'executing', 'state during script execution' ); lement.order' ), 'executing', 'state during script execution' );
assert.equal( mw.msg( 'test-foobar' ), 'Hello Foo assert.strictEqual( mw.msg( 'test-foobar' ), 'Hel
bar, $1!', 'messages load before script execution' ); lo Foobar, $1!', 'messages load before script execution' );
}, },
{}, {},
{ {
'test-foobar': 'Hello Foobar, $1!' 'test-foobar': 'Hello Foobar, $1!'
} }
); );
return mw.loader.using( 'test.implement.order' ).then( function ( ) { return mw.loader.using( 'test.implement.order' ).then( function ( ) {
assert.equal( mw.loader.getState( 'test.implement.order' ), 'ready', 'final success state' ); assert.strictEqual( mw.loader.getState( 'test.implement.o rder' ), 'ready', 'final success state' );
} ); } );
} ); } );
// @import (T33676) // @import (T33676)
QUnit.test( '.implement( styles with @import )', function ( assert ) { QUnit.test( '.implement( styles with @import )', function ( assert ) {
var $element, var $element,
done = assert.async(); done = assert.async();
mw.loader.implement( mw.loader.implement(
'test.implement.import', 'test.implement.import',
function () { function () {
$element = $( '<div class="mw-test-implement-impo rt">Foo bar</div>' ).appendTo( '#qunit-fixture' ); $element = $( '<div class="mw-test-implement-impo rt">Foo bar</div>' ).appendTo( '#qunit-fixture' );
assertStyleAsync( assert, $element, 'float', 'rig ht', function () { assertStyleAsync( assert, $element, 'float', 'rig ht', function () {
assert.equal( $element.css( 'text-align' ), 'center', assert.strictEqual( $element.css( 'text-a lign' ), 'center',
'CSS styles after the @import rul e are working' 'CSS styles after the @import rul e are working'
); );
done(); done();
} ); } );
}, },
{ {
css: [ css: [
'@import url(\'' '@import url(\''
+ urlStyleTest( '.mw-test-impleme nt-import', 'float', 'right' ) + urlStyleTest( '.mw-test-impleme nt-import', 'float', 'right' )
skipping to change at line 478 skipping to change at line 509
); );
mw.loader.register( [ mw.loader.register( [
[ 'test.implement.e', '0', [ 'test.implement.e2' ] ], [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
[ 'test.implement.e2', '0' ] [ 'test.implement.e2', '0' ]
] ); ] );
mw.loader.implement( mw.loader.implement(
'test.implement.e', 'test.implement.e',
function () { function () {
assert.equal( assert.strictEqual(
$element.css( 'float' ), $element.css( 'float' ),
'right', 'right',
'Depending module\'s style is applied' 'Depending module\'s style is applied'
); );
}, },
{ {
all: '.mw-test-implement-e { float: right; }' all: '.mw-test-implement-e { float: right; }'
} }
); );
mw.loader.implement( mw.loader.implement(
'test.implement.e2', 'test.implement.e2',
function () { function () {
assert.equal( assert.strictEqual(
$element2.css( 'float' ), $element2.css( 'float' ),
'left', 'left',
'Dependency\'s style is applied' 'Dependency\'s style is applied'
); );
}, },
{ {
all: '.mw-test-implement-e2 { float: left; }' all: '.mw-test-implement-e2 { float: left; }'
} }
); );
return mw.loader.using( 'test.implement.e' ); return mw.loader.using( 'test.implement.e' );
} ); } );
QUnit.test( '.implement( only scripts )', function ( assert ) { QUnit.test( '.implement( only scripts )', function ( assert ) {
mw.loader.implement( 'test.onlyscripts', function () {} ); mw.loader.implement( 'test.onlyscripts', function () {} );
assert.strictEqual( mw.loader.getState( 'test.onlyscripts' ), 're return mw.loader.using( 'test.onlyscripts', function () {
ady' ); assert.strictEqual( mw.loader.getState( 'test.onlyscripts
' ), 'ready' );
} );
} ); } );
QUnit.test( '.implement( only messages )', function ( assert ) { QUnit.test( '.implement( only messages )', function ( assert ) {
assert.assertFalse( mw.messages.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' ); assert.assertFalse( mw.messages.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
mw.loader.implement( 'test.implement.msgs', [], {}, { T31107: 'lo aded' } ); mw.loader.implement( 'test.implement.msgs', [], {}, { T31107: 'lo aded' } );
return mw.loader.using( 'test.implement.msgs', function () { return mw.loader.using( 'test.implement.msgs', function () {
assert.ok( mw.messages.exists( 'T31107' ), 'T31107: messa ges-only module should implement ok' ); assert.ok( mw.messages.exists( 'T31107' ), 'T31107: messa ges-only module should implement ok' );
} ); } );
} ); } );
QUnit.test( '.implement( empty )', function ( assert ) { QUnit.test( '.implement( empty )', function ( assert ) {
mw.loader.implement( 'test.empty' ); mw.loader.implement( 'test.empty' );
assert.strictEqual( mw.loader.getState( 'test.empty' ), 'ready' ) return mw.loader.using( 'test.empty', function () {
; assert.strictEqual( mw.loader.getState( 'test.empty' ), '
ready' );
} );
} );
QUnit.test( '.addSource()', function ( assert ) {
mw.loader.addSource( { testsource1: 'https://1.test/src' } );
assert.throws( function () {
mw.loader.addSource( { testsource1: 'https://1.test/src'
} );
}, /already registered/, 'duplicate pair from addSource(Object)'
);
assert.throws( function () {
mw.loader.addSource( { testsource1: 'https://1.test/src-d
iff' } );
}, /already registered/, 'duplicate ID from addSource(Object)' );
} ); } );
// @covers mw.loader#batchRequest // @covers mw.loader#batchRequest
// This is a regression test because in the past we called getCombinedVer sion() // This is a regression test because in the past we called getCombinedVer sion()
// for all requested modules, before url splitting took place. // for all requested modules, before url splitting took place.
// Discovered as part of T188076, but not directly related. // Discovered as part of T188076, but not directly related.
QUnit.test( 'Url composition (modules considered for version)', function ( assert ) { QUnit.test( 'Url composition (modules considered for version)', function ( assert ) {
mw.loader.register( [ mw.loader.register( [
// [module, version, dependencies, group, source] // [module, version, dependencies, group, source]
[ 'testUrlInc', 'url', [], null, 'testloader' ], [ 'testUrlInc', 'url', [], null, 'testloader' ],
skipping to change at line 594 skipping to change at line 641
// $hash = hash( 'fnv132', 'url12dump' ); // $hash = hash( 'fnv132', 'url12dump' );
// base_convert( $hash, 16, 36 ); // "1 1eo3in" // base_convert( $hash, 16, 36 ); // "1 1eo3in"
version: '1knqzan' version: '1knqzan'
}, },
'Query parameters' 'Query parameters'
); );
} ); } );
} ); } );
QUnit.test( 'Broken indirect dependency', function ( assert ) { QUnit.test( 'Broken indirect dependency', function ( assert ) {
// don't emit an error event this.useStubClock();
// Don't actually emit an error event
this.sandbox.stub( mw, 'track' ); this.sandbox.stub( mw, 'track' );
mw.loader.register( [ mw.loader.register( [
[ 'test.module1', '0' ], [ 'test.module1', '0' ],
[ 'test.module2', '0', [ 'test.module1' ] ], [ 'test.module2', '0', [ 'test.module1' ] ],
[ 'test.module3', '0', [ 'test.module2' ] ] [ 'test.module3', '0', [ 'test.module2' ] ]
] ); ] );
mw.loader.implement( 'test.module1', function () { mw.loader.implement( 'test.module1', function () {
throw new Error( 'expected' ); throw new Error( 'expected' );
}, {}, {} ); }, {}, {} );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error' , 'Expected "error" state for test.module1' ); assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error' , 'Expected "error" state for test.module1' );
assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error' , 'Expected "error" state for test.module2' ); assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error' , 'Expected "error" state for test.module2' );
assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error' , 'Expected "error" state for test.module3' ); assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error' , 'Expected "error" state for test.module3' );
assert.strictEqual( mw.track.callCount, 1 ); assert.strictEqual( mw.track.callCount, 1 );
} ); } );
QUnit.test( 'Out-of-order implementation', function ( assert ) { QUnit.test( 'Out-of-order implementation', function ( assert ) {
this.useStubClock();
mw.loader.register( [ mw.loader.register( [
[ 'test.module4', '0' ], [ 'test.module4', '0' ],
[ 'test.module5', '0', [ 'test.module4' ] ], [ 'test.module5', '0', [ 'test.module4' ] ],
[ 'test.module6', '0', [ 'test.module5' ] ] [ 'test.module6', '0', [ 'test.module5' ] ]
] ); ] );
mw.loader.implement( 'test.module4', function () {} ); mw.loader.implement( 'test.module4', function () {} );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready' , 'Expected "ready" state for test.module4' ); assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready' , 'Expected "ready" state for test.module4' );
assert.strictEqual( mw.loader.getState( 'test.module5' ), 'regist ered', 'Expected "registered" state for test.module5' ); assert.strictEqual( mw.loader.getState( 'test.module5' ), 'regist ered', 'Expected "registered" state for test.module5' );
assert.strictEqual( mw.loader.getState( 'test.module6' ), 'regist ered', 'Expected "registered" state for test.module6' ); assert.strictEqual( mw.loader.getState( 'test.module6' ), 'regist ered', 'Expected "registered" state for test.module6' );
mw.loader.implement( 'test.module6', function () {} ); mw.loader.implement( 'test.module6', function () {} );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready' , 'Expected "ready" state for test.module4' ); assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready' , 'Expected "ready" state for test.module4' );
assert.strictEqual( mw.loader.getState( 'test.module5' ), 'regist ered', 'Expected "registered" state for test.module5' ); assert.strictEqual( mw.loader.getState( 'test.module5' ), 'regist ered', 'Expected "registered" state for test.module5' );
assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded ', 'Expected "loaded" state for test.module6' ); assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded ', 'Expected "loaded" state for test.module6' );
mw.loader.implement( 'test.module5', function () {} ); mw.loader.implement( 'test.module5', function () {} );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready' , 'Expected "ready" state for test.module4' ); assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready' , 'Expected "ready" state for test.module4' );
assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready' , 'Expected "ready" state for test.module5' ); assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready' , 'Expected "ready" state for test.module5' );
assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready' , 'Expected "ready" state for test.module6' ); assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready' , 'Expected "ready" state for test.module6' );
} ); } );
QUnit.test( 'Missing dependency', function ( assert ) { QUnit.test( 'Missing dependency', function ( assert ) {
this.useStubClock();
mw.loader.register( [ mw.loader.register( [
[ 'test.module7', '0' ], [ 'test.module7', '0' ],
[ 'test.module8', '0', [ 'test.module7' ] ], [ 'test.module8', '0', [ 'test.module7' ] ],
[ 'test.module9', '0', [ 'test.module8' ] ] [ 'test.module9', '0', [ 'test.module8' ] ]
] ); ] );
mw.loader.implement( 'test.module8', function () {} ); mw.loader.implement( 'test.module8', function () {} );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'regist ered', 'Expected "registered" state for test.module7' ); assert.strictEqual( mw.loader.getState( 'test.module7' ), 'regist ered', 'Expected "registered" state for test.module7' );
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded ', 'Expected "loaded" state for test.module8' ); assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded ', 'Expected "loaded" state for test.module8' );
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'regist ered', 'Expected "registered" state for test.module9' ); assert.strictEqual( mw.loader.getState( 'test.module9' ), 'regist ered', 'Expected "registered" state for test.module9' );
mw.loader.state( 'test.module7', 'missing' );
mw.loader.state( { 'test.module7': 'missing' } );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missin g', 'Expected "missing" state for test.module7' ); assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missin g', 'Expected "missing" state for test.module7' );
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error' , 'Expected "error" state for test.module8' ); assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error' , 'Expected "error" state for test.module8' );
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error' , 'Expected "error" state for test.module9' ); assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error' , 'Expected "error" state for test.module9' );
mw.loader.implement( 'test.module9', function () {} ); mw.loader.implement( 'test.module9', function () {} );
this.tick();
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missin g', 'Expected "missing" state for test.module7' ); assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missin g', 'Expected "missing" state for test.module7' );
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error' , 'Expected "error" state for test.module8' ); assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error' , 'Expected "error" state for test.module8' );
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error' , 'Expected "error" state for test.module9' ); assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error' , 'Expected "error" state for test.module9' );
mw.loader.using(
[ 'test.module7' ], // Restore clock for QUnit and $.Deferred internals
this.clock.restore();
return mw.loader.using( [ 'test.module7' ] ).then(
function () { function () {
assert.ok( false, 'Success fired despite missing throw new Error( 'Success fired despite missing d
dependency' ); ependency' );
assert.ok( true, 'QUnit expected() count dummy' )
;
}, },
function ( e, dependencies ) { function ( e, dependencies ) {
assert.strictEqual( Array.isArray( dependencies ) , true, 'Expected array of dependencies' ); assert.strictEqual( Array.isArray( dependencies ) , true, 'Expected array of dependencies' );
assert.deepEqual( dependencies, [ 'test.module7' assert.deepEqual(
], 'Error callback called with module test.module7' ); dependencies,
[ 'jquery', 'mediawiki.base', 'test.modul
e7' ],
'Error callback called with module test.m
odule7'
);
} }
); ).then( function () {
mw.loader.using( return mw.loader.using( [ 'test.module9' ] );
[ 'test.module9' ], } ).then(
function () { function () {
assert.ok( false, 'Success fired despite missing throw new Error( 'Success fired despite missing d
dependency' ); ependency' );
assert.ok( true, 'QUnit expected() count dummy' )
;
}, },
function ( e, dependencies ) { function ( e, dependencies ) {
assert.strictEqual( Array.isArray( dependencies ) , true, 'Expected array of dependencies' ); assert.strictEqual( Array.isArray( dependencies ) , true, 'Expected array of dependencies' );
dependencies.sort(); dependencies.sort();
assert.deepEqual( assert.deepEqual(
dependencies, dependencies,
[ 'test.module7', 'test.module8', 'test.m odule9' ], [ 'jquery', 'mediawiki.base', 'test.modul e7', 'test.module8', 'test.module9' ],
'Error callback called with all three mod ules as dependencies' 'Error callback called with all three mod ules as dependencies'
); );
} }
); );
} ); } );
QUnit.test( 'Dependency handling', function ( assert ) { QUnit.test( 'Dependency handling', function ( assert ) {
var done = assert.async();
mw.loader.register( [ mw.loader.register( [
// [module, version, dependencies, group, source] // [module, version, dependencies, group, source]
[ 'testMissing', '1', [], null, 'testloader' ], [ 'testMissing', '1', [], null, 'testloader' ],
[ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testl oader' ], [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testl oader' ],
[ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], nu ll, 'testloader' ] [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], nu ll, 'testloader' ]
] ); ] );
function verifyModuleStates() { function verifyModuleStates() {
assert.equal( mw.loader.getState( 'testMissing' ), 'missi assert.strictEqual( mw.loader.getState( 'testMissing' ),
ng', 'Module "testMissing" state' ); 'missing', 'Module "testMissing" state' );
assert.equal( mw.loader.getState( 'testUsesMissing' ), 'e assert.strictEqual( mw.loader.getState( 'testUsesMissing'
rror', 'Module "testUsesMissing" state' ); ), 'error', 'Module "testUsesMissing" state' );
assert.equal( mw.loader.getState( 'testUsesNestedMissing' assert.strictEqual( mw.loader.getState( 'testUsesNestedMi
), 'error', 'Module "testUsesNestedMissing" state' ); ssing' ), 'error', 'Module "testUsesNestedMissing" state' );
} }
mw.loader.using( [ 'testUsesNestedMissing' ], return mw.loader.using( [ 'testUsesNestedMissing' ] ).then(
function () { function () {
assert.ok( false, 'Error handler should be invoke
d.' );
assert.ok( true ); // Dummy to reach QUnit expect
()
verifyModuleStates(); verifyModuleStates();
throw new Error( 'Error handler should be invoked
done(); .' );
}, },
function ( e, badmodules ) { function ( e, modules ) {
assert.ok( true, 'Error handler should be invoked // When the server sets state of 'testMissing' to
.' ); 'missing'
// As soon as server spits out state('testMissing // it should bubble up and trigger the error call
', 'missing'); back of the job for 'testUsesNestedMissing'.
// it will bubble up and trigger the error callba assert.strictEqual( modules.indexOf( 'testMissing
ck. ' ) !== -1, true, 'Triggered by testMissing.' );
// Therefor the badmodules array is not testUsesM
issing or testUsesNestedMissing.
assert.deepEqual( badmodules, [ 'testMissing' ],
'Bad modules as expected.' );
verifyModuleStates(); verifyModuleStates();
done();
} }
); );
} ); } );
QUnit.test( 'Skip-function handling', function ( assert ) { QUnit.test( 'Skip-function handling', function ( assert ) {
mw.loader.register( [ mw.loader.register( [
// [module, version, dependencies, group, source, skip] // [module, version, dependencies, group, source, skip]
[ 'testSkipped', '1', [], null, 'testloader', 'return tru e;' ], [ 'testSkipped', '1', [], null, 'testloader', 'return tru e;' ],
[ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ], [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
[ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkip ped' ], null, 'testloader' ] [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkip ped' ], null, 'testloader' ]
] ); ] );
return mw.loader.using( [ 'testUsesSkippable' ] ).then( return mw.loader.using( [ 'testUsesSkippable' ] ).then(
function () { function () {
assert.equal( mw.loader.getState( 'testSkipped' ) assert.strictEqual( mw.loader.getState( 'testSkip
, 'ready', 'Skipped module' ); ped' ), 'ready', 'Skipped module' );
assert.equal( mw.loader.getState( 'testNotSkipped assert.strictEqual( mw.loader.getState( 'testNotS
' ), 'ready', 'Regular module' ); kipped' ), 'ready', 'Regular module' );
assert.equal( mw.loader.getState( 'testUsesSkippa assert.strictEqual( mw.loader.getState( 'testUses
ble' ), 'ready', 'Regular module with skippable dependency' ); Skippable' ), 'ready', 'Regular module with skippable dependency' );
}, },
function ( e, badmodules ) { function ( e, badmodules ) {
// Should not fail and QUnit would already catch this, // Should not fail and QUnit would already catch this,
// but add a handler anyway to report details fro m 'badmodules // but add a handler anyway to report details fro m 'badmodules
assert.deepEqual( badmodules, [], 'Bad modules' ) ; assert.deepEqual( badmodules, [], 'Bad modules' ) ;
} }
); );
} ); } );
// This bug was actually already fixed in 1.18 and later when discovered in 1.17. // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
QUnit.test( '.load( "//protocol-relative" ) - T32825', function ( assert ) { QUnit.test( '.load( "//protocol-relative" ) - T32825', function ( assert ) {
var target, var target,
done = assert.async(); done = assert.async();
// URL to the callback script // URL to the callback script
target = QUnit.fixurl( target = QUnit.fixurl(
mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPat h' ) + '/tests/qunit/data/mwLoaderTestCallback.js' mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPat h' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
); );
// Ensure a protocol-relative URL for this test // Ensure a protocol-relative URL for this test
target = target.replace( /https?:/, '' ); target = target.replace( /https?:/, '' );
assert.equal( target.slice( 0, 2 ), '//', 'URL is protocol-relati ve' ); assert.strictEqual( target.slice( 0, 2 ), '//', 'URL is protocol- relative' );
mw.loader.testCallback = function () { mw.loader.testCallback = function () {
// Ensure once, delete now // Ensure once, delete now
delete mw.loader.testCallback; delete mw.loader.testCallback;
assert.ok( true, 'callback' ); assert.ok( true, 'callback' );
done(); done();
}; };
// Go! // Go!
mw.loader.load( target ); mw.loader.load( target );
} ); } );
QUnit.test( '.load( "/absolute-path" )', function ( assert ) { QUnit.test( '.load( "/absolute-path" )', function ( assert ) {
var target, var target,
done = assert.async(); done = assert.async();
// URL to the callback script // URL to the callback script
target = QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/ qunit/data/mwLoaderTestCallback.js' ); target = QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/ qunit/data/mwLoaderTestCallback.js' );
assert.equal( target.slice( 0, 1 ), '/', 'URL is relative to docu ment root' ); assert.strictEqual( target.slice( 0, 1 ), '/', 'URL is relative t o document root' );
mw.loader.testCallback = function () { mw.loader.testCallback = function () {
// Ensure once, delete now // Ensure once, delete now
delete mw.loader.testCallback; delete mw.loader.testCallback;
assert.ok( true, 'callback' ); assert.ok( true, 'callback' );
done(); done();
}; };
// Go! // Go!
mw.loader.load( target ); mw.loader.load( target );
skipping to change at line 828 skipping to change at line 890
mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] ); mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
return mw.loader.using( 'testRaceLoadMe', function () { return mw.loader.using( 'testRaceLoadMe', function () {
assert.strictEqual( done, true, 'script ran' ); assert.strictEqual( done, true, 'script ran' );
assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' ); assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' );
} ); } );
} ); } );
QUnit.test( 'Stale response caching - T117587', function ( assert ) { QUnit.test( 'Stale response caching - T117587', function ( assert ) {
var count = 0; var count = 0;
mw.loader.store.enabled = true; // Enable store and stub timeout/idle scheduling
this.sandbox.stub( mw.loader.store, 'enabled', true );
this.sandbox.stub( window, 'setTimeout', function ( fn ) {
fn();
} );
this.sandbox.stub( mw, 'requestIdleCallback', function ( fn ) {
fn();
} );
mw.loader.register( 'test.stale', 'v2' ); mw.loader.register( 'test.stale', 'v2' );
assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, ' Not in store' ); assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, ' Not in store' );
mw.loader.implement( 'test.stale@v1', function () { mw.loader.implement( 'test.stale@v1', function () {
count++; count++;
} ); } );
return mw.loader.using( 'test.stale' ) return mw.loader.using( 'test.stale' )
.then( function () { .then( function () {
assert.strictEqual( count, 1 ); assert.strictEqual( count, 1 );
// After implementing, registry contains version as implemented by the response. // After implementing, registry contains version as implemented by the response.
assert.strictEqual( mw.loader.getVersion( 'test.s tale' ), 'v1', 'Override version' ); assert.strictEqual( mw.loader.getVersion( 'test.s tale' ), 'v1', 'Override version' );
assert.strictEqual( mw.loader.getState( 'test.sta le' ), 'ready' ); assert.strictEqual( mw.loader.getState( 'test.sta le' ), 'ready' );
assert.ok( mw.loader.store.get( 'test.stale' ), ' In store' ); assert.strictEqual( typeof mw.loader.store.get( ' test.stale' ), 'string', 'In store' );
} ) } )
.then( function () { .then( function () {
// Reset run time, but keep mw.loader.store // Reset run time, but keep mw.loader.store
mw.loader.moduleRegistry[ 'test.stale' ].script = undefined; mw.loader.moduleRegistry[ 'test.stale' ].script = undefined;
mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered'; mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered';
mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2'; mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2';
// Module was stored correctly as v1 // Module was stored correctly as v1
// On future navigations, it will be ignored unti l evicted // On future navigations, it will be ignored unti l evicted
assert.strictEqual( mw.loader.store.get( 'test.st ale' ), false, 'Not in store' ); assert.strictEqual( mw.loader.store.get( 'test.st ale' ), false, 'Not in store' );
} ); } );
} ); } );
QUnit.test( 'Stale response caching - backcompat', function ( assert ) { QUnit.test( 'Stale response caching - backcompat', function ( assert ) {
var script = 0; var script = 0;
mw.loader.store.enabled = true; // Enable store and stub timeout/idle scheduling
this.sandbox.stub( mw.loader.store, 'enabled', true );
this.sandbox.stub( window, 'setTimeout', function ( fn ) {
fn();
} );
this.sandbox.stub( mw, 'requestIdleCallback', function ( fn ) {
fn();
} );
mw.loader.register( 'test.stalebc', 'v2' ); mw.loader.register( 'test.stalebc', 'v2' );
assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' ); assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' );
mw.loader.implement( 'test.stalebc', function () { mw.loader.implement( 'test.stalebc', function () {
script++; script++;
} ); } );
return mw.loader.using( 'test.stalebc' ) return mw.loader.using( 'test.stalebc' )
.then( function () { .then( function () {
assert.strictEqual( script, 1, 'module script ran ' ); assert.strictEqual( script, 1, 'module script ran ' );
assert.strictEqual( mw.loader.getState( 'test.sta lebc' ), 'ready' ); assert.strictEqual( mw.loader.getState( 'test.sta lebc' ), 'ready' );
assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' ); assert.strictEqual( typeof mw.loader.store.get( ' test.stalebc' ), 'string', 'In store' );
} ) } )
.then( function () { .then( function () {
// Reset run time, but keep mw.loader.store // Reset run time, but keep mw.loader.store
mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined; mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined;
mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered'; mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered';
mw.loader.moduleRegistry[ 'test.stalebc' ].versio n = 'v2'; mw.loader.moduleRegistry[ 'test.stalebc' ].versio n = 'v2';
// Legacy behaviour is storing under the expected version, // Legacy behaviour is storing under the expected version,
// which woudl lead to whitewashing and stale val ues (T117587). // which woudl lead to whitewashing and stale val ues (T117587).
assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' ); assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
skipping to change at line 945 skipping to change at line 1023
mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] ); mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
return mw.loader.using( 'test.require.callback' ).then( function ( require ) { return mw.loader.using( 'test.require.callback' ).then( function ( require ) {
var cb = require( 'test.require.callback' ); var cb = require( 'test.require.callback' );
assert.strictEqual( cb.immediate, 'Defined.', 'module.exp orts and require work in debug mode' ); assert.strictEqual( cb.immediate, 'Defined.', 'module.exp orts and require work in debug mode' );
// Must use try-catch because cb.later() will throw if re quire is undefined, // Must use try-catch because cb.later() will throw if re quire is undefined,
// which doesn't work well inside Deferred.then() when us ing jQuery 1.x with QUnit // which doesn't work well inside Deferred.then() when us ing jQuery 1.x with QUnit
try { try {
assert.strictEqual( cb.later(), 'Defined.', 'requ ire works asynchrously in debug mode' ); assert.strictEqual( cb.later(), 'Defined.', 'requ ire works asynchrously in debug mode' );
} catch ( e ) { } catch ( e ) {
assert.equal( null, String( e ), 'require works a synchrously in debug mode' ); assert.strictEqual( String( e ), null, 'require w orks asynchrously in debug mode' );
} }
} ); } );
} ); } );
QUnit.test( 'Implicit dependencies', function ( assert ) { QUnit.test( 'Implicit dependencies', function ( assert ) {
var user = 0, var user = 0,
site = 0, site = 0,
siteFromUser = 0; siteFromUser = 0;
mw.loader.implement( mw.loader.implement(
skipping to change at line 980 skipping to change at line 1058
assert.strictEqual( site, 1, 'site module' ); assert.strictEqual( site, 1, 'site module' );
assert.strictEqual( user, 1, 'user module' ); assert.strictEqual( user, 1, 'user module' );
assert.strictEqual( siteFromUser, 1, 'site ran before use r' ); assert.strictEqual( siteFromUser, 1, 'site ran before use r' );
} ).always( function () { } ).always( function () {
// Reset // Reset
mw.loader.moduleRegistry[ 'site' ].state = 'registered'; mw.loader.moduleRegistry[ 'site' ].state = 'registered';
mw.loader.moduleRegistry[ 'user' ].state = 'registered'; mw.loader.moduleRegistry[ 'user' ].state = 'registered';
} ); } );
} ); } );
}( mediaWiki, jQuery ) ); }() );
 End of changes. 62 change blocks. 
97 lines changed or deleted 178 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)