| 1 |
( function () { |
| 2 |
QUnit.module( 'mw.loader', QUnit.newMwEnvironment( { |
| 3 |
beforeEach: function ( assert ) { |
| 4 |
|
| 5 |
mw.loader.testFail = function ( reason ) { |
| 6 |
assert.true( false, reason ); |
| 7 |
}; |
| 8 |
|
| 9 |
this.resetStore = false; |
| 10 |
this.stubStore = function () { |
| 11 |
this.resetStore = true; |
| 12 |
mw.loader.store.items = {}; |
| 13 |
|
| 14 |
mw.loader.store.enabled = false; |
| 15 |
}; |
| 16 |
|
| 17 |
this.useStubClock = function () { |
| 18 |
this.clock = this.sandbox.useFakeTimers(); |
| 19 |
this.tick = function ( forward ) { |
| 20 |
return this.clock.tick( forward || 1 ); |
| 21 |
}; |
| 22 |
this.sandbox.stub( mw, 'requestIdleCallback', mw.requestIdleCallbackInternal ); |
| 23 |
}; |
| 24 |
}, |
| 25 |
afterEach: function () { |
| 26 |
mw.loader.maxQueryLength = 2000; |
| 27 |
if ( this.resetStore ) { |
| 28 |
mw.loader.store.enabled = null; |
| 29 |
mw.loader.store.items = {}; |
| 30 |
localStorage.removeItem( mw.loader.store.key ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
delete mw.loader.testCallback; |
| 35 |
delete mw.loader.testFail; |
| 36 |
delete mw.getScriptExampleScriptLoaded; |
| 37 |
} |
| 38 |
} ) ); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
const SCRIPT_PATH_URL = new URL( |
| 46 |
mw.config.get( 'wgScriptPath' ) + '/', |
| 47 |
mw.config.get( 'wgServer' ) |
| 48 |
).toString(); |
| 49 |
|
| 50 |
mw.loader.addSource( { |
| 51 |
testloader: SCRIPT_PATH_URL + 'tests/qunit/data/load.mock.php' |
| 52 |
} ); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
function assertStyleAsync( assert, $element, prop, val, fn ) { |
| 73 |
let styleTestStart = null; |
| 74 |
const el = $element.get( 0 ), |
| 75 |
styleTestTimeout = ( QUnit.config.testTimeout || 5000 ) - 200; |
| 76 |
|
| 77 |
function isCssImportApplied() { |
| 78 |
|
| 79 |
$element.css( 'height' ); |
| 80 |
|
| 81 |
el.innerHTML; |
| 82 |
|
| 83 |
el.className = el.className; |
| 84 |
|
| 85 |
document.documentElement.clientHeight; |
| 86 |
|
| 87 |
return $element.css( prop ) === val; |
| 88 |
} |
| 89 |
|
| 90 |
function styleTestLoop() { |
| 91 |
const styleTestSince = Date.now() - styleTestStart; |
| 92 |
|
| 93 |
if ( isCssImportApplied() || styleTestSince > styleTestTimeout ) { |
| 94 |
assert.strictEqual( $element.css( prop ), val, |
| 95 |
'style "' + prop + ': ' + val + '" from url is applied (after ' + styleTestSince + 'ms)' |
| 96 |
); |
| 97 |
|
| 98 |
if ( fn ) { |
| 99 |
fn(); |
| 100 |
} |
| 101 |
|
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
setTimeout( styleTestLoop ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
styleTestStart = Date.now(); |
| 110 |
styleTestLoop(); |
| 111 |
} |
| 112 |
|
| 113 |
function urlStyleTest( selector, prop, val ) { |
| 114 |
return SCRIPT_PATH_URL + 'tests/qunit/data/styleTest.css.php?' + |
| 115 |
$.param( { |
| 116 |
selector: selector, |
| 117 |
prop: prop, |
| 118 |
val: val |
| 119 |
} ); |
| 120 |
} |
| 121 |
|
| 122 |
QUnit.test( '.using( .., Function callback ) Promise', ( assert ) => { |
| 123 |
let script = 0, callback = 0; |
| 124 |
mw.loader.testCallback = function () { |
| 125 |
script++; |
| 126 |
}; |
| 127 |
mw.loader.implement( 'test.promise', [ SCRIPT_PATH_URL + 'tests/qunit/data/mwLoaderTestCallback.js' ] ); |
| 128 |
|
| 129 |
return mw.loader.using( 'test.promise', () => { |
| 130 |
callback++; |
| 131 |
} ).then( () => { |
| 132 |
assert.strictEqual( script, 1, 'module script ran' ); |
| 133 |
assert.strictEqual( callback, 1, 'using() callback ran' ); |
| 134 |
} ); |
| 135 |
} ); |
| 136 |
|
| 137 |
QUnit.test( 'Prototype method as module name', ( assert ) => { |
| 138 |
let call = 0; |
| 139 |
mw.loader.testCallback = function () { |
| 140 |
call++; |
| 141 |
}; |
| 142 |
mw.loader.implement( 'hasOwnProperty', [ SCRIPT_PATH_URL + 'tests/qunit/data/mwLoaderTestCallback.js' ], {}, {} ); |
| 143 |
|
| 144 |
return mw.loader.using( 'hasOwnProperty', () => { |
| 145 |
assert.strictEqual( call, 1, 'module script ran' ); |
| 146 |
} ); |
| 147 |
} ); |
| 148 |
|
| 149 |
|
| 150 |
QUnit.test( '.using() - Error: Circular dependency [Set]', ( assert ) => { |
| 151 |
const done = assert.async(); |
| 152 |
|
| 153 |
mw.loader.register( [ |
| 154 |
[ 'test.set.circleA', '0', [ 'test.set.circleB' ] ], |
| 155 |
[ 'test.set.circleB', '0', [ 'test.set.circleC' ] ], |
| 156 |
[ 'test.set.circleC', '0', [ 'test.set.circleA' ] ] |
| 157 |
] ); |
| 158 |
mw.loader.using( 'test.set.circleC' ).then( |
| 159 |
() => { |
| 160 |
assert.true( false, 'Unexpected resolution, expected error.' ); |
| 161 |
}, |
| 162 |
( e ) => { |
| 163 |
assert.true( /Circular/.test( String( e ) ), 'Detect circular dependency' ); |
| 164 |
} |
| 165 |
) |
| 166 |
.always( done ); |
| 167 |
} ); |
| 168 |
|
| 169 |
QUnit.test( '.load() - Error: Circular dependency', function ( assert ) { |
| 170 |
const capture = []; |
| 171 |
mw.loader.register( [ |
| 172 |
[ 'test.load.circleA', '0', [ 'test.load.circleB' ] ], |
| 173 |
[ 'test.load.circleB', '0', [ 'test.load.circleC' ] ], |
| 174 |
[ 'test.load.circleC', '0', [ 'test.load.circleA' ] ] |
| 175 |
] ); |
| 176 |
this.sandbox.stub( mw, 'trackError', ( data ) => { |
| 177 |
capture.push( { |
| 178 |
error: data.exception && data.exception.message, |
| 179 |
source: data.source |
| 180 |
} ); |
| 181 |
} ); |
| 182 |
|
| 183 |
mw.loader.load( 'test.load.circleC' ); |
| 184 |
assert.deepEqual( |
| 185 |
capture, |
| 186 |
[ { |
| 187 |
error: 'Circular reference detected: test.load.circleB -> test.load.circleC', |
| 188 |
source: 'resolve' |
| 189 |
} ], |
| 190 |
'Detect circular dependency' |
| 191 |
); |
| 192 |
} ); |
| 193 |
|
| 194 |
QUnit.test( '.load() - Error: Circular dependency (direct)', function ( assert ) { |
| 195 |
const capture = []; |
| 196 |
mw.loader.register( [ |
| 197 |
[ 'test.load.circleDirect', '0', [ 'test.load.circleDirect' ] ] |
| 198 |
] ); |
| 199 |
this.sandbox.stub( mw, 'trackError', ( data ) => { |
| 200 |
capture.push( { |
| 201 |
error: data.exception && data.exception.message, |
| 202 |
source: data.source |
| 203 |
} ); |
| 204 |
} ); |
| 205 |
|
| 206 |
mw.loader.load( 'test.load.circleDirect' ); |
| 207 |
assert.deepEqual( |
| 208 |
capture, |
| 209 |
[ { |
| 210 |
error: 'Circular reference detected: test.load.circleDirect -> test.load.circleDirect', |
| 211 |
source: 'resolve' |
| 212 |
} ], |
| 213 |
'Detect a direct self-dependency' |
| 214 |
); |
| 215 |
} ); |
| 216 |
|
| 217 |
QUnit.test( '.using() - Error: Unregistered', ( assert ) => { |
| 218 |
const done = assert.async(); |
| 219 |
|
| 220 |
mw.loader.using( 'test.using.unreg' ).then( |
| 221 |
() => { |
| 222 |
assert.true( false, 'Unexpected resolution, expected error.' ); |
| 223 |
}, |
| 224 |
( e ) => { |
| 225 |
assert.true( /Unknown/.test( String( e ) ), 'Detect unknown dependency' ); |
| 226 |
} |
| 227 |
).always( done ); |
| 228 |
} ); |
| 229 |
|
| 230 |
QUnit.test( '.load() - Error: Unregistered', function ( assert ) { |
| 231 |
const capture = []; |
| 232 |
this.sandbox.stub( mw.log, 'warn', ( str ) => { |
| 233 |
capture.push( str ); |
| 234 |
} ); |
| 235 |
|
| 236 |
mw.loader.load( 'test.load.unreg' ); |
| 237 |
assert.deepEqual( capture, [ 'Skipped unavailable module test.load.unreg' ] ); |
| 238 |
} ); |
| 239 |
|
| 240 |
|
| 241 |
QUnit.test( '.load() - Error: Missing dependency', function ( assert ) { |
| 242 |
const capture = []; |
| 243 |
this.sandbox.stub( mw, 'trackError', ( data ) => { |
| 244 |
capture.push( { |
| 245 |
error: data.exception && data.exception.message, |
| 246 |
source: data.source |
| 247 |
} ); |
| 248 |
} ); |
| 249 |
|
| 250 |
mw.loader.register( [ |
| 251 |
[ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ], |
| 252 |
[ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ] |
| 253 |
] ); |
| 254 |
mw.loader.load( 'test.load.missingdep' ); |
| 255 |
assert.deepEqual( |
| 256 |
capture, |
| 257 |
[ { |
| 258 |
error: 'Unknown module: test.load.missingdep2', |
| 259 |
source: 'resolve' |
| 260 |
} ] |
| 261 |
); |
| 262 |
} ); |
| 263 |
|
| 264 |
QUnit.test( '.implement( styles={ "css": [text, ..] } )', ( assert ) => { |
| 265 |
const $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' ); |
| 266 |
|
| 267 |
assert.notStrictEqual( |
| 268 |
$element.css( 'float' ), |
| 269 |
'right', |
| 270 |
'style is clear' |
| 271 |
); |
| 272 |
|
| 273 |
mw.loader.implement( |
| 274 |
'test.implement.a', |
| 275 |
() => { |
| 276 |
assert.strictEqual( |
| 277 |
$element.css( 'float' ), |
| 278 |
'right', |
| 279 |
'style is applied' |
| 280 |
); |
| 281 |
}, |
| 282 |
{ |
| 283 |
css: [ '.mw-test-implement-a { float: right; }' ] |
| 284 |
} |
| 285 |
); |
| 286 |
|
| 287 |
return mw.loader.using( 'test.implement.a' ); |
| 288 |
} ); |
| 289 |
|
| 290 |
QUnit.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', ( assert ) => { |
| 291 |
const $element1 = $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ), |
| 292 |
$element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ), |
| 293 |
$element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ), |
| 294 |
done = assert.async(); |
| 295 |
|
| 296 |
assert.notStrictEqual( |
| 297 |
$element1.css( 'text-align' ), |
| 298 |
'center', |
| 299 |
'style is clear' |
| 300 |
); |
| 301 |
assert.notStrictEqual( |
| 302 |
$element2.css( 'float' ), |
| 303 |
'left', |
| 304 |
'style is clear' |
| 305 |
); |
| 306 |
assert.notStrictEqual( |
| 307 |
$element3.css( 'text-align' ), |
| 308 |
'right', |
| 309 |
'style is clear' |
| 310 |
); |
| 311 |
|
| 312 |
mw.loader.implement( |
| 313 |
'test.implement.b', |
| 314 |
() => { |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
let pending = 2; |
| 319 |
assertStyleAsync( assert, $element2, 'float', 'left', () => { |
| 320 |
assert.notStrictEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' ); |
| 321 |
|
| 322 |
pending--; |
| 323 |
if ( pending === 0 ) { |
| 324 |
done(); |
| 325 |
} |
| 326 |
} ); |
| 327 |
assertStyleAsync( assert, $element3, 'float', 'right', () => { |
| 328 |
assert.notStrictEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' ); |
| 329 |
|
| 330 |
pending--; |
| 331 |
if ( pending === 0 ) { |
| 332 |
done(); |
| 333 |
} |
| 334 |
} ); |
| 335 |
}, |
| 336 |
{ |
| 337 |
url: { |
| 338 |
print: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ], |
| 339 |
screen: [ |
| 340 |
|
| 341 |
urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ), |
| 342 |
urlStyleTest( '.mw-test-implement-b3', 'float', 'right' ) |
| 343 |
] |
| 344 |
} |
| 345 |
} |
| 346 |
); |
| 347 |
|
| 348 |
mw.loader.load( 'test.implement.b' ); |
| 349 |
} ); |
| 350 |
|
| 351 |
QUnit.test( '.implement( messages before script )', ( assert ) => { |
| 352 |
mw.loader.implement( |
| 353 |
'test.implement.order', |
| 354 |
() => { |
| 355 |
assert.strictEqual( mw.loader.getState( 'test.implement.order' ), 'executing', 'state during script execution' ); |
| 356 |
assert.strictEqual( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' ); |
| 357 |
}, |
| 358 |
{}, |
| 359 |
{ |
| 360 |
'test-foobar': 'Hello Foobar, $1!' |
| 361 |
} |
| 362 |
); |
| 363 |
|
| 364 |
return mw.loader.using( 'test.implement.order' ).then( () => { |
| 365 |
assert.strictEqual( mw.loader.getState( 'test.implement.order' ), 'ready', 'final success state' ); |
| 366 |
} ); |
| 367 |
} ); |
| 368 |
|
| 369 |
|
| 370 |
QUnit.test( '.implement( styles with @import )', ( assert ) => { |
| 371 |
let $element; |
| 372 |
const done = assert.async(); |
| 373 |
|
| 374 |
mw.loader.implement( |
| 375 |
'test.implement.import', |
| 376 |
() => { |
| 377 |
$element = $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' ); |
| 378 |
|
| 379 |
assertStyleAsync( assert, $element, 'float', 'right', () => { |
| 380 |
assert.strictEqual( $element.css( 'text-align' ), 'center', |
| 381 |
'CSS styles after the @import rule are working' |
| 382 |
); |
| 383 |
|
| 384 |
done(); |
| 385 |
} ); |
| 386 |
}, |
| 387 |
{ |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
css: [ |
| 393 |
'.something-else-first {}', |
| 394 |
'@import url(\'' + |
| 395 |
urlStyleTest( '.mw-test-implement-import', 'float', 'right' ) + |
| 396 |
'\');\n' + |
| 397 |
'.mw-test-implement-import { text-align: center; }' |
| 398 |
] |
| 399 |
} |
| 400 |
); |
| 401 |
|
| 402 |
return mw.loader.using( 'test.implement.import' ); |
| 403 |
} ); |
| 404 |
|
| 405 |
QUnit.test( '.implement( dependency with styles )', ( assert ) => { |
| 406 |
const $element = $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ), |
| 407 |
$element2 = $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' ); |
| 408 |
|
| 409 |
assert.notStrictEqual( |
| 410 |
$element.css( 'float' ), |
| 411 |
'right', |
| 412 |
'style is clear' |
| 413 |
); |
| 414 |
assert.notStrictEqual( |
| 415 |
$element2.css( 'float' ), |
| 416 |
'left', |
| 417 |
'style is clear' |
| 418 |
); |
| 419 |
|
| 420 |
mw.loader.register( [ |
| 421 |
[ 'test.implement.e', '0', [ 'test.implement.e2' ] ], |
| 422 |
[ 'test.implement.e2', '0' ] |
| 423 |
] ); |
| 424 |
|
| 425 |
mw.loader.implement( |
| 426 |
'test.implement.e', |
| 427 |
() => { |
| 428 |
assert.strictEqual( |
| 429 |
$element.css( 'float' ), |
| 430 |
'right', |
| 431 |
'Depending module\'s style is applied' |
| 432 |
); |
| 433 |
}, |
| 434 |
{ |
| 435 |
css: [ '.mw-test-implement-e { float: right; }' ] |
| 436 |
} |
| 437 |
); |
| 438 |
|
| 439 |
mw.loader.implement( |
| 440 |
'test.implement.e2', |
| 441 |
() => { |
| 442 |
assert.strictEqual( |
| 443 |
$element2.css( 'float' ), |
| 444 |
'left', |
| 445 |
'Dependency\'s style is applied' |
| 446 |
); |
| 447 |
}, |
| 448 |
{ |
| 449 |
css: [ '.mw-test-implement-e2 { float: left; }' ] |
| 450 |
} |
| 451 |
); |
| 452 |
|
| 453 |
return mw.loader.using( 'test.implement.e' ); |
| 454 |
} ); |
| 455 |
|
| 456 |
QUnit.test( '.implement( only scripts )', ( assert ) => { |
| 457 |
mw.loader.implement( 'test.onlyscripts', () => {} ); |
| 458 |
return mw.loader.using( 'test.onlyscripts', () => { |
| 459 |
assert.strictEqual( mw.loader.getState( 'test.onlyscripts' ), 'ready' ); |
| 460 |
} ); |
| 461 |
} ); |
| 462 |
|
| 463 |
QUnit.test( '.implement( only messages )', ( assert ) => { |
| 464 |
assert.false( mw.messages.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' ); |
| 465 |
|
| 466 |
mw.loader.implement( 'test.implement.msgs', [], {}, { T31107: 'loaded' } ); |
| 467 |
|
| 468 |
return mw.loader.using( 'test.implement.msgs', () => { |
| 469 |
assert.true( mw.messages.exists( 'T31107' ), 'T31107: messages-only module should implement ok' ); |
| 470 |
} ); |
| 471 |
} ); |
| 472 |
|
| 473 |
QUnit.test( '.implement( empty )', ( assert ) => { |
| 474 |
mw.loader.implement( 'test.empty' ); |
| 475 |
return mw.loader.using( 'test.empty', () => { |
| 476 |
assert.strictEqual( mw.loader.getState( 'test.empty' ), 'ready' ); |
| 477 |
} ); |
| 478 |
} ); |
| 479 |
|
| 480 |
QUnit.test( '.implement() [packageFiles long paths]', ( assert ) => { |
| 481 |
const done = assert.async(); |
| 482 |
let initJsRan = false, |
| 483 |
counter = 41; |
| 484 |
mw.loader.implement( |
| 485 |
'test.implement.packageFiles', |
| 486 |
{ |
| 487 |
main: 'resources/src/foo/init.js', |
| 488 |
files: { |
| 489 |
'resources/src/foo/data/hello.json': { hello: 'world' }, |
| 490 |
'resources/src/foo/foo.js': function ( require, module ) { |
| 491 |
counter++; |
| 492 |
module.exports = { answer: counter }; |
| 493 |
}, |
| 494 |
'resources/src/bar/bar.js': function ( require, module ) { |
| 495 |
const core = require( './core.js' ); |
| 496 |
module.exports = { data: core.sayHello( 'Alice' ) }; |
| 497 |
}, |
| 498 |
'resources/src/bar/core.js': function ( require, module ) { |
| 499 |
module.exports = { sayHello: function ( name ) { |
| 500 |
return 'Hello ' + name; |
| 501 |
} }; |
| 502 |
}, |
| 503 |
'resources/src/foo/init.js': function ( require ) { |
| 504 |
initJsRan = true; |
| 505 |
assert.deepEqual( require( './data/hello.json' ), { hello: 'world' }, 'require() with .json file' ); |
| 506 |
assert.deepEqual( require( './foo.js' ), { answer: 42 }, 'require() with .js file in same directory' ); |
| 507 |
assert.deepEqual( require( '../bar/bar.js' ), { data: 'Hello Alice' }, 'require() with ../ of a file that uses same-directory require()' ); |
| 508 |
assert.deepEqual( require( './foo.js' ), { answer: 42 }, 'require()ing the same script twice only runs it once' ); |
| 509 |
} |
| 510 |
} |
| 511 |
}, |
| 512 |
{}, |
| 513 |
{}, |
| 514 |
{} |
| 515 |
); |
| 516 |
mw.loader.using( 'test.implement.packageFiles' ).done( () => { |
| 517 |
assert.true( initJsRan, 'main JS file is executed' ); |
| 518 |
done(); |
| 519 |
} ); |
| 520 |
} ); |
| 521 |
|
| 522 |
QUnit.test( '.implement() [packageFiles with parent files]', ( assert ) => { |
| 523 |
const done = assert.async(); |
| 524 |
let initJsRan = false; |
| 525 |
let counter = 41; |
| 526 |
mw.loader.implement( |
| 527 |
'test.implement.packageWithParentFiles', |
| 528 |
{ |
| 529 |
main: 'init.js', |
| 530 |
files: { |
| 531 |
'data/hello.json': { hello: 'world' }, |
| 532 |
'foo.js': function ( require, module ) { |
| 533 |
counter++; |
| 534 |
module.exports = { answer: counter }; |
| 535 |
}, |
| 536 |
'../../lib/quux.js': function ( require, module ) { |
| 537 |
module.exports = 'Quux'; |
| 538 |
}, |
| 539 |
'../bar/bar.js': function ( require, module ) { |
| 540 |
const core = require( './core.js' ); |
| 541 |
module.exports = { data: core.sayHello( 'Alice' ) }; |
| 542 |
}, |
| 543 |
'../bar/core.js': function ( require, module ) { |
| 544 |
module.exports = { sayHello: function ( name ) { |
| 545 |
return 'Hello ' + name; |
| 546 |
} }; |
| 547 |
}, |
| 548 |
'init.js': function ( require ) { |
| 549 |
initJsRan = true; |
| 550 |
assert.deepEqual( require( './data/hello.json' ), { hello: 'world' }, 'require() .json' ); |
| 551 |
assert.deepEqual( require( './foo.js' ), { answer: 42 }, 'require() .js in same dir' ); |
| 552 |
assert.deepEqual( require( '../bar/bar.js' ), { data: 'Hello Alice' }, 'require() with ../ ' ); |
| 553 |
assert.deepEqual( require( '../../lib/quux.js' ), 'Quux', 'require() with ../../ ' ); |
| 554 |
assert.deepEqual( require( './foo.js' ), { answer: 42 }, 'require() same script twice' ); |
| 555 |
} |
| 556 |
} |
| 557 |
}, |
| 558 |
{}, |
| 559 |
{}, |
| 560 |
{} |
| 561 |
); |
| 562 |
|
| 563 |
return mw.loader.using( 'test.implement.packageWithParentFiles' ).done( () => { |
| 564 |
assert.true( initJsRan, 'main JS file is executed' ); |
| 565 |
done(); |
| 566 |
} ); |
| 567 |
} ); |
| 568 |
|
| 569 |
QUnit.test( '.implement( name with @ )', ( assert ) => { |
| 570 |
const done = assert.async(); |
| 571 |
|
| 572 |
mw.loader.implement( '@foo/bar', ( $, jQuery, require, module ) => { |
| 573 |
module.exports = 'foobar'; |
| 574 |
} ); |
| 575 |
|
| 576 |
mw.loader.implement( 'baz@quux@123', ( $, jQuery, require, module ) => { |
| 577 |
module.exports = 'bazquux'; |
| 578 |
} ); |
| 579 |
|
| 580 |
assert.strictEqual( mw.loader.getState( '@foo/bar' ), 'loaded' ); |
| 581 |
assert.strictEqual( mw.loader.getState( 'baz@quux' ), 'loaded' ); |
| 582 |
mw.loader.using( [ '@foo/bar', 'baz@quux' ], ( require ) => { |
| 583 |
assert.strictEqual( mw.loader.getState( '@foo/bar' ), 'ready' ); |
| 584 |
assert.strictEqual( require( '@foo/bar' ), 'foobar' ); |
| 585 |
assert.strictEqual( mw.loader.getState( 'baz@quux' ), 'ready' ); |
| 586 |
assert.strictEqual( require( 'baz@quux' ), 'bazquux' ); |
| 587 |
done(); |
| 588 |
} ); |
| 589 |
} ); |
| 590 |
|
| 591 |
QUnit.test( '.addSource()', ( assert ) => { |
| 592 |
mw.loader.addSource( { testsource1: 'https://1.test/src' } ); |
| 593 |
|
| 594 |
assert.throws( () => { |
| 595 |
mw.loader.addSource( { testsource1: 'https://1.test/src' } ); |
| 596 |
}, /already registered/, 'duplicate pair from addSource(Object)' ); |
| 597 |
|
| 598 |
assert.throws( () => { |
| 599 |
mw.loader.addSource( { testsource1: 'https://1.test/src-diff' } ); |
| 600 |
}, /already registered/, 'duplicate ID from addSource(Object)' ); |
| 601 |
} ); |
| 602 |
|
| 603 |
QUnit.test( '.register() - ES6 support always true', ( assert ) => { |
| 604 |
mw.loader.register( 'test1.regular', 'aaa' ); |
| 605 |
mw.loader.register( 'test1.es6only', 'bbb!' ); |
| 606 |
assert.strictEqual( mw.loader.getState( 'test1.regular' ), 'registered' ); |
| 607 |
assert.strictEqual( mw.loader.getState( 'test1.es6only' ), 'registered' ); |
| 608 |
} ); |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
QUnit.test( '.batchRequest() - Module version combines for given batch', ( assert ) => { |
| 614 |
mw.loader.register( [ |
| 615 |
|
| 616 |
[ 'testUrlInc', 'url', [], null, 'testloader' ], |
| 617 |
[ 'testUrlIncDump', 'dump', [], null, 'testloader' ] |
| 618 |
] ); |
| 619 |
|
| 620 |
mw.loader.maxQueryLength = 10; |
| 621 |
|
| 622 |
return mw.loader.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( ( require ) => { |
| 623 |
assert.propEqual( |
| 624 |
require( 'testUrlIncDump' ).query, |
| 625 |
{ |
| 626 |
modules: 'testUrlIncDump', |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
version: '13e9z' |
| 632 |
}, |
| 633 |
'Query parameters' |
| 634 |
); |
| 635 |
|
| 636 |
assert.strictEqual( mw.loader.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' ); |
| 637 |
} ); |
| 638 |
} ); |
| 639 |
|
| 640 |
|
| 641 |
QUnit.test( '.batchRequest() - Module version combined based on sorted order', ( assert ) => { |
| 642 |
mw.loader.register( [ |
| 643 |
|
| 644 |
[ 'testUrlOrder', 'url', [], null, 'testloader' ], |
| 645 |
[ 'testUrlOrder.a', '1', [], null, 'testloader' ], |
| 646 |
[ 'testUrlOrder.b', '2', [], null, 'testloader' ], |
| 647 |
[ 'testUrlOrderDump', 'dump', [], null, 'testloader' ] |
| 648 |
] ); |
| 649 |
|
| 650 |
return mw.loader.using( [ |
| 651 |
'testUrlOrderDump', |
| 652 |
'testUrlOrder.b', |
| 653 |
'testUrlOrder.a', |
| 654 |
'testUrlOrder' |
| 655 |
] ).then( ( require ) => { |
| 656 |
assert.propEqual( |
| 657 |
require( 'testUrlOrderDump' ).query, |
| 658 |
{ |
| 659 |
modules: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b', |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
version: '1knqz' |
| 665 |
}, |
| 666 |
'Query parameters' |
| 667 |
); |
| 668 |
} ); |
| 669 |
} ); |
| 670 |
|
| 671 |
QUnit.test( 'Broken indirect dependency', function ( assert ) { |
| 672 |
this.useStubClock(); |
| 673 |
|
| 674 |
|
| 675 |
this.sandbox.stub( mw, 'trackError' ); |
| 676 |
|
| 677 |
mw.loader.register( [ |
| 678 |
[ 'test.module1', '0' ], |
| 679 |
[ 'test.module2', '0', [ 'test.module1' ] ], |
| 680 |
[ 'test.module3', '0', [ 'test.module2' ] ] |
| 681 |
] ); |
| 682 |
mw.loader.implement( 'test.module1', () => { |
| 683 |
throw new Error( 'expected' ); |
| 684 |
}, {}, {} ); |
| 685 |
this.tick(); |
| 686 |
|
| 687 |
assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'State of test.module1' ); |
| 688 |
assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'State of test.module2' ); |
| 689 |
assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'State of test.module3' ); |
| 690 |
|
| 691 |
assert.strictEqual( mw.trackError.callCount, 1 ); |
| 692 |
} ); |
| 693 |
|
| 694 |
QUnit.test( 'Out-of-order implementation', function ( assert ) { |
| 695 |
this.useStubClock(); |
| 696 |
|
| 697 |
mw.loader.register( [ |
| 698 |
[ 'test.module4', '0' ], |
| 699 |
[ 'test.module5', '0', [ 'test.module4' ] ], |
| 700 |
[ 'test.module6', '0', [ 'test.module5' ] ] |
| 701 |
] ); |
| 702 |
|
| 703 |
mw.loader.implement( 'test.module4', () => {} ); |
| 704 |
this.tick(); |
| 705 |
assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'State of test.module4' ); |
| 706 |
assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'State of test.module5' ); |
| 707 |
assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'State of test.module6' ); |
| 708 |
|
| 709 |
mw.loader.implement( 'test.module6', () => {} ); |
| 710 |
this.tick(); |
| 711 |
assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'State of test.module4' ); |
| 712 |
assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'State of test.module5' ); |
| 713 |
assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'State of test.module6' ); |
| 714 |
|
| 715 |
mw.loader.implement( 'test.module5', () => {} ); |
| 716 |
this.tick(); |
| 717 |
assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'State of test.module4' ); |
| 718 |
assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'State of test.module5' ); |
| 719 |
assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'State of test.module6' ); |
| 720 |
} ); |
| 721 |
|
| 722 |
QUnit.test( 'Missing dependency', function ( assert ) { |
| 723 |
this.useStubClock(); |
| 724 |
|
| 725 |
mw.loader.register( [ |
| 726 |
[ 'test.module7', '0' ], |
| 727 |
[ 'test.module8', '0', [ 'test.module7' ] ], |
| 728 |
[ 'test.module9', '0', [ 'test.module8' ] ] |
| 729 |
] ); |
| 730 |
|
| 731 |
mw.loader.implement( 'test.module8', () => {} ); |
| 732 |
this.tick(); |
| 733 |
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' ); |
| 734 |
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' ); |
| 735 |
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' ); |
| 736 |
|
| 737 |
mw.loader.state( { 'test.module7': 'missing' } ); |
| 738 |
this.tick(); |
| 739 |
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' ); |
| 740 |
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' ); |
| 741 |
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' ); |
| 742 |
|
| 743 |
mw.loader.implement( 'test.module9', () => {} ); |
| 744 |
this.tick(); |
| 745 |
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' ); |
| 746 |
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' ); |
| 747 |
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' ); |
| 748 |
|
| 749 |
|
| 750 |
this.clock.restore(); |
| 751 |
return mw.loader.using( [ 'test.module7' ] ).then( |
| 752 |
() => { |
| 753 |
throw new Error( 'Success fired despite missing dependency' ); |
| 754 |
}, |
| 755 |
( e, dependencies ) => { |
| 756 |
assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' ); |
| 757 |
assert.deepEqual( |
| 758 |
dependencies, |
| 759 |
[ 'jquery', 'mediawiki.base', 'test.module7' ], |
| 760 |
'Error callback called with module test.module7' |
| 761 |
); |
| 762 |
} |
| 763 |
).then( () => mw.loader.using( [ 'test.module9' ] ) ).then( |
| 764 |
() => { |
| 765 |
throw new Error( 'Success fired despite missing dependency' ); |
| 766 |
}, |
| 767 |
( e, dependencies ) => { |
| 768 |
assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' ); |
| 769 |
dependencies.sort(); |
| 770 |
assert.deepEqual( |
| 771 |
dependencies, |
| 772 |
[ 'jquery', 'mediawiki.base', 'test.module7', 'test.module8', 'test.module9' ], |
| 773 |
'Error callback called with all three modules as dependencies' |
| 774 |
); |
| 775 |
} |
| 776 |
); |
| 777 |
} ); |
| 778 |
|
| 779 |
QUnit.test( 'Dependency handling', ( assert ) => { |
| 780 |
mw.loader.register( [ |
| 781 |
|
| 782 |
[ 'testMissing', '1', [], null, 'testloader' ], |
| 783 |
[ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ], |
| 784 |
[ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ] |
| 785 |
] ); |
| 786 |
|
| 787 |
function verifyModuleStates() { |
| 788 |
assert.strictEqual( mw.loader.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' ); |
| 789 |
assert.strictEqual( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' ); |
| 790 |
assert.strictEqual( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' ); |
| 791 |
} |
| 792 |
|
| 793 |
return mw.loader.using( [ 'testUsesNestedMissing' ] ).then( |
| 794 |
() => { |
| 795 |
verifyModuleStates(); |
| 796 |
throw new Error( 'Error handler should be invoked.' ); |
| 797 |
}, |
| 798 |
( e, modules ) => { |
| 799 |
|
| 800 |
|
| 801 |
assert.true( modules.includes( 'testMissing' ), 'Triggered by testMissing.' ); |
| 802 |
|
| 803 |
verifyModuleStates(); |
| 804 |
} |
| 805 |
); |
| 806 |
} ); |
| 807 |
|
| 808 |
|
| 809 |
QUnit.test( 'Network failure', function ( assert ) { |
| 810 |
|
| 811 |
mw.loader.register( [ |
| 812 |
|
| 813 |
[ 'testNetfailBadDump', '1', [], 'unlucky', 'testloader' ], |
| 814 |
[ 'testNetfailGoodDump', '1', [], 'lucky', 'testloader' ], |
| 815 |
[ 'testNetfailDump', '1', [ 'testNetfailBadDump', 'testNetfailGoodDump' ], null, 'testloader' ] |
| 816 |
] ); |
| 817 |
|
| 818 |
|
| 819 |
const appendSuper = document.head.appendChild; |
| 820 |
const appendStub = this.sandbox.stub( document.head, 'appendChild', function ( node ) { |
| 821 |
if ( node.nodeName === 'SCRIPT' && node.src.includes( 'testNetfailBadDump' ) ) { |
| 822 |
Promise.resolve().then( node.onerror ); |
| 823 |
appendStub.restore(); |
| 824 |
return; |
| 825 |
} |
| 826 |
return appendSuper.apply( this, arguments ); |
| 827 |
} ); |
| 828 |
|
| 829 |
return mw.loader.using( [ 'testNetfailDump' ] ).then( |
| 830 |
() => { |
| 831 |
throw new Error( 'Unexpected success.' ); |
| 832 |
}, |
| 833 |
( e, modules ) => { |
| 834 |
assert.propEqual( { |
| 835 |
testNetfailDump: mw.loader.getState( 'testNetfailDump' ), |
| 836 |
testNetfailBadDump: mw.loader.getState( 'testNetfailBadDump' ) |
| 837 |
}, { |
| 838 |
testNetfailDump: 'error', |
| 839 |
testNetfailBadDump: 'error' |
| 840 |
}, 'module state' ); |
| 841 |
|
| 842 |
assert.strictEqual( e.message, 'Failed dependency: testNetfailBadDump', 'error message' ); |
| 843 |
assert.true( modules.includes( 'testNetfailBadDump' ), 'attribute failure' ); |
| 844 |
} |
| 845 |
); |
| 846 |
} ); |
| 847 |
|
| 848 |
QUnit.test( 'Skip-function handling', ( assert ) => { |
| 849 |
mw.loader.register( [ |
| 850 |
|
| 851 |
[ 'testSkipped', '1', [], null, 'testloader', 'return true;' ], |
| 852 |
[ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ], |
| 853 |
[ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ] |
| 854 |
] ); |
| 855 |
|
| 856 |
return mw.loader.using( [ 'testUsesSkippable' ] ).then( |
| 857 |
() => { |
| 858 |
assert.strictEqual( mw.loader.getState( 'testSkipped' ), 'ready', 'Skipped module' ); |
| 859 |
assert.strictEqual( mw.loader.getState( 'testNotSkipped' ), 'ready', 'Regular module' ); |
| 860 |
assert.strictEqual( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' ); |
| 861 |
}, |
| 862 |
( e, badmodules ) => { |
| 863 |
|
| 864 |
|
| 865 |
assert.deepEqual( badmodules, [], 'Bad modules' ); |
| 866 |
} |
| 867 |
); |
| 868 |
} ); |
| 869 |
|
| 870 |
|
| 871 |
QUnit.test( '.load() [protocol-relative URL T32825]', ( assert ) => { |
| 872 |
const done = assert.async(); |
| 873 |
let target = SCRIPT_PATH_URL + 'tests/qunit/data/mwLoaderTestCallback.js'; |
| 874 |
|
| 875 |
target = target.replace( /https?:/, '' ); |
| 876 |
assert.strictEqual( target.slice( 0, 2 ), '//', 'URL is protocol-relative' ); |
| 877 |
|
| 878 |
mw.loader.testCallback = function () { |
| 879 |
|
| 880 |
delete mw.loader.testCallback; |
| 881 |
assert.true( true, 'callback' ); |
| 882 |
done(); |
| 883 |
}; |
| 884 |
|
| 885 |
|
| 886 |
mw.loader.load( target ); |
| 887 |
} ); |
| 888 |
|
| 889 |
QUnit.test( '.load() [absolute URL]', ( assert ) => { |
| 890 |
const done = assert.async(); |
| 891 |
const target = SCRIPT_PATH_URL + 'tests/qunit/data/mwLoaderTestCallback.js'; |
| 892 |
|
| 893 |
mw.loader.testCallback = function () { |
| 894 |
|
| 895 |
delete mw.loader.testCallback; |
| 896 |
assert.true( true, 'callback' ); |
| 897 |
done(); |
| 898 |
}; |
| 899 |
|
| 900 |
|
| 901 |
mw.loader.load( target ); |
| 902 |
} ); |
| 903 |
|
| 904 |
QUnit.test( 'importScript()', function ( assert ) { |
| 905 |
|
| 906 |
mw.config.set( 'wgScript', '/w/index.php' ); |
| 907 |
const stub = this.sandbox.stub( mw.loader, 'addScriptTag' ); |
| 908 |
|
| 909 |
importScript( 'User:Foo bar!/Scripts=Love/[email protected]' ); |
| 910 |
assert.deepEqual( stub.getCall( 0 ).args, [ |
| 911 |
'/w/index.php?title=User:Foo_bar!/Scripts%3DLove/[email protected]&action=raw&ctype=text/javascript' |
| 912 |
] ); |
| 913 |
} ); |
| 914 |
|
| 915 |
QUnit.test( 'importStylesheet()', function ( assert ) { |
| 916 |
|
| 917 |
mw.config.set( 'wgScript', '/w/index.php' ); |
| 918 |
const stub = this.sandbox.stub( mw.loader, 'addLinkTag' ); |
| 919 |
|
| 920 |
importStylesheet( 'User:Foo bar!/Scripts=Love/[email protected]' ); |
| 921 |
assert.deepEqual( stub.getCall( 0 ).args, [ |
| 922 |
'/w/index.php?title=User:Foo_bar!/Scripts%3DLove/[email protected]&action=raw&ctype=text/css' |
| 923 |
] ); |
| 924 |
} ); |
| 925 |
|
| 926 |
QUnit.test( 'Empty string module name - T28804', ( assert ) => { |
| 927 |
let done = false; |
| 928 |
|
| 929 |
assert.strictEqual( mw.loader.moduleRegistry[ '' ], undefined, 'Unregistered' ); |
| 930 |
|
| 931 |
mw.loader.register( '', 'v1' ); |
| 932 |
assert.strictEqual( mw.loader.moduleRegistry[ '' ].state, 'registered', 'State before' ); |
| 933 |
assert.strictEqual( mw.loader.moduleRegistry[ '' ].version, 'v1', 'Version' ); |
| 934 |
|
| 935 |
mw.loader.implement( '', () => { |
| 936 |
done = true; |
| 937 |
} ); |
| 938 |
|
| 939 |
return mw.loader.using( '', () => { |
| 940 |
assert.strictEqual( done, true, 'script ran' ); |
| 941 |
assert.strictEqual( mw.loader.moduleRegistry[ '' ].state, 'ready', 'State after' ); |
| 942 |
} ); |
| 943 |
} ); |
| 944 |
|
| 945 |
QUnit.test( 'Executing race - T112232', ( assert ) => { |
| 946 |
let done = false; |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
mw.loader.implement( |
| 951 |
'testRaceRedHerring', |
| 952 |
() => {}, |
| 953 |
{ css: [ '.mw-testRaceRedHerring {}' ] } |
| 954 |
); |
| 955 |
mw.loader.implement( |
| 956 |
'testRaceLoadMe', |
| 957 |
() => { |
| 958 |
done = true; |
| 959 |
}, |
| 960 |
{ css: [ '.mw-testRaceLoadMe { float: left; }' ] } |
| 961 |
); |
| 962 |
|
| 963 |
mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] ); |
| 964 |
return mw.loader.using( 'testRaceLoadMe', () => { |
| 965 |
assert.strictEqual( done, true, 'script ran' ); |
| 966 |
assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' ); |
| 967 |
} ); |
| 968 |
} ); |
| 969 |
|
| 970 |
QUnit.test( 'Stale response caching - T117587', function ( assert ) { |
| 971 |
let count = 0; |
| 972 |
|
| 973 |
this.sandbox.stub( mw.loader.store, 'enabled', true ); |
| 974 |
this.sandbox.stub( window, 'setTimeout', ( fn ) => { |
| 975 |
fn(); |
| 976 |
} ); |
| 977 |
this.sandbox.stub( mw, 'requestIdleCallback', ( fn ) => { |
| 978 |
fn(); |
| 979 |
} ); |
| 980 |
|
| 981 |
mw.loader.register( 'test.stale', 'v2' ); |
| 982 |
assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' ); |
| 983 |
|
| 984 |
mw.loader.impl( () => [ |
| 985 |
'test.stale@v1', |
| 986 |
function () { |
| 987 |
count++; |
| 988 |
} |
| 989 |
] ); |
| 990 |
|
| 991 |
return mw.loader.using( 'test.stale' ) |
| 992 |
.then( () => { |
| 993 |
assert.strictEqual( count, 1 ); |
| 994 |
|
| 995 |
assert.strictEqual( mw.loader.moduleRegistry[ 'test.stale' ].version, 'v1', 'Override version' ); |
| 996 |
assert.strictEqual( mw.loader.moduleRegistry[ 'test.stale' ].state, 'ready' ); |
| 997 |
assert.strictEqual( typeof mw.loader.store.get( 'test.stale' ), 'string', 'In store' ); |
| 998 |
} ) |
| 999 |
.then( () => { |
| 1000 |
|
| 1001 |
mw.loader.moduleRegistry[ 'test.stale' ].script = undefined; |
| 1002 |
mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered'; |
| 1003 |
mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2'; |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' ); |
| 1008 |
} ); |
| 1009 |
} ); |
| 1010 |
|
| 1011 |
QUnit.test( 'No storing of group=private responses', function ( assert ) { |
| 1012 |
const name = 'test.group.priv'; |
| 1013 |
|
| 1014 |
|
| 1015 |
this.sandbox.stub( mw.loader.store, 'enabled', true ); |
| 1016 |
this.sandbox.stub( window, 'setTimeout', ( fn ) => { |
| 1017 |
fn(); |
| 1018 |
} ); |
| 1019 |
this.sandbox.stub( mw, 'requestIdleCallback', ( fn ) => { |
| 1020 |
fn(); |
| 1021 |
} ); |
| 1022 |
|
| 1023 |
|
| 1024 |
mw.loader.register( name, 'x', [], 1 ); |
| 1025 |
assert.strictEqual( mw.loader.store.get( name ), false, 'Not in store' ); |
| 1026 |
|
| 1027 |
mw.loader.implement( name, () => {} ); |
| 1028 |
return mw.loader.using( name ).then( () => { |
| 1029 |
assert.strictEqual( mw.loader.getState( name ), 'ready' ); |
| 1030 |
assert.strictEqual( mw.loader.store.get( name ), false, 'Still not in store' ); |
| 1031 |
} ); |
| 1032 |
} ); |
| 1033 |
|
| 1034 |
QUnit.test( 'No storing of group=user responses', function ( assert ) { |
| 1035 |
const name = 'test.group.user'; |
| 1036 |
|
| 1037 |
|
| 1038 |
this.sandbox.stub( mw.loader.store, 'enabled', true ); |
| 1039 |
this.sandbox.stub( window, 'setTimeout', ( fn ) => { |
| 1040 |
fn(); |
| 1041 |
} ); |
| 1042 |
this.sandbox.stub( mw, 'requestIdleCallback', ( fn ) => { |
| 1043 |
fn(); |
| 1044 |
} ); |
| 1045 |
|
| 1046 |
|
| 1047 |
mw.loader.register( name, 'y', [], 0 ); |
| 1048 |
assert.strictEqual( mw.loader.store.get( name ), false, 'Not in store' ); |
| 1049 |
|
| 1050 |
mw.loader.implement( name, () => {} ); |
| 1051 |
return mw.loader.using( name ).then( () => { |
| 1052 |
assert.strictEqual( mw.loader.getState( name ), 'ready' ); |
| 1053 |
assert.strictEqual( mw.loader.store.get( name ), false, 'Still not in store' ); |
| 1054 |
} ); |
| 1055 |
} ); |
| 1056 |
|
| 1057 |
QUnit.test( 'mw.loader.store.load - Disallowed localStorage', function ( assert ) { |
| 1058 |
this.stubStore(); |
| 1059 |
this.sandbox.stub( Storage.prototype, 'getItem', () => { |
| 1060 |
throw new Error( 'Mock-disabled localStorage' ); |
| 1061 |
} ); |
| 1062 |
|
| 1063 |
mw.loader.store.load(); |
| 1064 |
assert.false( mw.loader.store.enabled, 'Disabled' ); |
| 1065 |
} ); |
| 1066 |
|
| 1067 |
QUnit.test( 'mw.loader.store.load - Invalid JSON', function ( assert ) { |
| 1068 |
this.stubStore(); |
| 1069 |
localStorage.setItem( mw.loader.store.key, 'invalid' ); |
| 1070 |
|
| 1071 |
mw.loader.store.load(); |
| 1072 |
assert.true( mw.loader.store.enabled, 'Enabled' ); |
| 1073 |
assert.true( $.isEmptyObject( mw.loader.store.items ), 'Items stay empty' ); |
| 1074 |
} ); |
| 1075 |
|
| 1076 |
QUnit.test( 'mw.loader.store.load - Unusable JSON', function ( assert ) { |
| 1077 |
this.stubStore(); |
| 1078 |
localStorage.setItem( mw.loader.store.key, JSON.stringify( { wrong: true } ) ); |
| 1079 |
|
| 1080 |
mw.loader.store.load(); |
| 1081 |
assert.true( mw.loader.store.enabled, 'Enabled' ); |
| 1082 |
assert.true( $.isEmptyObject( mw.loader.store.items ), 'Items stay empty' ); |
| 1083 |
} ); |
| 1084 |
|
| 1085 |
QUnit.test( 'mw.loader.store.load - Expired JSON', function ( assert ) { |
| 1086 |
this.stubStore(); |
| 1087 |
localStorage.setItem( mw.loader.store.key, JSON.stringify( { |
| 1088 |
items: { use: 'not me' }, |
| 1089 |
vary: mw.loader.store.vary, |
| 1090 |
asOf: 130161 |
| 1091 |
} ) ); |
| 1092 |
|
| 1093 |
mw.loader.store.load(); |
| 1094 |
assert.true( mw.loader.store.enabled, 'Enabled' ); |
| 1095 |
assert.true( $.isEmptyObject( mw.loader.store.items ), 'Items stay empty' ); |
| 1096 |
} ); |
| 1097 |
|
| 1098 |
QUnit.test( 'mw.loader.store.load - Good JSON', function ( assert ) { |
| 1099 |
this.stubStore(); |
| 1100 |
localStorage.setItem( mw.loader.store.key, JSON.stringify( { |
| 1101 |
items: { use: 'me' }, |
| 1102 |
vary: mw.loader.store.vary, |
| 1103 |
asOf: Math.ceil( Date.now() / 1e7 ) - 5 |
| 1104 |
} ) ); |
| 1105 |
|
| 1106 |
mw.loader.store.load(); |
| 1107 |
assert.true( mw.loader.store.enabled, 'Enabled' ); |
| 1108 |
assert.deepEqual( |
| 1109 |
mw.loader.store.items, |
| 1110 |
{ use: 'me' }, |
| 1111 |
'Items are loaded' |
| 1112 |
); |
| 1113 |
} ); |
| 1114 |
|
| 1115 |
QUnit.test( 'require()', ( assert ) => { |
| 1116 |
mw.loader.register( [ |
| 1117 |
[ 'test.require1', '0' ], |
| 1118 |
[ 'test.require2', '0' ], |
| 1119 |
[ 'test.require3', '0' ], |
| 1120 |
[ 'test.require4', '0', [ 'test.require3' ] ] |
| 1121 |
] ); |
| 1122 |
mw.loader.implement( 'test.require1', () => {} ); |
| 1123 |
mw.loader.implement( 'test.require2', ( $, jQuery, require, module ) => { |
| 1124 |
module.exports = 1; |
| 1125 |
} ); |
| 1126 |
mw.loader.implement( 'test.require3', ( $, jQuery, require, module ) => { |
| 1127 |
module.exports = function () { |
| 1128 |
return 'hello world'; |
| 1129 |
}; |
| 1130 |
} ); |
| 1131 |
mw.loader.implement( 'test.require4', ( $, jQuery, require, module ) => { |
| 1132 |
const other = require( 'test.require3' ); |
| 1133 |
module.exports = { |
| 1134 |
pizza: function () { |
| 1135 |
return other(); |
| 1136 |
} |
| 1137 |
}; |
| 1138 |
} ); |
| 1139 |
return mw.loader.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( ( require ) => { |
| 1140 |
const module1 = require( 'test.require1' ); |
| 1141 |
const module2 = require( 'test.require2' ); |
| 1142 |
const module3 = require( 'test.require3' ); |
| 1143 |
const module4 = require( 'test.require4' ); |
| 1144 |
|
| 1145 |
assert.strictEqual( typeof module1, 'object', 'export of module with no export' ); |
| 1146 |
assert.strictEqual( module2, 1, 'export a number' ); |
| 1147 |
assert.strictEqual( module3(), 'hello world', 'export a function' ); |
| 1148 |
assert.strictEqual( typeof module4.pizza, 'function', 'export an object' ); |
| 1149 |
assert.strictEqual( module4.pizza(), 'hello world', 'module can require other modules' ); |
| 1150 |
|
| 1151 |
assert.throws( () => { |
| 1152 |
require( '_badmodule' ); |
| 1153 |
}, /is not loaded/, 'Requesting non-existent modules throws error.' ); |
| 1154 |
} ); |
| 1155 |
} ); |
| 1156 |
|
| 1157 |
QUnit.test( 'require() in debug mode', ( assert ) => { |
| 1158 |
mw.loader.register( [ |
| 1159 |
[ 'test.require.define', '0' ], |
| 1160 |
[ 'test.require.callback', '0', [ 'test.require.define' ] ] |
| 1161 |
] ); |
| 1162 |
mw.loader.implement( 'test.require.callback', [ SCRIPT_PATH_URL + 'tests/qunit/data/requireCallMwLoaderTestCallback.js' ] ); |
| 1163 |
mw.loader.implement( 'test.require.define', [ SCRIPT_PATH_URL + 'tests/qunit/data/defineCallMwLoaderTestCallback.js' ] ); |
| 1164 |
|
| 1165 |
return mw.loader.using( 'test.require.callback' ).then( ( require ) => { |
| 1166 |
const cb = require( 'test.require.callback' ); |
| 1167 |
assert.strictEqual( cb.immediate, 'Defined.', 'module.exports and require work in debug mode' ); |
| 1168 |
|
| 1169 |
|
| 1170 |
try { |
| 1171 |
assert.strictEqual( cb.later(), 'Defined.', 'require works asynchrously in debug mode' ); |
| 1172 |
} catch ( e ) { |
| 1173 |
assert.strictEqual( String( e ), null, 'require works asynchrously in debug mode' ); |
| 1174 |
} |
| 1175 |
} ); |
| 1176 |
} ); |
| 1177 |
|
| 1178 |
QUnit.test( 'Implicit dependencies', ( assert ) => { |
| 1179 |
let user = 0, |
| 1180 |
site = 0, |
| 1181 |
siteFromUser = 0; |
| 1182 |
|
| 1183 |
mw.loader.implement( |
| 1184 |
'site', |
| 1185 |
() => { |
| 1186 |
site++; |
| 1187 |
} |
| 1188 |
); |
| 1189 |
mw.loader.implement( |
| 1190 |
'user', |
| 1191 |
() => { |
| 1192 |
user++; |
| 1193 |
siteFromUser = site; |
| 1194 |
} |
| 1195 |
); |
| 1196 |
|
| 1197 |
return mw.loader.using( 'user', () => { |
| 1198 |
assert.strictEqual( site, 1, 'site module' ); |
| 1199 |
assert.strictEqual( user, 1, 'user module' ); |
| 1200 |
assert.strictEqual( siteFromUser, 1, 'site ran before user' ); |
| 1201 |
} ).always( () => { |
| 1202 |
|
| 1203 |
mw.loader.moduleRegistry.site.state = 'registered'; |
| 1204 |
mw.loader.moduleRegistry.user.state = 'registered'; |
| 1205 |
} ); |
| 1206 |
} ); |
| 1207 |
|
| 1208 |
QUnit.test( '.getScript() - success', ( assert ) => { |
| 1209 |
const scriptUrl = SCRIPT_PATH_URL + 'tests/qunit/data/mediawiki.loader.getScript.example.js'; |
| 1210 |
|
| 1211 |
return mw.loader.getScript( scriptUrl ).then( |
| 1212 |
() => { |
| 1213 |
assert.strictEqual( mw.getScriptExampleScriptLoaded, true, 'Data attached to a global object is available' ); |
| 1214 |
} |
| 1215 |
); |
| 1216 |
} ); |
| 1217 |
|
| 1218 |
QUnit.test( '.getScript() - failure', ( assert ) => { |
| 1219 |
assert.rejects( |
| 1220 |
mw.loader.getScript( '/this-is-not-found.txt' ), |
| 1221 |
/Failed to load script/, |
| 1222 |
'Descriptive error message' |
| 1223 |
); |
| 1224 |
} ); |
| 1225 |
|
| 1226 |
}() ); |