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