Merge "config: Add new ConfigRepository"
[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( dependencies, [ 'test.module7' ], 'Error callback called with module test.module7' );
660 }
661 );
662 mw.loader.using(
663 [ 'test.module9' ],
664 function () {
665 assert.ok( false, 'Success fired despite missing dependency' );
666 assert.ok( true, 'QUnit expected() count dummy' );
667 },
668 function ( e, dependencies ) {
669 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
670 dependencies.sort();
671 assert.deepEqual(
672 dependencies,
673 [ 'test.module7', 'test.module8', 'test.module9' ],
674 'Error callback called with all three modules as dependencies'
675 );
676 }
677 );
678 } );
679
680 QUnit.test( 'Dependency handling', function ( assert ) {
681 var done = assert.async();
682 mw.loader.register( [
683 // [module, version, dependencies, group, source]
684 [ 'testMissing', '1', [], null, 'testloader' ],
685 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
686 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
687 ] );
688
689 function verifyModuleStates() {
690 assert.strictEqual( mw.loader.getState( 'testMissing' ), 'missing', 'Module "testMissing" state' );
691 assert.strictEqual( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module "testUsesMissing" state' );
692 assert.strictEqual( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module "testUsesNestedMissing" state' );
693 }
694
695 mw.loader.using( [ 'testUsesNestedMissing' ],
696 function () {
697 assert.ok( false, 'Error handler should be invoked.' );
698 assert.ok( true ); // Dummy to reach QUnit expect()
699
700 verifyModuleStates();
701
702 done();
703 },
704 function ( e, badmodules ) {
705 assert.ok( true, 'Error handler should be invoked.' );
706 // As soon as server spits out state('testMissing', 'missing');
707 // it will bubble up and trigger the error callback.
708 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
709 assert.deepEqual( badmodules, [ 'testMissing' ], 'Bad modules as expected.' );
710
711 verifyModuleStates();
712
713 done();
714 }
715 );
716 } );
717
718 QUnit.test( 'Skip-function handling', function ( assert ) {
719 mw.loader.register( [
720 // [module, version, dependencies, group, source, skip]
721 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
722 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
723 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
724 ] );
725
726 return mw.loader.using( [ 'testUsesSkippable' ] ).then(
727 function () {
728 assert.strictEqual( mw.loader.getState( 'testSkipped' ), 'ready', 'Skipped module' );
729 assert.strictEqual( mw.loader.getState( 'testNotSkipped' ), 'ready', 'Regular module' );
730 assert.strictEqual( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Regular module with skippable dependency' );
731 },
732 function ( e, badmodules ) {
733 // Should not fail and QUnit would already catch this,
734 // but add a handler anyway to report details from 'badmodules
735 assert.deepEqual( badmodules, [], 'Bad modules' );
736 }
737 );
738 } );
739
740 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
741 QUnit.test( '.load( "//protocol-relative" ) - T32825', function ( assert ) {
742 var target,
743 done = assert.async();
744
745 // URL to the callback script
746 target = QUnit.fixurl(
747 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
748 );
749 // Ensure a protocol-relative URL for this test
750 target = target.replace( /https?:/, '' );
751 assert.strictEqual( target.slice( 0, 2 ), '//', 'URL is protocol-relative' );
752
753 mw.loader.testCallback = function () {
754 // Ensure once, delete now
755 delete mw.loader.testCallback;
756 assert.ok( true, 'callback' );
757 done();
758 };
759
760 // Go!
761 mw.loader.load( target );
762 } );
763
764 QUnit.test( '.load( "/absolute-path" )', function ( assert ) {
765 var target,
766 done = assert.async();
767
768 // URL to the callback script
769 target = QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
770 assert.strictEqual( target.slice( 0, 1 ), '/', 'URL is relative to document root' );
771
772 mw.loader.testCallback = function () {
773 // Ensure once, delete now
774 delete mw.loader.testCallback;
775 assert.ok( true, 'callback' );
776 done();
777 };
778
779 // Go!
780 mw.loader.load( target );
781 } );
782
783 QUnit.test( 'Empty string module name - T28804', function ( assert ) {
784 var done = false;
785
786 assert.strictEqual( mw.loader.getState( '' ), null, 'State (unregistered)' );
787
788 mw.loader.register( '', 'v1' );
789 assert.strictEqual( mw.loader.getState( '' ), 'registered', 'State (registered)' );
790 assert.strictEqual( mw.loader.getVersion( '' ), 'v1', 'Version' );
791
792 mw.loader.implement( '', function () {
793 done = true;
794 } );
795
796 return mw.loader.using( '', function () {
797 assert.strictEqual( done, true, 'script ran' );
798 assert.strictEqual( mw.loader.getState( '' ), 'ready', 'State (ready)' );
799 } );
800 } );
801
802 QUnit.test( 'Executing race - T112232', function ( assert ) {
803 var done = false;
804
805 // The red herring schedules its CSS buffer first. In T112232, a bug in the
806 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
807 mw.loader.implement(
808 'testRaceRedHerring',
809 function () {},
810 { css: [ '.mw-testRaceRedHerring {}' ] }
811 );
812 mw.loader.implement(
813 'testRaceLoadMe',
814 function () {
815 done = true;
816 },
817 { css: [ '.mw-testRaceLoadMe { float: left; }' ] }
818 );
819
820 mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
821 return mw.loader.using( 'testRaceLoadMe', function () {
822 assert.strictEqual( done, true, 'script ran' );
823 assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' );
824 } );
825 } );
826
827 QUnit.test( 'Stale response caching - T117587', function ( assert ) {
828 var count = 0;
829 mw.loader.store.enabled = true;
830 mw.loader.register( 'test.stale', 'v2' );
831 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
832
833 mw.loader.implement( 'test.stale@v1', function () {
834 count++;
835 } );
836
837 return mw.loader.using( 'test.stale' )
838 .then( function () {
839 assert.strictEqual( count, 1 );
840 // After implementing, registry contains version as implemented by the response.
841 assert.strictEqual( mw.loader.getVersion( 'test.stale' ), 'v1', 'Override version' );
842 assert.strictEqual( mw.loader.getState( 'test.stale' ), 'ready' );
843 assert.ok( mw.loader.store.get( 'test.stale' ), 'In store' );
844 } )
845 .then( function () {
846 // Reset run time, but keep mw.loader.store
847 mw.loader.moduleRegistry[ 'test.stale' ].script = undefined;
848 mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered';
849 mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2';
850
851 // Module was stored correctly as v1
852 // On future navigations, it will be ignored until evicted
853 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
854 } );
855 } );
856
857 QUnit.test( 'Stale response caching - backcompat', function ( assert ) {
858 var script = 0;
859 mw.loader.store.enabled = true;
860 mw.loader.register( 'test.stalebc', 'v2' );
861 assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' );
862
863 mw.loader.implement( 'test.stalebc', function () {
864 script++;
865 } );
866
867 return mw.loader.using( 'test.stalebc' )
868 .then( function () {
869 assert.strictEqual( script, 1, 'module script ran' );
870 assert.strictEqual( mw.loader.getState( 'test.stalebc' ), 'ready' );
871 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
872 } )
873 .then( function () {
874 // Reset run time, but keep mw.loader.store
875 mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined;
876 mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered';
877 mw.loader.moduleRegistry[ 'test.stalebc' ].version = 'v2';
878
879 // Legacy behaviour is storing under the expected version,
880 // which woudl lead to whitewashing and stale values (T117587).
881 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
882 } );
883 } );
884
885 QUnit.test( 'require()', function ( assert ) {
886 mw.loader.register( [
887 [ 'test.require1', '0' ],
888 [ 'test.require2', '0' ],
889 [ 'test.require3', '0' ],
890 [ 'test.require4', '0', [ 'test.require3' ] ]
891 ] );
892 mw.loader.implement( 'test.require1', function () {} );
893 mw.loader.implement( 'test.require2', function ( $, jQuery, require, module ) {
894 module.exports = 1;
895 } );
896 mw.loader.implement( 'test.require3', function ( $, jQuery, require, module ) {
897 module.exports = function () {
898 return 'hello world';
899 };
900 } );
901 mw.loader.implement( 'test.require4', function ( $, jQuery, require, module ) {
902 var other = require( 'test.require3' );
903 module.exports = {
904 pizza: function () {
905 return other();
906 }
907 };
908 } );
909 return mw.loader.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require ) {
910 var module1, module2, module3, module4;
911
912 module1 = require( 'test.require1' );
913 module2 = require( 'test.require2' );
914 module3 = require( 'test.require3' );
915 module4 = require( 'test.require4' );
916
917 assert.strictEqual( typeof module1, 'object', 'export of module with no export' );
918 assert.strictEqual( module2, 1, 'export a number' );
919 assert.strictEqual( module3(), 'hello world', 'export a function' );
920 assert.strictEqual( typeof module4.pizza, 'function', 'export an object' );
921 assert.strictEqual( module4.pizza(), 'hello world', 'module can require other modules' );
922
923 assert.throws( function () {
924 require( '_badmodule' );
925 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
926 } );
927 } );
928
929 QUnit.test( 'require() in debug mode', function ( assert ) {
930 var path = mw.config.get( 'wgScriptPath' );
931 mw.loader.register( [
932 [ 'test.require.define', '0' ],
933 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
934 ] );
935 mw.loader.implement( 'test.require.callback', [ QUnit.fixurl( path + '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
936 mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
937
938 return mw.loader.using( 'test.require.callback' ).then( function ( require ) {
939 var cb = require( 'test.require.callback' );
940 assert.strictEqual( cb.immediate, 'Defined.', 'module.exports and require work in debug mode' );
941 // Must use try-catch because cb.later() will throw if require is undefined,
942 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
943 try {
944 assert.strictEqual( cb.later(), 'Defined.', 'require works asynchrously in debug mode' );
945 } catch ( e ) {
946 assert.strictEqual( String( e ), null, 'require works asynchrously in debug mode' );
947 }
948 } );
949 } );
950
951 QUnit.test( 'Implicit dependencies', function ( assert ) {
952 var user = 0,
953 site = 0,
954 siteFromUser = 0;
955
956 mw.loader.implement(
957 'site',
958 function () {
959 site++;
960 }
961 );
962 mw.loader.implement(
963 'user',
964 function () {
965 user++;
966 siteFromUser = site;
967 }
968 );
969
970 return mw.loader.using( 'user', function () {
971 assert.strictEqual( site, 1, 'site module' );
972 assert.strictEqual( user, 1, 'user module' );
973 assert.strictEqual( siteFromUser, 1, 'site ran before user' );
974 } ).always( function () {
975 // Reset
976 mw.loader.moduleRegistry[ 'site' ].state = 'registered';
977 mw.loader.moduleRegistry[ 'user' ].state = 'registered';
978 } );
979 } );
980
981 }( mediaWiki, jQuery ) );