resourceloader: Make doRequest() an inline helper for batchRequest()
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.loader.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki (mw.loader)', QUnit.newMwEnvironment( {
3 setup: function ( assert ) {
4 mw.loader.store.enabled = false;
5
6 // Expose for load.mock.php
7 mw.loader.testFail = function ( reason ) {
8 assert.ok( false, reason );
9 };
10 },
11 teardown: function () {
12 mw.loader.store.enabled = false;
13 // Teardown for StringSet shim test
14 if ( this.nativeSet ) {
15 window.Set = this.nativeSet;
16 mw.redefineFallbacksForTest();
17 }
18 // Remove any remaining temporary statics
19 // exposed for cross-file mocks.
20 if ( 'testCallback' in mw.loader ) {
21 delete mw.loader.testCallback;
22 }
23 if ( 'testFail' in mw.loader ) {
24 delete mw.loader.testFail;
25 }
26 }
27 } ) );
28
29 mw.loader.addSource(
30 'testloader',
31 QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
32 );
33
34 /**
35 * The sync style load test (for @import). This is, in a way, also an open bug for
36 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
37 * way to get a callback from when a stylesheet is loaded (that is, including any
38 * `@import` rules inside). To work around this, we'll have a little time loop to check
39 * if the styles apply.
40 *
41 * Note: This test originally used new Image() and onerror to get a callback
42 * when the url is loaded, but that is fragile since it doesn't monitor the
43 * same request as the css @import, and Safari 4 has issues with
44 * onerror/onload not being fired at all in weird cases like this.
45 */
46 function assertStyleAsync( assert, $element, prop, val, fn ) {
47 var styleTestStart,
48 el = $element.get( 0 ),
49 styleTestTimeout = ( QUnit.config.testTimeout || 5000 ) - 200;
50
51 function isCssImportApplied() {
52 // Trigger reflow, repaint, redraw, whatever (cross-browser)
53 $element.css( 'height' );
54 // eslint-disable-next-line no-unused-expressions
55 el.innerHTML;
56 el.className = el.className;
57 // eslint-disable-next-line no-unused-expressions
58 document.documentElement.clientHeight;
59
60 return $element.css( prop ) === val;
61 }
62
63 function styleTestLoop() {
64 var styleTestSince = new Date().getTime() - styleTestStart;
65 // If it is passing or if we timed out, run the real test and stop the loop
66 if ( isCssImportApplied() || styleTestSince > styleTestTimeout ) {
67 assert.equal( $element.css( prop ), val,
68 'style "' + prop + ': ' + val + '" from url is applied (after ' + styleTestSince + 'ms)'
69 );
70
71 if ( fn ) {
72 fn();
73 }
74
75 return;
76 }
77 // Otherwise, keep polling
78 setTimeout( styleTestLoop );
79 }
80
81 // Start the loop
82 styleTestStart = new Date().getTime();
83 styleTestLoop();
84 }
85
86 function urlStyleTest( selector, prop, val ) {
87 return QUnit.fixurl(
88 mw.config.get( 'wgScriptPath' ) +
89 '/tests/qunit/data/styleTest.css.php?' +
90 $.param( {
91 selector: selector,
92 prop: prop,
93 val: val
94 } )
95 );
96 }
97
98 QUnit.test( 'Basic', function ( assert ) {
99 var isAwesomeDone;
100
101 mw.loader.testCallback = function () {
102 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
103 isAwesomeDone = true;
104 };
105
106 mw.loader.implement( 'test.callback', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
107
108 return mw.loader.using( 'test.callback', function () {
109 assert.strictEqual( isAwesomeDone, true, 'test.callback module should\'ve caused isAwesomeDone to be true' );
110 }, function () {
111 assert.ok( false, 'Error callback fired while loader.using "test.callback" module' );
112 } );
113 } );
114
115 QUnit.test( 'Object method as module name', function ( assert ) {
116 var isAwesomeDone;
117
118 mw.loader.testCallback = function () {
119 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module hasOwnProperty: isAwesomeDone should still be undefined' );
120 isAwesomeDone = true;
121 };
122
123 mw.loader.implement( 'hasOwnProperty', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ], {}, {} );
124
125 return mw.loader.using( 'hasOwnProperty', function () {
126 assert.strictEqual( isAwesomeDone, true, 'hasOwnProperty module should\'ve caused isAwesomeDone to be true' );
127 }, function () {
128 assert.ok( false, 'Error callback fired while loader.using "hasOwnProperty" module' );
129 } );
130 } );
131
132 QUnit.test( '.using( .. ) Promise', function ( assert ) {
133 var isAwesomeDone;
134
135 mw.loader.testCallback = function () {
136 assert.strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined' );
137 isAwesomeDone = true;
138 };
139
140 mw.loader.implement( 'test.promise', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
141
142 return mw.loader.using( 'test.promise' )
143 .done( function () {
144 assert.strictEqual( isAwesomeDone, true, 'test.promise module should\'ve caused isAwesomeDone to be true' );
145 } )
146 .fail( function () {
147 assert.ok( false, 'Error callback fired while loader.using "test.promise" module' );
148 } );
149 } );
150
151 // Covers mw.loader#sortDependencies (with native Set if available)
152 QUnit.test( '.using() Error: Circular dependency [StringSet default]', function ( assert ) {
153 var done = assert.async();
154
155 mw.loader.register( [
156 [ 'test.circle1', '0', [ 'test.circle2' ] ],
157 [ 'test.circle2', '0', [ 'test.circle3' ] ],
158 [ 'test.circle3', '0', [ 'test.circle1' ] ]
159 ] );
160 mw.loader.using( 'test.circle3' ).then(
161 function done() {
162 assert.ok( false, 'Unexpected resolution, expected error.' );
163 },
164 function fail( e ) {
165 assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
166 }
167 )
168 .always( done );
169 } );
170
171 // @covers mw.loader#sortDependencies (with fallback shim)
172 QUnit.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert ) {
173 var done = assert.async();
174
175 if ( !window.Set ) {
176 assert.expect( 0 );
177 done();
178 return;
179 }
180
181 this.nativeSet = window.Set;
182 window.Set = undefined;
183 mw.redefineFallbacksForTest();
184
185 mw.loader.register( [
186 [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
187 [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
188 [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
189 ] );
190 mw.loader.using( 'test.shim.circle3' ).then(
191 function done() {
192 assert.ok( false, 'Unexpected resolution, expected error.' );
193 },
194 function fail( e ) {
195 assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
196 }
197 )
198 .always( done );
199 } );
200
201 QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
202 var capture = [];
203 mw.loader.register( [
204 [ 'test.circleA', '0', [ 'test.circleB' ] ],
205 [ 'test.circleB', '0', [ 'test.circleC' ] ],
206 [ 'test.circleC', '0', [ 'test.circleA' ] ]
207 ] );
208 this.sandbox.stub( mw, 'track', function ( topic, data ) {
209 capture.push( {
210 topic: topic,
211 error: data.exception && data.exception.message,
212 source: data.source
213 } );
214 } );
215
216 mw.loader.load( 'test.circleC' );
217 assert.deepEqual(
218 [ {
219 topic: 'resourceloader.exception',
220 error: 'Circular reference detected: test.circleB -> test.circleC',
221 source: 'resolve'
222 } ],
223 capture,
224 'Detect circular dependency'
225 );
226 } );
227
228 QUnit.test( '.using() - Error: Unregistered', function ( assert ) {
229 var done = assert.async();
230
231 mw.loader.using( 'test.using.unreg' ).then(
232 function done() {
233 assert.ok( false, 'Unexpected resolution, expected error.' );
234 },
235 function fail( e ) {
236 assert.ok( /Unknown/.test( String( e ) ), 'Detect unknown dependency' );
237 }
238 ).always( done );
239 } );
240
241 QUnit.test( '.load() - Error: Unregistered', function ( assert ) {
242 var capture = [];
243 this.sandbox.stub( mw, 'track', function ( topic, data ) {
244 capture.push( {
245 topic: topic,
246 error: data.exception && data.exception.message,
247 source: data.source
248 } );
249 } );
250
251 mw.loader.load( 'test.load.unreg' );
252 assert.deepEqual(
253 [ {
254 topic: 'resourceloader.exception',
255 error: 'Unknown dependency: test.load.unreg',
256 source: 'resolve'
257 } ],
258 capture
259 );
260 } );
261
262 // Regression test for T36853
263 QUnit.test( '.load() - Error: Missing dependency', function ( assert ) {
264 var capture = [];
265 this.sandbox.stub( mw, 'track', function ( topic, data ) {
266 capture.push( {
267 topic: topic,
268 error: data.exception && data.exception.message,
269 source: data.source
270 } );
271 } );
272
273 mw.loader.register( [
274 [ 'test.load.missingdep1', '0', [ 'test.load.missingdep2' ] ],
275 [ 'test.load.missingdep', '0', [ 'test.load.missingdep1' ] ]
276 ] );
277 mw.loader.load( 'test.load.missingdep' );
278 assert.deepEqual(
279 [ {
280 topic: 'resourceloader.exception',
281 error: 'Unknown dependency: test.load.missingdep2',
282 source: 'resolve'
283 } ],
284 capture
285 );
286 } );
287
288 QUnit.test( '.implement( styles={ "css": [text, ..] } )', function ( assert ) {
289 var $element = $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
290
291 assert.notEqual(
292 $element.css( 'float' ),
293 'right',
294 'style is clear'
295 );
296
297 mw.loader.implement(
298 'test.implement.a',
299 function () {
300 assert.equal(
301 $element.css( 'float' ),
302 'right',
303 'style is applied'
304 );
305 },
306 {
307 all: '.mw-test-implement-a { float: right; }'
308 }
309 );
310
311 return mw.loader.using( 'test.implement.a' );
312 } );
313
314 QUnit.test( '.implement( styles={ "url": { <media>: [url, ..] } } )', function ( assert ) {
315 var $element1 = $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
316 $element2 = $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
317 $element3 = $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' ),
318 done = assert.async();
319
320 assert.notEqual(
321 $element1.css( 'text-align' ),
322 'center',
323 'style is clear'
324 );
325 assert.notEqual(
326 $element2.css( 'float' ),
327 'left',
328 'style is clear'
329 );
330 assert.notEqual(
331 $element3.css( 'text-align' ),
332 'right',
333 'style is clear'
334 );
335
336 mw.loader.implement(
337 'test.implement.b',
338 function () {
339 // Note: done() must only be called when the entire test is
340 // complete. So, make sure that we don't start until *both*
341 // assertStyleAsync calls have completed.
342 var pending = 2;
343 assertStyleAsync( assert, $element2, 'float', 'left', function () {
344 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
345
346 pending--;
347 if ( pending === 0 ) {
348 done();
349 }
350 } );
351 assertStyleAsync( assert, $element3, 'float', 'right', function () {
352 assert.notEqual( $element1.css( 'text-align' ), 'center', 'print style is not applied' );
353
354 pending--;
355 if ( pending === 0 ) {
356 done();
357 }
358 } );
359 },
360 {
361 url: {
362 print: [ urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' ) ],
363 screen: [
364 // T42834: Make sure it actually works with more than 1 stylesheet reference
365 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
366 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
367 ]
368 }
369 }
370 );
371
372 mw.loader.load( 'test.implement.b' );
373 } );
374
375 // Backwards compatibility
376 QUnit.test( '.implement( styles={ <media>: text } ) (back-compat)', function ( assert ) {
377 var $element = $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
378
379 assert.notEqual(
380 $element.css( 'float' ),
381 'right',
382 'style is clear'
383 );
384
385 mw.loader.implement(
386 'test.implement.c',
387 function () {
388 assert.equal(
389 $element.css( 'float' ),
390 'right',
391 'style is applied'
392 );
393 },
394 {
395 all: '.mw-test-implement-c { float: right; }'
396 }
397 );
398
399 return mw.loader.using( 'test.implement.c' );
400 } );
401
402 // Backwards compatibility
403 QUnit.test( '.implement( styles={ <media>: [url, ..] } ) (back-compat)', function ( assert ) {
404 var $element = $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
405 $element2 = $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' ),
406 done = assert.async();
407
408 assert.notEqual(
409 $element.css( 'float' ),
410 'right',
411 'style is clear'
412 );
413 assert.notEqual(
414 $element2.css( 'text-align' ),
415 'center',
416 'style is clear'
417 );
418
419 mw.loader.implement(
420 'test.implement.d',
421 function () {
422 assertStyleAsync( assert, $element, 'float', 'right', function () {
423 assert.notEqual( $element2.css( 'text-align' ), 'center', 'print style is not applied (T42500)' );
424 done();
425 } );
426 },
427 {
428 all: [ urlStyleTest( '.mw-test-implement-d', 'float', 'right' ) ],
429 print: [ urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' ) ]
430 }
431 );
432
433 mw.loader.load( 'test.implement.d' );
434 } );
435
436 // @import (T33676)
437 QUnit.test( '.implement( styles has @import )', function ( assert ) {
438 var isJsExecuted, $element,
439 done = assert.async();
440
441 mw.loader.implement(
442 'test.implement.import',
443 function () {
444 assert.strictEqual( isJsExecuted, undefined, 'script not executed multiple times' );
445 isJsExecuted = true;
446
447 assert.equal( mw.loader.getState( 'test.implement.import' ), 'executing', 'module state during implement() script execution' );
448
449 $element = $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
450
451 assert.equal( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'messages load before script execution' );
452
453 assertStyleAsync( assert, $element, 'float', 'right', function () {
454 assert.equal( $element.css( 'text-align' ), 'center',
455 'CSS styles after the @import rule are working'
456 );
457
458 done();
459 } );
460 },
461 {
462 css: [
463 '@import url(\''
464 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
465 + '\');\n'
466 + '.mw-test-implement-import { text-align: center; }'
467 ]
468 },
469 {
470 'test-foobar': 'Hello Foobar, $1!'
471 }
472 );
473
474 mw.loader.using( 'test.implement.import' ).always( function () {
475 assert.strictEqual( isJsExecuted, true, 'script executed' );
476 assert.equal( mw.loader.getState( 'test.implement.import' ), 'ready', 'module state after script execution' );
477 } );
478 } );
479
480 QUnit.test( '.implement( dependency with styles )', function ( assert ) {
481 var $element = $( '<div class="mw-test-implement-e"></div>' ).appendTo( '#qunit-fixture' ),
482 $element2 = $( '<div class="mw-test-implement-e2"></div>' ).appendTo( '#qunit-fixture' );
483
484 assert.notEqual(
485 $element.css( 'float' ),
486 'right',
487 'style is clear'
488 );
489 assert.notEqual(
490 $element2.css( 'float' ),
491 'left',
492 'style is clear'
493 );
494
495 mw.loader.register( [
496 [ 'test.implement.e', '0', [ 'test.implement.e2' ] ],
497 [ 'test.implement.e2', '0' ]
498 ] );
499
500 mw.loader.implement(
501 'test.implement.e',
502 function () {
503 assert.equal(
504 $element.css( 'float' ),
505 'right',
506 'Depending module\'s style is applied'
507 );
508 },
509 {
510 all: '.mw-test-implement-e { float: right; }'
511 }
512 );
513
514 mw.loader.implement(
515 'test.implement.e2',
516 function () {
517 assert.equal(
518 $element2.css( 'float' ),
519 'left',
520 'Dependency\'s style is applied'
521 );
522 },
523 {
524 all: '.mw-test-implement-e2 { float: left; }'
525 }
526 );
527
528 return mw.loader.using( 'test.implement.e' );
529 } );
530
531 QUnit.test( '.implement( only scripts )', function ( assert ) {
532 mw.loader.implement( 'test.onlyscripts', function () {} );
533 assert.strictEqual( mw.loader.getState( 'test.onlyscripts' ), 'ready' );
534 } );
535
536 QUnit.test( '.implement( only messages )', function ( assert ) {
537 assert.assertFalse( mw.messages.exists( 'T31107' ), 'Verify that the test message doesn\'t exist yet' );
538
539 mw.loader.implement( 'test.implement.msgs', [], {}, { T31107: 'loaded' } );
540
541 return mw.loader.using( 'test.implement.msgs', function () {
542 assert.ok( mw.messages.exists( 'T31107' ), 'T31107: messages-only module should implement ok' );
543 }, function () {
544 assert.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
545 } );
546 } );
547
548 QUnit.test( '.implement( empty )', function ( assert ) {
549 mw.loader.implement( 'test.empty' );
550 assert.strictEqual( mw.loader.getState( 'test.empty' ), 'ready' );
551 } );
552
553 // @covers mw.loader#batchRequest
554 // This is a regression test because in the past we called getCombinedVersion()
555 // for all requested modules, before url splitting took place.
556 // Discovered as part of T188076, but not directly related.
557 QUnit.test( 'Url composition (modules considered for version)', function ( assert ) {
558 mw.loader.register( [
559 // [module, version, dependencies, group, source]
560 [ 'testUrlInc', 'url', [], null, 'testloader' ],
561 [ 'testUrlIncDump', 'dump', [], null, 'testloader' ]
562 ] );
563
564 mw.config.set( 'wgResourceLoaderMaxQueryLength', 10 );
565
566 return mw.loader.using( [ 'testUrlIncDump', 'testUrlInc' ] ).then( function ( require ) {
567 assert.propEqual(
568 require( 'testUrlIncDump' ).query,
569 {
570 modules: 'testUrlIncDump',
571 // Expected: Wrapped hash just for this one module
572 // $hash = hash( 'fnv132', 'dump');
573 // base_convert( $hash, 16, 36 ); // "13e9zzn"
574 // Previously: Wrapped hash for both modules, despite being in separate requests
575 // $hash = hash( 'fnv132', 'urldump' );
576 // base_convert( $hash, 16, 36 ); // "18kz9ca"
577 version: '13e9zzn'
578 },
579 'Query parameters'
580 );
581
582 assert.strictEqual( mw.loader.getState( 'testUrlInc' ), 'ready', 'testUrlInc also loaded' );
583 } );
584 } );
585
586 QUnit.test( 'Broken indirect dependency', function ( assert ) {
587 // don't emit an error event
588 this.sandbox.stub( mw, 'track' );
589
590 mw.loader.register( [
591 [ 'test.module1', '0' ],
592 [ 'test.module2', '0', [ 'test.module1' ] ],
593 [ 'test.module3', '0', [ 'test.module2' ] ]
594 ] );
595 mw.loader.implement( 'test.module1', function () {
596 throw new Error( 'expected' );
597 }, {}, {} );
598 assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
599 assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
600 assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
601
602 assert.strictEqual( mw.track.callCount, 1 );
603 } );
604
605 QUnit.test( 'Out-of-order implementation', function ( assert ) {
606 mw.loader.register( [
607 [ 'test.module4', '0' ],
608 [ 'test.module5', '0', [ 'test.module4' ] ],
609 [ 'test.module6', '0', [ 'test.module5' ] ]
610 ] );
611 mw.loader.implement( 'test.module4', function () {} );
612 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
613 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
614 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
615 mw.loader.implement( 'test.module6', function () {} );
616 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
617 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
618 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
619 mw.loader.implement( 'test.module5', function () {} );
620 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
621 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
622 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
623 } );
624
625 QUnit.test( 'Missing dependency', function ( assert ) {
626 mw.loader.register( [
627 [ 'test.module7', '0' ],
628 [ 'test.module8', '0', [ 'test.module7' ] ],
629 [ 'test.module9', '0', [ 'test.module8' ] ]
630 ] );
631 mw.loader.implement( 'test.module8', function () {} );
632 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
633 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
634 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
635 mw.loader.state( 'test.module7', 'missing' );
636 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
637 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
638 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
639 mw.loader.implement( 'test.module9', function () {} );
640 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
641 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
642 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
643 mw.loader.using(
644 [ 'test.module7' ],
645 function () {
646 assert.ok( false, 'Success fired despite missing dependency' );
647 assert.ok( true, 'QUnit expected() count dummy' );
648 },
649 function ( e, dependencies ) {
650 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
651 assert.deepEqual( dependencies, [ 'test.module7' ], 'Error callback called with module test.module7' );
652 }
653 );
654 mw.loader.using(
655 [ 'test.module9' ],
656 function () {
657 assert.ok( false, 'Success fired despite missing dependency' );
658 assert.ok( true, 'QUnit expected() count dummy' );
659 },
660 function ( e, dependencies ) {
661 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
662 dependencies.sort();
663 assert.deepEqual(
664 dependencies,
665 [ 'test.module7', 'test.module8', 'test.module9' ],
666 'Error callback called with all three modules as dependencies'
667 );
668 }
669 );
670 } );
671
672 QUnit.test( 'Dependency handling', function ( assert ) {
673 var done = assert.async();
674 mw.loader.register( [
675 // [module, version, dependencies, group, source]
676 [ 'testMissing', '1', [], null, 'testloader' ],
677 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
678 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
679 ] );
680
681 function verifyModuleStates() {
682 assert.equal( mw.loader.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
683 assert.equal( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
684 assert.equal( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
685 }
686
687 mw.loader.using( [ 'testUsesNestedMissing' ],
688 function () {
689 assert.ok( false, 'Error handler should be invoked.' );
690 assert.ok( true ); // Dummy to reach QUnit expect()
691
692 verifyModuleStates();
693
694 done();
695 },
696 function ( e, badmodules ) {
697 assert.ok( true, 'Error handler should be invoked.' );
698 // As soon as server spits out state('testMissing', 'missing');
699 // it will bubble up and trigger the error callback.
700 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
701 assert.deepEqual( badmodules, [ 'testMissing' ], 'Bad modules as expected.' );
702
703 verifyModuleStates();
704
705 done();
706 }
707 );
708 } );
709
710 QUnit.test( 'Skip-function handling', function ( assert ) {
711 mw.loader.register( [
712 // [module, version, dependencies, group, source, skip]
713 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
714 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
715 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
716 ] );
717
718 function verifyModuleStates() {
719 assert.equal( mw.loader.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
720 assert.equal( mw.loader.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
721 assert.equal( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
722 }
723
724 return mw.loader.using( [ 'testUsesSkippable' ],
725 function () {
726 assert.ok( true, 'Success handler should be invoked.' );
727 assert.ok( true ); // Dummy to match error handler and reach QUnit expect()
728
729 verifyModuleStates();
730 },
731 function ( e, badmodules ) {
732 assert.ok( false, 'Error handler should not be invoked.' );
733 assert.deepEqual( badmodules, [], 'Bad modules as expected.' );
734
735 verifyModuleStates();
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.equal( 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.equal( 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 count = 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 count++;
865 } );
866
867 return mw.loader.using( 'test.stalebc' )
868 .then( function () {
869 assert.strictEqual( count, 1 );
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.equal( null, String( e ), 'require works asynchrously in debug mode' );
947 }
948 }, function () {
949 assert.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
950 } );
951 } );
952
953 QUnit.test( 'Implicit dependencies', function ( assert ) {
954 var ranUser = false,
955 userSeesSite = false,
956 ranSite = false;
957
958 mw.loader.implement(
959 'site',
960 function () {
961 ranSite = true;
962 }
963 );
964 mw.loader.implement(
965 'user',
966 function () {
967 userSeesSite = ranSite;
968 ranUser = true;
969 }
970 );
971
972 assert.strictEqual( ranSite, false, 'verify site module not yet loaded' );
973 assert.strictEqual( ranUser, false, 'verify user module not yet loaded' );
974 return mw.loader.using( 'user', function () {
975 assert.strictEqual( ranSite, true, 'ran site module' );
976 assert.strictEqual( ranUser, true, 'ran user module' );
977 assert.strictEqual( userSeesSite, true, 'ran site before user module' );
978
979 // Reset
980 mw.loader.moduleRegistry[ 'site' ].state = 'registered';
981 mw.loader.moduleRegistry[ 'user' ].state = 'registered';
982 } );
983 } );
984
985 }( mediaWiki, jQuery ) );