0b05ac12449208bb63ed1759c94e766c6da3dad2
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.loader.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki (mw.loader)', QUnit.newMwEnvironment( {
3 setup: function ( assert ) {
4 mw.loader.store.enabled = false;
5
6 // Expose for load.mock.php
7 mw.loader.testFail = function ( reason ) {
8 assert.ok( false, reason );
9 };
10 },
11 teardown: function () {
12 mw.loader.store.enabled = false;
13 // Teardown for StringSet shim test
14 if ( this.nativeSet ) {
15 window.Set = this.nativeSet;
16 mw.redefineFallbacksForTest();
17 }
18 // Remove any remaining temporary statics
19 // exposed for cross-file mocks.
20 if ( 'testCallback' in mw.loader ) {
21 delete mw.loader.testCallback;
22 }
23 if ( 'testFail' in mw.loader ) {
24 delete mw.loader.testFail;
25 }
26 }
27 } ) );
28
29 mw.loader.addSource(
30 'testloader',
31 QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
32 );
33
34 /**
35 * The sync style load test (for @import). This is, in a way, also an open bug for
36 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
37 * way to get a callback from when a stylesheet is loaded (that is, including any
38 * `@import` rules inside). To work around this, we'll have a little time loop to check
39 * if the styles apply.
40 *
41 * Note: This test originally used new Image() and onerror to get a callback
42 * when the url is loaded, but that is fragile since it doesn't monitor the
43 * same request as the css @import, and Safari 4 has issues with
44 * onerror/onload not being fired at all in weird cases like this.
45 */
46 function assertStyleAsync( assert, $element, prop, val, fn ) {
47 var styleTestStart,
48 el = $element.get( 0 ),
49 styleTestTimeout = ( QUnit.config.testTimeout || 5000 ) - 200;
50
51 function isCssImportApplied() {
52 // Trigger reflow, repaint, redraw, whatever (cross-browser)
53 $element.css( 'height' );
54 // eslint-disable-next-line no-unused-expressions
55 el.innerHTML;
56 el.className = el.className;
57 // eslint-disable-next-line no-unused-expressions
58 document.documentElement.clientHeight;
59
60 return $element.css( prop ) === val;
61 }
62
63 function styleTestLoop() {
64 var styleTestSince = new Date().getTime() - styleTestStart;
65 // If it is passing or if we timed out, run the real test and stop the loop
66 if ( isCssImportApplied() || styleTestSince > styleTestTimeout ) {
67 assert.equal( $element.css( prop ), val,
68 'style "' + prop + ': ' + val + '" from url is applied (after ' + styleTestSince + 'ms)'
69 );
70
71 if ( fn ) {
72 fn();
73 }
74
75 return;
76 }
77 // Otherwise, keep polling
78 setTimeout( styleTestLoop );
79 }
80
81 // Start the loop
82 styleTestStart = new Date().getTime();
83 styleTestLoop();
84 }
85
86 function urlStyleTest( selector, prop, val ) {
87 return QUnit.fixurl(
88 mw.config.get( 'wgScriptPath' ) +
89 '/tests/qunit/data/styleTest.css.php?' +
90 $.param( {
91 selector: selector,
92 prop: prop,
93 val: val
94 } )
95 );
96 }
97
98 QUnit.test( 'Basic', function ( assert ) {
99 var isAwesomeDone;
100
101 mw.loader.testCallback = function () {
102 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
103 isAwesomeDone = true;
104 };
105
106 mw.loader.implement( 'test.callback', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
107
108 return mw.loader.using( 'test.callback', function () {
109 assert.strictEqual( isAwesomeDone, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
110 }, function () {
111 assert.ok( false, 'Error callback fired while loader.using "test.callback" module' );
112 } );
113 } );
114
115 QUnit.test( 'Object method as module name', function ( assert ) {
116 var isAwesomeDone;
117
118 mw.loader.testCallback = function () {
119 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
120 isAwesomeDone = true;
121 };
122
123 mw.loader.implement( 'hasOwnProperty', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
124
125 return mw.loader.using( 'hasOwnProperty', function () {
126 assert.strictEqual( isAwesomeDone, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
127 }, function () {
128 assert.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
129 } );
130 } );
131
132 QUnit.test( '.using( .. ) Promise', function ( assert ) {
133 var isAwesomeDone;
134
135 mw.loader.testCallback = function () {
136 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
137 isAwesomeDone = true;
138 };
139
140 mw.loader.implement( 'test.promise', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
141
142 return mw.loader.using( 'test.promise' )
143 .done( function () {
144 assert.strictEqual( isAwesomeDone, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
145 } )
146 .fail( function () {
147 assert.ok( false, 'Error callback fired while loader.using "test.promise" module' );
148 } );
149 } );
150
151 // Covers mw.loader#sortDependencies (with native Set if available)
152 QUnit.test( '.using() Error: Circular dependency [StringSet default]', function ( assert ) {
153 var done = assert.async();
154
155 mw.loader.register( [
156 [ 'test.circle1', '0', [ 'test.circle2' ] ],
157 [ 'test.circle2', '0', [ 'test.circle3' ] ],
158 [ 'test.circle3', '0', [ 'test.circle1' ] ]
159 ] );
160 mw.loader.using( 'test.circle3' ).then(
161 function done() {
162 assert.ok( false, 'Unexpected resolution, expected error.' );
163 },
164 function fail( e ) {
165 assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
166 }
167 )
168 .always( done );
169 } );
170
171 // @covers mw.loader#sortDependencies (with fallback shim)
172 QUnit.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert ) {
173 var done = assert.async();
174
175 if ( !window.Set ) {
176 assert.expect( 0 );
177 done();
178 return;
179 }
180
181 this.nativeSet = window.Set;
182 window.Set = undefined;
183 mw.redefineFallbacksForTest();
184
185 mw.loader.register( [
186 [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
187 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
188 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
189 ] );
190 mw.loader.using( 'test.shim.circle3' ).then(
191 function done() {
192 assert.ok( false, 'Unexpected resolution, expected error.' );
193 },
194 function fail( e ) {
195 assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
196 }
197 )
198 .always( done );
199 } );
200
201 QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
202 var capture = [];
203 mw.loader.register( [
204 [ 'test.circleA', '0', [ 'test.circleB' ] ],
205 [ 'test.circleB', '0', [ 'test.circleC' ] ],
206 [ 'test.circleC', '0', [ 'test.circleA' ] ]
207 ] );
208 this.sandbox.stub( mw, 'track', function ( topic, data ) {
209 capture.push( {
210 topic: topic,
211 error: data.exception && data.exception.message,
212 source: data.source
213 } );
214 } );
215
216 mw.loader.load( 'test.circleC' );
217 assert.deepEqual(
218 [ {
219 topic: 'resourceloader.exception',
220 error: 'Circular reference detected: test.circleB -> test.circleC',
221 source: 'resolve'
222 } ],
223 capture,
224 'Detect circular dependency'
225 );
226 } );
227
228 QUnit.test( '.using() - Error: Unregistered', function ( assert ) {
229 var done = assert.async();
230
231 mw.loader.using( 'test.using.unreg' ).then(
232 function done() {
233 assert.ok( false, 'Unexpected resolution, expected error.' );
234 },
235 function fail( e ) {
236 assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' );
237 }
238 ).always( done );
239 } );
240
241 QUnit.test( '.load() - Error: Unregistered', function ( assert ) {
242 var capture = [];
243 this.sandbox.stub( mw, 'track', function ( topic, data ) {
244 capture.push( {
245 topic: topic,
246 error: data.exception && data.exception.message,
247 source: data.source
248 } );
249 } );
250
251 mw.loader.load( 'test.load.unreg' );
252 assert.deepEqual(
253 [ {
254 topic: 'resourceloader.exception',
255 error: 'Unknown dependency: test.load.unreg',
256 source: 'resolve'
257 } ],
258 capture
259 );
260 } );
261
262 // Regression test for T36853
263 QUnit.test( '.load() - Error: Missing dependency', function ( assert ) {
264 var capture = [];
265 this.sandbox.stub( mw, 'track', function ( topic, data ) {
266 capture.push( {
267 topic: topic,
268 error: data.exception && data.exception.message,
269 source: data.source
270 } );
271 } );
272
273 mw.loader.register( [
274 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
275 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
276 ] );
277 mw.loader.load( 'test.load.missingdep' );
278 assert.deepEqual(
279 [ {
280 topic: 'resourceloader.exception',
281 error: 'Unknown dependency: test.load.missingdep2',
282 source: 'resolve'
283 } ],
284 capture
285 );
286 } );
287
288 QUnit.test( '.implement( styles={ "css": [text, ..] } )', function ( assert ) {
289 var $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
290
291 assert.notEqual(
292 $element.css( 'float' ),
293 'right',
294 'style is clear'
295 );
296
297 mw.loader.implement(
298 'test.implement.a',
299 function () {
300 assert.equal(
301 $element.css( 'float' ),
302 'right',
303 'style is applied'
304 );
305 },
306 {
307 all: '.mw-test-implement-a { float: right; }'
308 }
309 );
310
311 return mw.loader.using( 'test.implement.a' );
312 } );
313
314 QUnit.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert ) {
315 var $element1 = $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
316 $element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
317 $element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
318 done = assert.async();
319
320 assert.notEqual(
321 $element1.css( 'text-align' ),
322 'center',
323 'style is clear'
324 );
325 assert.notEqual(
326 $element2.css( 'float' ),
327 'left',
328 'style is clear'
329 );
330 assert.notEqual(
331 $element3.css( 'text-align' ),
332 'right',
333 'style is clear'
334 );
335
336 mw.loader.implement(
337 'test.implement.b',
338 function () {
339 // Note: done() must only be called when the entire test is
340 // complete. So, make sure that we don't start until *both*
341 // assertStyleAsync calls have completed.
342 var pending = 2;
343 assertStyleAsync( assert, $element2, 'float', 'left', function () {
344 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
345
346 pending--;
347 if ( pending === 0 ) {
348 done();
349 }
350 } );
351 assertStyleAsync( assert, $element3, 'float', 'right', function () {
352 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
353
354 pending--;
355 if ( pending === 0 ) {
356 done();
357 }
358 } );
359 },
360 {
361 url: {
362 print: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
363 screen: [
364 // T42834: Make sure it actually works with more than 1 stylesheet reference
365 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
366 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
367 ]
368 }
369 }
370 );
371
372 mw.loader.load( 'test.implement.b' );
373 } );
374
375 // Backwards compatibility
376 QUnit.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert ) {
377 var $element = $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
378
379 assert.notEqual(
380 $element.css( 'float' ),
381 'right',
382 'style is clear'
383 );
384
385 mw.loader.implement(
386 'test.implement.c',
387 function () {
388 assert.equal(
389 $element.css( 'float' ),
390 'right',
391 'style is applied'
392 );
393 },
394 {
395 all: '.mw-test-implement-c { float: right; }'
396 }
397 );
398
399 return mw.loader.using( 'test.implement.c' );
400 } );
401
402 // Backwards compatibility
403 QUnit.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert ) {
404 var $element = $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
405 $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
406 done = assert.async();
407
408 assert.notEqual(
409 $element.css( 'float' ),
410 'right',
411 'style is clear'
412 );
413 assert.notEqual(
414 $element2.css( 'text-align' ),
415 'center',
416 'style is clear'
417 );
418
419 mw.loader.implement(
420 'test.implement.d',
421 function () {
422 assertStyleAsync( assert, $element, 'float', 'right', function () {
423 assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
424 done();
425 } );
426 },
427 {
428 all: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
429 print: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
430 }
431 );
432
433 mw.loader.load( 'test.implement.d' );
434 } );
435
436 // @import (T33676)
437 QUnit.test( '.implement( styles has @import )', function ( assert ) {
438 var isJsExecuted, $element,
439 done = assert.async();
440
441 mw.loader.implement(
442 'test.implement.import',
443 function () {
444 assert.strictEqual( isJsExecuted, undefined, 'script not executed multiple times' );
445 isJsExecuted = true;
446
447 assert.equal( mw.loader.getState( 'test.implement.import' ), 'executing', 'module state during implement() script execution' );
448
449 $element = $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
450
451 assert.equal( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
452
453 assertStyleAsync( assert, $element, 'float', 'right', function () {
454 assert.equal( $element.css( 'text-align' ), 'center',
455 'CSS styles after the @import rule are working'
456 );
457
458 done();
459 } );
460 },
461 {
462 css: [
463 '@import url(\''
464 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
465 + '\');\n'
466 + '.mw-test-implement-import { text-align: center; }'
467 ]
468 },
469 {
470 'test-foobar': 'Hello Foobar, $1!'
471 }
472 );
473
474 mw.loader.using( 'test.implement.import' ).always( function () {
475 assert.strictEqual( isJsExecuted, true, 'script executed' );
476 assert.equal( mw.loader.getState( 'test.implement.import' ), 'ready', 'module state after script execution' );
477 } );
478 } );
479
480 QUnit.test( '.implement( dependency with styles )', function ( assert ) {
481 var $element = $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
482 $element2 = $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
483
484 assert.notEqual(
485 $element.css( 'float' ),
486 'right',
487 'style is clear'
488 );
489 assert.notEqual(
490 $element2.css( 'float' ),
491 'left',
492 'style is clear'
493 );
494
495 mw.loader.register( [
496 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
497 [ 'test.implement.e2', '0' ]
498 ] );
499
500 mw.loader.implement(
501 'test.implement.e',
502 function () {
503 assert.equal(
504 $element.css( 'float' ),
505 'right',
506 'Depending module\'s style is applied'
507 );
508 },
509 {
510 all: '.mw-test-implement-e { float: right; }'
511 }
512 );
513
514 mw.loader.implement(
515 'test.implement.e2',
516 function () {
517 assert.equal(
518 $element2.css( 'float' ),
519 'left',
520 'Dependency\'s style is applied'
521 );
522 },
523 {
524 all: '.mw-test-implement-e2 { float: left; }'
525 }
526 );
527
528 return mw.loader.using( 'test.implement.e' );
529 } );
530
531 QUnit.test( '.implement( only scripts )', function ( assert ) {
532 mw.loader.implement( 'test.onlyscripts', function () {} );
533 assert.strictEqual( mw.loader.getState( 'test.onlyscripts' ), 'ready' );
534 } );
535
536 QUnit.test( '.implement( only messages )', function ( assert ) {
537 assert.assertFalse( mw.messages.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
538
539 mw.loader.implement( 'test.implement.msgs', [], {}, { T31107: 'loaded' } );
540
541 return mw.loader.using( 'test.implement.msgs', function () {
542 assert.ok( mw.messages.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
543 }, function () {
544 assert.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
545 } );
546 } );
547
548 QUnit.test( '.implement( empty )', function ( assert ) {
549 mw.loader.implement( 'test.empty' );
550 assert.strictEqual( mw.loader.getState( 'test.empty' ), 'ready' );
551 } );
552
553 // @covers mw.loader#batchRequest
554 // This is a regression test because in the past we called getCombinedVersion()
555 // for all requested modules, before url splitting took place.
556 // Discovered as part of T188076, but not directly related.
557 QUnit.test( 'Url composition (modules considered for version)', function ( assert ) {
558 mw.loader.register( [
559 // [module, version, dependencies, group, source]
560 [ 'testUrlInc', 'url', [], null, 'testloader' ],
561 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
562 ] );
563
564 mw.config.set( 'wgResourceLoaderMaxQueryLength', 10 );
565
566 return mw.loader.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require ) {
567 assert.propEqual(
568 require( 'testUrlIncDump' ).query,
569 {
570 modules: 'testUrlIncDump',
571 // Expected: Wrapped hash just for this one module
572 // $hash = hash( 'fnv132', 'dump');
573 // base_convert( $hash, 16, 36 ); // "13e9zzn"
574 // Previously: Wrapped hash for both modules, despite being in separate requests
575 // $hash = hash( 'fnv132', 'urldump' );
576 // base_convert( $hash, 16, 36 ); // "18kz9ca"
577 version: '13e9zzn'
578 },
579 'Query parameters'
580 );
581
582 assert.strictEqual( mw.loader.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
583 } );
584 } );
585
586 // @covers mw.loader#batchRequest
587 // @covers mw.loader#buildModulesString
588 QUnit.test( 'Url composition (order of modules for version) – T188076', function ( assert ) {
589 mw.loader.register( [
590 // [module, version, dependencies, group, source]
591 [ 'testUrlOrder', 'url', [], null, 'testloader' ],
592 [ 'testUrlOrder.a', '1', [], null, 'testloader' ],
593 [ 'testUrlOrder.b', '2', [], null, 'testloader' ],
594 [ 'testUrlOrderDump', 'dump', [], null, 'testloader' ]
595 ] );
596
597 return mw.loader.using( [
598 'testUrlOrderDump',
599 'testUrlOrder.b',
600 'testUrlOrder.a',
601 'testUrlOrder'
602 ] ).then( function ( require ) {
603 assert.propEqual(
604 require( 'testUrlOrderDump' ).query,
605 {
606 modules: 'testUrlOrder,testUrlOrderDump|testUrlOrder.a,b',
607 // Expected: Combined in order after string packing
608 // $hash = hash( 'fnv132', 'urldump12' );
609 // base_convert( $hash, 16, 36 ); // "1knqzan"
610 // Previously: Combined in order of before string packing
611 // $hash = hash( 'fnv132', 'url12dump' );
612 // base_convert( $hash, 16, 36 ); // "11eo3in"
613 version: '1knqzan'
614 },
615 'Query parameters'
616 );
617 } );
618 } );
619
620 QUnit.test( 'Broken indirect dependency', function ( assert ) {
621 // don't emit an error event
622 this.sandbox.stub( mw, 'track' );
623
624 mw.loader.register( [
625 [ 'test.module1', '0' ],
626 [ 'test.module2', '0', [ 'test.module1' ] ],
627 [ 'test.module3', '0', [ 'test.module2' ] ]
628 ] );
629 mw.loader.implement( 'test.module1', function () {
630 throw new Error( 'expected' );
631 }, {}, {} );
632 assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
633 assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
634 assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
635
636 assert.strictEqual( mw.track.callCount, 1 );
637 } );
638
639 QUnit.test( 'Out-of-order implementation', function ( assert ) {
640 mw.loader.register( [
641 [ 'test.module4', '0' ],
642 [ 'test.module5', '0', [ 'test.module4' ] ],
643 [ 'test.module6', '0', [ 'test.module5' ] ]
644 ] );
645 mw.loader.implement( 'test.module4', function () {} );
646 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
647 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
648 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
649 mw.loader.implement( 'test.module6', function () {} );
650 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
651 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
652 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
653 mw.loader.implement( 'test.module5', function () {} );
654 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
655 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
656 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
657 } );
658
659 QUnit.test( 'Missing dependency', function ( assert ) {
660 mw.loader.register( [
661 [ 'test.module7', '0' ],
662 [ 'test.module8', '0', [ 'test.module7' ] ],
663 [ 'test.module9', '0', [ 'test.module8' ] ]
664 ] );
665 mw.loader.implement( 'test.module8', function () {} );
666 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
667 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
668 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
669 mw.loader.state( 'test.module7', 'missing' );
670 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
671 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
672 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
673 mw.loader.implement( 'test.module9', function () {} );
674 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
675 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
676 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
677 mw.loader.using(
678 [ 'test.module7' ],
679 function () {
680 assert.ok( false, 'Success fired despite missing dependency' );
681 assert.ok( true, 'QUnit expected() count dummy' );
682 },
683 function ( e, dependencies ) {
684 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
685 assert.deepEqual( dependencies, [ 'test.module7' ], 'Error callback called with module test.module7' );
686 }
687 );
688 mw.loader.using(
689 [ 'test.module9' ],
690 function () {
691 assert.ok( false, 'Success fired despite missing dependency' );
692 assert.ok( true, 'QUnit expected() count dummy' );
693 },
694 function ( e, dependencies ) {
695 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
696 dependencies.sort();
697 assert.deepEqual(
698 dependencies,
699 [ 'test.module7', 'test.module8', 'test.module9' ],
700 'Error callback called with all three modules as dependencies'
701 );
702 }
703 );
704 } );
705
706 QUnit.test( 'Dependency handling', function ( assert ) {
707 var done = assert.async();
708 mw.loader.register( [
709 // [module, version, dependencies, group, source]
710 [ 'testMissing', '1', [], null, 'testloader' ],
711 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
712 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
713 ] );
714
715 function verifyModuleStates() {
716 assert.equal( mw.loader.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
717 assert.equal( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
718 assert.equal( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
719 }
720
721 mw.loader.using( [ 'testUsesNestedMissing' ],
722 function () {
723 assert.ok( false, 'Error handler should be invoked.' );
724 assert.ok( true ); // Dummy to reach QUnit expect()
725
726 verifyModuleStates();
727
728 done();
729 },
730 function ( e, badmodules ) {
731 assert.ok( true, 'Error handler should be invoked.' );
732 // As soon as server spits out state('testMissing', 'missing');
733 // it will bubble up and trigger the error callback.
734 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
735 assert.deepEqual( badmodules, [ 'testMissing' ], 'Bad modules as expected.' );
736
737 verifyModuleStates();
738
739 done();
740 }
741 );
742 } );
743
744 QUnit.test( 'Skip-function handling', function ( assert ) {
745 mw.loader.register( [
746 // [module, version, dependencies, group, source, skip]
747 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
748 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
749 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
750 ] );
751
752 function verifyModuleStates() {
753 assert.equal( mw.loader.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
754 assert.equal( mw.loader.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
755 assert.equal( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
756 }
757
758 return mw.loader.using( [ 'testUsesSkippable' ],
759 function () {
760 assert.ok( true, 'Success handler should be invoked.' );
761 assert.ok( true ); // Dummy to match error handler and reach QUnit expect()
762
763 verifyModuleStates();
764 },
765 function ( e, badmodules ) {
766 assert.ok( false, 'Error handler should not be invoked.' );
767 assert.deepEqual( badmodules, [], 'Bad modules as expected.' );
768
769 verifyModuleStates();
770 }
771 );
772 } );
773
774 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
775 QUnit.test( '.load( "//protocol-relative" ) - T32825', function ( assert ) {
776 var target,
777 done = assert.async();
778
779 // URL to the callback script
780 target = QUnit.fixurl(
781 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
782 );
783 // Ensure a protocol-relative URL for this test
784 target = target.replace( /https?:/, '' );
785 assert.equal( target.slice( 0, 2 ), '//', 'URL is protocol-relative' );
786
787 mw.loader.testCallback = function () {
788 // Ensure once, delete now
789 delete mw.loader.testCallback;
790 assert.ok( true, 'callback' );
791 done();
792 };
793
794 // Go!
795 mw.loader.load( target );
796 } );
797
798 QUnit.test( '.load( "/absolute-path" )', function ( assert ) {
799 var target,
800 done = assert.async();
801
802 // URL to the callback script
803 target = QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
804 assert.equal( target.slice( 0, 1 ), '/', 'URL is relative to document root' );
805
806 mw.loader.testCallback = function () {
807 // Ensure once, delete now
808 delete mw.loader.testCallback;
809 assert.ok( true, 'callback' );
810 done();
811 };
812
813 // Go!
814 mw.loader.load( target );
815 } );
816
817 QUnit.test( 'Empty string module name - T28804', function ( assert ) {
818 var done = false;
819
820 assert.strictEqual( mw.loader.getState( '' ), null, 'State (unregistered)' );
821
822 mw.loader.register( '', 'v1' );
823 assert.strictEqual( mw.loader.getState( '' ), 'registered', 'State (registered)' );
824 assert.strictEqual( mw.loader.getVersion( '' ), 'v1', 'Version' );
825
826 mw.loader.implement( '', function () {
827 done = true;
828 } );
829
830 return mw.loader.using( '', function () {
831 assert.strictEqual( done, true, 'script ran' );
832 assert.strictEqual( mw.loader.getState( '' ), 'ready', 'State (ready)' );
833 } );
834 } );
835
836 QUnit.test( 'Executing race - T112232', function ( assert ) {
837 var done = false;
838
839 // The red herring schedules its CSS buffer first. In T112232, a bug in the
840 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
841 mw.loader.implement(
842 'testRaceRedHerring',
843 function () {},
844 { css: [ '.mw-testRaceRedHerring {}' ] }
845 );
846 mw.loader.implement(
847 'testRaceLoadMe',
848 function () {
849 done = true;
850 },
851 { css: [ '.mw-testRaceLoadMe { float: left; }' ] }
852 );
853
854 mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
855 return mw.loader.using( 'testRaceLoadMe', function () {
856 assert.strictEqual( done, true, 'script ran' );
857 assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' );
858 } );
859 } );
860
861 QUnit.test( 'Stale response caching - T117587', function ( assert ) {
862 var count = 0;
863 mw.loader.store.enabled = true;
864 mw.loader.register( 'test.stale', 'v2' );
865 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
866
867 mw.loader.implement( 'test.stale@v1', function () {
868 count++;
869 } );
870
871 return mw.loader.using( 'test.stale' )
872 .then( function () {
873 assert.strictEqual( count, 1 );
874 // After implementing, registry contains version as implemented by the response.
875 assert.strictEqual( mw.loader.getVersion( 'test.stale' ), 'v1', 'Override version' );
876 assert.strictEqual( mw.loader.getState( 'test.stale' ), 'ready' );
877 assert.ok( mw.loader.store.get( 'test.stale' ), 'In store' );
878 } )
879 .then( function () {
880 // Reset run time, but keep mw.loader.store
881 mw.loader.moduleRegistry[ 'test.stale' ].script = undefined;
882 mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered';
883 mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2';
884
885 // Module was stored correctly as v1
886 // On future navigations, it will be ignored until evicted
887 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
888 } );
889 } );
890
891 QUnit.test( 'Stale response caching - backcompat', function ( assert ) {
892 var count = 0;
893 mw.loader.store.enabled = true;
894 mw.loader.register( 'test.stalebc', 'v2' );
895 assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' );
896
897 mw.loader.implement( 'test.stalebc', function () {
898 count++;
899 } );
900
901 return mw.loader.using( 'test.stalebc' )
902 .then( function () {
903 assert.strictEqual( count, 1 );
904 assert.strictEqual( mw.loader.getState( 'test.stalebc' ), 'ready' );
905 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
906 } )
907 .then( function () {
908 // Reset run time, but keep mw.loader.store
909 mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined;
910 mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered';
911 mw.loader.moduleRegistry[ 'test.stalebc' ].version = 'v2';
912
913 // Legacy behaviour is storing under the expected version,
914 // which woudl lead to whitewashing and stale values (T117587).
915 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
916 } );
917 } );
918
919 QUnit.test( 'require()', function ( assert ) {
920 mw.loader.register( [
921 [ 'test.require1', '0' ],
922 [ 'test.require2', '0' ],
923 [ 'test.require3', '0' ],
924 [ 'test.require4', '0', [ 'test.require3' ] ]
925 ] );
926 mw.loader.implement( 'test.require1', function () {} );
927 mw.loader.implement( 'test.require2', function ( $, jQuery, require, module ) {
928 module.exports = 1;
929 } );
930 mw.loader.implement( 'test.require3', function ( $, jQuery, require, module ) {
931 module.exports = function () {
932 return 'hello world';
933 };
934 } );
935 mw.loader.implement( 'test.require4', function ( $, jQuery, require, module ) {
936 var other = require( 'test.require3' );
937 module.exports = {
938 pizza: function () {
939 return other();
940 }
941 };
942 } );
943 return mw.loader.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require ) {
944 var module1, module2, module3, module4;
945
946 module1 = require( 'test.require1' );
947 module2 = require( 'test.require2' );
948 module3 = require( 'test.require3' );
949 module4 = require( 'test.require4' );
950
951 assert.strictEqual( typeof module1, 'object', 'export of module with no export' );
952 assert.strictEqual( module2, 1, 'export a number' );
953 assert.strictEqual( module3(), 'hello world', 'export a function' );
954 assert.strictEqual( typeof module4.pizza, 'function', 'export an object' );
955 assert.strictEqual( module4.pizza(), 'hello world', 'module can require other modules' );
956
957 assert.throws( function () {
958 require( '_badmodule' );
959 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
960 } );
961 } );
962
963 QUnit.test( 'require() in debug mode', function ( assert ) {
964 var path = mw.config.get( 'wgScriptPath' );
965 mw.loader.register( [
966 [ 'test.require.define', '0' ],
967 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
968 ] );
969 mw.loader.implement( 'test.require.callback', [ QUnit.fixurl( path + '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
970 mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
971
972 return mw.loader.using( 'test.require.callback' ).then( function ( require ) {
973 var cb = require( 'test.require.callback' );
974 assert.strictEqual( cb.immediate, 'Defined.', 'module.exports and require work in debug mode' );
975 // Must use try-catch because cb.later() will throw if require is undefined,
976 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
977 try {
978 assert.strictEqual( cb.later(), 'Defined.', 'require works asynchrously in debug mode' );
979 } catch ( e ) {
980 assert.equal( null, String( e ), 'require works asynchrously in debug mode' );
981 }
982 }, function () {
983 assert.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
984 } );
985 } );
986
987 QUnit.test( 'Implicit dependencies', function ( assert ) {
988 var ranUser = false,
989 userSeesSite = false,
990 ranSite = false;
991
992 mw.loader.implement(
993 'site',
994 function () {
995 ranSite = true;
996 }
997 );
998 mw.loader.implement(
999 'user',
1000 function () {
1001 userSeesSite = ranSite;
1002 ranUser = true;
1003 }
1004 );
1005
1006 assert.strictEqual( ranSite, false, 'verify site module not yet loaded' );
1007 assert.strictEqual( ranUser, false, 'verify user module not yet loaded' );
1008 return mw.loader.using( 'user', function () {
1009 assert.strictEqual( ranSite, true, 'ran site module' );
1010 assert.strictEqual( ranUser, true, 'ran user module' );
1011 assert.strictEqual( userSeesSite, true, 'ran site before user module' );
1012
1013 // Reset
1014 mw.loader.moduleRegistry[ 'site' ].state = 'registered';
1015 mw.loader.moduleRegistry[ 'user' ].state = 'registered';
1016 } );
1017 } );
1018
1019 }( mediaWiki, jQuery ) );