resourceloader: Fix broken code in load.php mock used to make a test fail
[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 QUnit.test( 'Broken indirect dependency', function ( assert ) {
554 // don't emit an error event
555 this.sandbox.stub( mw, 'track' );
556
557 mw.loader.register( [
558 [ 'test.module1', '0' ],
559 [ 'test.module2', '0', [ 'test.module1' ] ],
560 [ 'test.module3', '0', [ 'test.module2' ] ]
561 ] );
562 mw.loader.implement( 'test.module1', function () {
563 throw new Error( 'expected' );
564 }, {}, {} );
565 assert.strictEqual( mw.loader.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
566 assert.strictEqual( mw.loader.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
567 assert.strictEqual( mw.loader.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
568
569 assert.strictEqual( mw.track.callCount, 1 );
570 } );
571
572 QUnit.test( 'Out-of-order implementation', function ( assert ) {
573 mw.loader.register( [
574 [ 'test.module4', '0' ],
575 [ 'test.module5', '0', [ 'test.module4' ] ],
576 [ 'test.module6', '0', [ 'test.module5' ] ]
577 ] );
578 mw.loader.implement( 'test.module4', function () {} );
579 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
580 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
581 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
582 mw.loader.implement( 'test.module6', function () {} );
583 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
584 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
585 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
586 mw.loader.implement( 'test.module5', function () {} );
587 assert.strictEqual( mw.loader.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
588 assert.strictEqual( mw.loader.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
589 assert.strictEqual( mw.loader.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
590 } );
591
592 QUnit.test( 'Missing dependency', function ( assert ) {
593 mw.loader.register( [
594 [ 'test.module7', '0' ],
595 [ 'test.module8', '0', [ 'test.module7' ] ],
596 [ 'test.module9', '0', [ 'test.module8' ] ]
597 ] );
598 mw.loader.implement( 'test.module8', function () {} );
599 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
600 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
601 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
602 mw.loader.state( 'test.module7', 'missing' );
603 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
604 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
605 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
606 mw.loader.implement( 'test.module9', function () {} );
607 assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
608 assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
609 assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
610 mw.loader.using(
611 [ 'test.module7' ],
612 function () {
613 assert.ok( false, 'Success fired despite missing dependency' );
614 assert.ok( true, 'QUnit expected() count dummy' );
615 },
616 function ( e, dependencies ) {
617 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
618 assert.deepEqual( dependencies, [ 'test.module7' ], 'Error callback called with module test.module7' );
619 }
620 );
621 mw.loader.using(
622 [ 'test.module9' ],
623 function () {
624 assert.ok( false, 'Success fired despite missing dependency' );
625 assert.ok( true, 'QUnit expected() count dummy' );
626 },
627 function ( e, dependencies ) {
628 assert.strictEqual( Array.isArray( dependencies ), true, 'Expected array of dependencies' );
629 dependencies.sort();
630 assert.deepEqual(
631 dependencies,
632 [ 'test.module7', 'test.module8', 'test.module9' ],
633 'Error callback called with all three modules as dependencies'
634 );
635 }
636 );
637 } );
638
639 QUnit.test( 'Dependency handling', function ( assert ) {
640 var done = assert.async();
641 mw.loader.register( [
642 // [module, version, dependencies, group, source]
643 [ 'testMissing', '1', [], null, 'testloader' ],
644 [ 'testUsesMissing', '1', [ 'testMissing' ], null, 'testloader' ],
645 [ 'testUsesNestedMissing', '1', [ 'testUsesMissing' ], null, 'testloader' ]
646 ] );
647
648 function verifyModuleStates() {
649 assert.equal( mw.loader.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
650 assert.equal( mw.loader.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
651 assert.equal( mw.loader.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
652 }
653
654 mw.loader.using( [ 'testUsesNestedMissing' ],
655 function () {
656 assert.ok( false, 'Error handler should be invoked.' );
657 assert.ok( true ); // Dummy to reach QUnit expect()
658
659 verifyModuleStates();
660
661 done();
662 },
663 function ( e, badmodules ) {
664 assert.ok( true, 'Error handler should be invoked.' );
665 // As soon as server spits out state('testMissing', 'missing');
666 // it will bubble up and trigger the error callback.
667 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
668 assert.deepEqual( badmodules, [ 'testMissing' ], 'Bad modules as expected.' );
669
670 verifyModuleStates();
671
672 done();
673 }
674 );
675 } );
676
677 QUnit.test( 'Skip-function handling', function ( assert ) {
678 mw.loader.register( [
679 // [module, version, dependencies, group, source, skip]
680 [ 'testSkipped', '1', [], null, 'testloader', 'return true;' ],
681 [ 'testNotSkipped', '1', [], null, 'testloader', 'return false;' ],
682 [ 'testUsesSkippable', '1', [ 'testSkipped', 'testNotSkipped' ], null, 'testloader' ]
683 ] );
684
685 function verifyModuleStates() {
686 assert.equal( mw.loader.getState( 'testSkipped' ), 'ready', 'Module is ready when skipped' );
687 assert.equal( mw.loader.getState( 'testNotSkipped' ), 'ready', 'Module is ready when not skipped but loaded' );
688 assert.equal( mw.loader.getState( 'testUsesSkippable' ), 'ready', 'Module is ready when skippable dependencies are ready' );
689 }
690
691 return mw.loader.using( [ 'testUsesSkippable' ],
692 function () {
693 assert.ok( true, 'Success handler should be invoked.' );
694 assert.ok( true ); // Dummy to match error handler and reach QUnit expect()
695
696 verifyModuleStates();
697 },
698 function ( e, badmodules ) {
699 assert.ok( false, 'Error handler should not be invoked.' );
700 assert.deepEqual( badmodules, [], 'Bad modules as expected.' );
701
702 verifyModuleStates();
703 }
704 );
705 } );
706
707 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
708 QUnit.test( '.load( "//protocol-relative" ) - T32825', function ( assert ) {
709 var target,
710 done = assert.async();
711
712 // URL to the callback script
713 target = QUnit.fixurl(
714 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js'
715 );
716 // Ensure a protocol-relative URL for this test
717 target = target.replace( /https?:/, '' );
718 assert.equal( target.slice( 0, 2 ), '//', 'URL is protocol-relative' );
719
720 mw.loader.testCallback = function () {
721 // Ensure once, delete now
722 delete mw.loader.testCallback;
723 assert.ok( true, 'callback' );
724 done();
725 };
726
727 // Go!
728 mw.loader.load( target );
729 } );
730
731 QUnit.test( '.load( "/absolute-path" )', function ( assert ) {
732 var target,
733 done = assert.async();
734
735 // URL to the callback script
736 target = QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' );
737 assert.equal( target.slice( 0, 1 ), '/', 'URL is relative to document root' );
738
739 mw.loader.testCallback = function () {
740 // Ensure once, delete now
741 delete mw.loader.testCallback;
742 assert.ok( true, 'callback' );
743 done();
744 };
745
746 // Go!
747 mw.loader.load( target );
748 } );
749
750 QUnit.test( 'Empty string module name - T28804', function ( assert ) {
751 var done = false;
752
753 assert.strictEqual( mw.loader.getState( '' ), null, 'State (unregistered)' );
754
755 mw.loader.register( '', 'v1' );
756 assert.strictEqual( mw.loader.getState( '' ), 'registered', 'State (registered)' );
757 assert.strictEqual( mw.loader.getVersion( '' ), 'v1', 'Version' );
758
759 mw.loader.implement( '', function () {
760 done = true;
761 } );
762
763 return mw.loader.using( '', function () {
764 assert.strictEqual( done, true, 'script ran' );
765 assert.strictEqual( mw.loader.getState( '' ), 'ready', 'State (ready)' );
766 } );
767 } );
768
769 QUnit.test( 'Executing race - T112232', function ( assert ) {
770 var done = false;
771
772 // The red herring schedules its CSS buffer first. In T112232, a bug in the
773 // state machine would cause the job for testRaceLoadMe to run with an earlier job.
774 mw.loader.implement(
775 'testRaceRedHerring',
776 function () {},
777 { css: [ '.mw-testRaceRedHerring {}' ] }
778 );
779 mw.loader.implement(
780 'testRaceLoadMe',
781 function () {
782 done = true;
783 },
784 { css: [ '.mw-testRaceLoadMe { float: left; }' ] }
785 );
786
787 mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
788 return mw.loader.using( 'testRaceLoadMe', function () {
789 assert.strictEqual( done, true, 'script ran' );
790 assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' );
791 } );
792 } );
793
794 QUnit.test( 'Stale response caching - T117587', function ( assert ) {
795 var count = 0;
796 mw.loader.store.enabled = true;
797 mw.loader.register( 'test.stale', 'v2' );
798 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
799
800 mw.loader.implement( 'test.stale@v1', function () {
801 count++;
802 } );
803
804 return mw.loader.using( 'test.stale' )
805 .then( function () {
806 assert.strictEqual( count, 1 );
807 // After implementing, registry contains version as implemented by the response.
808 assert.strictEqual( mw.loader.getVersion( 'test.stale' ), 'v1', 'Override version' );
809 assert.strictEqual( mw.loader.getState( 'test.stale' ), 'ready' );
810 assert.ok( mw.loader.store.get( 'test.stale' ), 'In store' );
811 } )
812 .then( function () {
813 // Reset run time, but keep mw.loader.store
814 mw.loader.moduleRegistry[ 'test.stale' ].script = undefined;
815 mw.loader.moduleRegistry[ 'test.stale' ].state = 'registered';
816 mw.loader.moduleRegistry[ 'test.stale' ].version = 'v2';
817
818 // Module was stored correctly as v1
819 // On future navigations, it will be ignored until evicted
820 assert.strictEqual( mw.loader.store.get( 'test.stale' ), false, 'Not in store' );
821 } );
822 } );
823
824 QUnit.test( 'Stale response caching - backcompat', function ( assert ) {
825 var count = 0;
826 mw.loader.store.enabled = true;
827 mw.loader.register( 'test.stalebc', 'v2' );
828 assert.strictEqual( mw.loader.store.get( 'test.stalebc' ), false, 'Not in store' );
829
830 mw.loader.implement( 'test.stalebc', function () {
831 count++;
832 } );
833
834 return mw.loader.using( 'test.stalebc' )
835 .then( function () {
836 assert.strictEqual( count, 1 );
837 assert.strictEqual( mw.loader.getState( 'test.stalebc' ), 'ready' );
838 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
839 } )
840 .then( function () {
841 // Reset run time, but keep mw.loader.store
842 mw.loader.moduleRegistry[ 'test.stalebc' ].script = undefined;
843 mw.loader.moduleRegistry[ 'test.stalebc' ].state = 'registered';
844 mw.loader.moduleRegistry[ 'test.stalebc' ].version = 'v2';
845
846 // Legacy behaviour is storing under the expected version,
847 // which woudl lead to whitewashing and stale values (T117587).
848 assert.ok( mw.loader.store.get( 'test.stalebc' ), 'In store' );
849 } );
850 } );
851
852 QUnit.test( 'require()', function ( assert ) {
853 mw.loader.register( [
854 [ 'test.require1', '0' ],
855 [ 'test.require2', '0' ],
856 [ 'test.require3', '0' ],
857 [ 'test.require4', '0', [ 'test.require3' ] ]
858 ] );
859 mw.loader.implement( 'test.require1', function () {} );
860 mw.loader.implement( 'test.require2', function ( $, jQuery, require, module ) {
861 module.exports = 1;
862 } );
863 mw.loader.implement( 'test.require3', function ( $, jQuery, require, module ) {
864 module.exports = function () {
865 return 'hello world';
866 };
867 } );
868 mw.loader.implement( 'test.require4', function ( $, jQuery, require, module ) {
869 var other = require( 'test.require3' );
870 module.exports = {
871 pizza: function () {
872 return other();
873 }
874 };
875 } );
876 return mw.loader.using( [ 'test.require1', 'test.require2', 'test.require3', 'test.require4' ] ).then( function ( require ) {
877 var module1, module2, module3, module4;
878
879 module1 = require( 'test.require1' );
880 module2 = require( 'test.require2' );
881 module3 = require( 'test.require3' );
882 module4 = require( 'test.require4' );
883
884 assert.strictEqual( typeof module1, 'object', 'export of module with no export' );
885 assert.strictEqual( module2, 1, 'export a number' );
886 assert.strictEqual( module3(), 'hello world', 'export a function' );
887 assert.strictEqual( typeof module4.pizza, 'function', 'export an object' );
888 assert.strictEqual( module4.pizza(), 'hello world', 'module can require other modules' );
889
890 assert.throws( function () {
891 require( '_badmodule' );
892 }, /is not loaded/, 'Requesting non-existent modules throws error.' );
893 } );
894 } );
895
896 QUnit.test( 'require() in debug mode', function ( assert ) {
897 var path = mw.config.get( 'wgScriptPath' );
898 mw.loader.register( [
899 [ 'test.require.define', '0' ],
900 [ 'test.require.callback', '0', [ 'test.require.define' ] ]
901 ] );
902 mw.loader.implement( 'test.require.callback', [ QUnit.fixurl( path + '/tests/qunit/data/requireCallMwLoaderTestCallback.js' ) ] );
903 mw.loader.implement( 'test.require.define', [ QUnit.fixurl( path + '/tests/qunit/data/defineCallMwLoaderTestCallback.js' ) ] );
904
905 return mw.loader.using( 'test.require.callback' ).then( function ( require ) {
906 var cb = require( 'test.require.callback' );
907 assert.strictEqual( cb.immediate, 'Defined.', 'module.exports and require work in debug mode' );
908 // Must use try-catch because cb.later() will throw if require is undefined,
909 // which doesn't work well inside Deferred.then() when using jQuery 1.x with QUnit
910 try {
911 assert.strictEqual( cb.later(), 'Defined.', 'require works asynchrously in debug mode' );
912 } catch ( e ) {
913 assert.equal( null, String( e ), 'require works asynchrously in debug mode' );
914 }
915 }, function () {
916 assert.ok( false, 'Error callback fired while loader.using "test.require.callback" module' );
917 } );
918 } );
919
920 QUnit.test( 'Implicit dependencies', function ( assert ) {
921 var ranUser = false,
922 userSeesSite = false,
923 ranSite = false;
924
925 mw.loader.implement(
926 'site',
927 function () {
928 ranSite = true;
929 }
930 );
931 mw.loader.implement(
932 'user',
933 function () {
934 userSeesSite = ranSite;
935 ranUser = true;
936 }
937 );
938
939 assert.strictEqual( ranSite, false, 'verify site module not yet loaded' );
940 assert.strictEqual( ranUser, false, 'verify user module not yet loaded' );
941 return mw.loader.using( 'user', function () {
942 assert.strictEqual( ranSite, true, 'ran site module' );
943 assert.strictEqual( ranUser, true, 'ran user module' );
944 assert.strictEqual( userSeesSite, true, 'ran site before user module' );
945
946 // Reset
947 mw.loader.moduleRegistry[ 'site' ].state = 'registered';
948 mw.loader.moduleRegistry[ 'user' ].state = 'registered';
949 } );
950 } );
951
952 }( mediaWiki, jQuery ) );