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