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