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