resourceloader: Deprecate mw.Map
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 /*global CompletenessTest, sinon */
2 ( function ( $, mw, QUnit ) {
3 'use strict';
4
5 var mwTestIgnore, mwTester, addons;
6
7 /**
8 * Add bogus to url to prevent IE crazy caching
9 *
10 * @param {string} value a relative path (eg. 'data/foo.js'
11 * or 'data/test.php?foo=bar').
12 * @return {string} Such as 'data/foo.js?131031765087663960'
13 */
14 QUnit.fixurl = function ( value ) {
15 return value + ( /\?/.test( value ) ? '&' : '?' )
16 + String( new Date().getTime() )
17 + String( parseInt( Math.random() * 100000, 10 ) );
18 };
19
20 /**
21 * Configuration
22 */
23
24 // For each test() that is asynchronous, allow this time to pass before
25 // killing the test and assuming timeout failure.
26 QUnit.config.testTimeout = 60 * 1000;
27
28 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
29 QUnit.config.urlConfig.push( {
30 id: 'debug',
31 label: 'Enable ResourceLoaderDebug',
32 tooltip: 'Enable debug mode in ResourceLoader',
33 value: 'true'
34 } );
35
36 /**
37 * CompletenessTest
38 *
39 * Adds toggle checkbox to header
40 */
41 QUnit.config.urlConfig.push( {
42 id: 'completenesstest',
43 label: 'Run CompletenessTest',
44 tooltip: 'Run the completeness test'
45 } );
46
47 /**
48 * SinonJS
49 *
50 * Glue code for nicer integration with QUnit setup/teardown
51 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
52 * Fixes:
53 * - Work properly with asynchronous QUnit by using module setup/teardown
54 * instead of synchronously wrapping QUnit.test.
55 */
56 sinon.assert.fail = function ( msg ) {
57 QUnit.assert.ok( false, msg );
58 };
59 sinon.assert.pass = function ( msg ) {
60 QUnit.assert.ok( true, msg );
61 };
62 sinon.config = {
63 injectIntoThis: true,
64 injectInto: null,
65 properties: [ 'spy', 'stub', 'mock', 'sandbox' ],
66 // Don't fake timers by default
67 useFakeTimers: false,
68 useFakeServer: false
69 };
70 ( function () {
71 var orgModule = QUnit.module;
72
73 QUnit.module = function ( name, localEnv ) {
74 localEnv = localEnv || {};
75 orgModule( name, {
76 setup: function () {
77 var config = sinon.getConfig( sinon.config );
78 config.injectInto = this;
79 sinon.sandbox.create( config );
80
81 if ( localEnv.setup ) {
82 localEnv.setup.call( this );
83 }
84 },
85 teardown: function () {
86 if ( localEnv.teardown ) {
87 localEnv.teardown.call( this );
88 }
89
90 this.sandbox.verifyAndRestore();
91 }
92 } );
93 };
94 }() );
95
96 // Extend QUnit.module to provide a fixture element.
97 ( function () {
98 var orgModule = QUnit.module;
99
100 QUnit.module = function ( name, localEnv ) {
101 var fixture;
102 localEnv = localEnv || {};
103 orgModule( name, {
104 setup: function () {
105 fixture = document.createElement( 'div' );
106 fixture.id = 'qunit-fixture';
107 document.body.appendChild( fixture );
108
109 if ( localEnv.setup ) {
110 localEnv.setup.call( this );
111 }
112 },
113 teardown: function () {
114 if ( localEnv.teardown ) {
115 localEnv.teardown.call( this );
116 }
117
118 fixture.parentNode.removeChild( fixture );
119 }
120 } );
121 };
122 }() );
123
124 // Initiate when enabled
125 if ( QUnit.urlParams.completenesstest ) {
126
127 // Return true to ignore
128 mwTestIgnore = function ( val, tester ) {
129
130 // Don't record methods of the properties of constructors,
131 // to avoid getting into a loop (prototype.constructor.prototype..).
132 // Since we're therefor skipping any injection for
133 // "new mw.Foo()", manually set it to true here.
134 if ( val instanceof mw.Map ) {
135 tester.methodCallTracker.Map = true;
136 return true;
137 }
138 if ( val instanceof mw.Title ) {
139 tester.methodCallTracker.Title = true;
140 return true;
141 }
142
143 // Don't record methods of the properties of a jQuery object
144 if ( val instanceof $ ) {
145 return true;
146 }
147
148 // Don't iterate over the module registry (the 'script' references would
149 // be listed as untested methods otherwise)
150 if ( val === mw.loader.moduleRegistry ) {
151 return true;
152 }
153
154 return false;
155 };
156
157 mwTester = new CompletenessTest( mw, mwTestIgnore );
158 }
159
160 /**
161 * Reset mw.config and others to a fresh copy of the live config for each test(),
162 * and restore it back to the live one afterwards.
163 *
164 * @param {Object} [localEnv]
165 * @example (see test suite at the bottom of this file)
166 * </code>
167 */
168 QUnit.newMwEnvironment = ( function () {
169 var warn, error, liveConfig, liveMessages,
170 MwMap = mw.config.constructor, // internal use only
171 ajaxRequests = [];
172
173 liveConfig = mw.config;
174 liveMessages = mw.messages;
175
176 function suppressWarnings() {
177 warn = mw.log.warn;
178 error = mw.log.error;
179 mw.log.warn = mw.log.error = $.noop;
180 }
181
182 function restoreWarnings() {
183 // Guard against calls not balanced with suppressWarnings()
184 if ( warn !== undefined ) {
185 mw.log.warn = warn;
186 mw.log.error = error;
187 warn = error = undefined;
188 }
189 }
190
191 function freshConfigCopy( custom ) {
192 var copy;
193 // Tests should mock all factors that directly influence the tested code.
194 // For backwards compatibility though we set mw.config to a fresh copy of the live
195 // config. This way any modifications made to mw.config during the test will not
196 // affect other tests, nor the global scope outside the test runner.
197 // This is a shallow copy, since overriding an array or object value via "custom"
198 // should replace it. Setting a config property means you override it, not extend it.
199 // NOTE: It is important that we suppress warnings because extend() will also access
200 // deprecated properties and trigger deprecation warnings from mw.log#deprecate.
201 suppressWarnings();
202 copy = $.extend( {}, liveConfig.get(), custom );
203 restoreWarnings();
204
205 return copy;
206 }
207
208 function freshMessagesCopy( custom ) {
209 return $.extend( /*deep=*/true, {}, liveMessages.get(), custom );
210 }
211
212 /**
213 * @param {jQuery.Event} event
214 * @param {jqXHR} jqXHR
215 * @param {Object} ajaxOptions
216 */
217 function trackAjax( event, jqXHR, ajaxOptions ) {
218 ajaxRequests.push( { xhr: jqXHR, options: ajaxOptions } );
219 }
220
221 return function ( localEnv ) {
222 localEnv = $.extend( {
223 // QUnit
224 setup: $.noop,
225 teardown: $.noop,
226 // MediaWiki
227 config: {},
228 messages: {}
229 }, localEnv );
230
231 return {
232 setup: function () {
233
234 // Greetings, mock environment!
235 mw.config = new MwMap();
236 mw.config.set( freshConfigCopy( localEnv.config ) );
237 mw.messages = new MwMap();
238 mw.messages.set( freshMessagesCopy( localEnv.messages ) );
239 // Update reference to mw.messages
240 mw.jqueryMsg.setParserDefaults( {
241 messages: mw.messages
242 } );
243
244 this.suppressWarnings = suppressWarnings;
245 this.restoreWarnings = restoreWarnings;
246
247 // Start tracking ajax requests
248 $( document ).on( 'ajaxSend', trackAjax );
249
250 localEnv.setup.call( this );
251 },
252
253 teardown: function () {
254 var timers, pending, $activeLen;
255
256 localEnv.teardown.call( this );
257
258 // Stop tracking ajax requests
259 $( document ).off( 'ajaxSend', trackAjax );
260
261 // Farewell, mock environment!
262 mw.config = liveConfig;
263 mw.messages = liveMessages;
264 // Restore reference to mw.messages
265 mw.jqueryMsg.setParserDefaults( {
266 messages: liveMessages
267 } );
268
269 // As a convenience feature, automatically restore warnings if they're
270 // still suppressed by the end of the test.
271 restoreWarnings();
272
273 // Tests should use fake timers or wait for animations to complete
274 // Check for incomplete animations/requests/etc and throw if there are any.
275 if ( $.timers && $.timers.length !== 0 ) {
276 timers = $.timers.length;
277 $.each( $.timers, function ( i, timer ) {
278 var node = timer.elem;
279 mw.log.warn( 'Unfinished animation #' + i + ' in ' + timer.queue + ' queue on ' +
280 mw.html.element( node.nodeName.toLowerCase(), $( node ).getAttrs() )
281 );
282 } );
283 // Force animations to stop to give the next test a clean start
284 $.fx.stop();
285
286 throw new Error( 'Unfinished animations: ' + timers );
287 }
288
289 // Test should use fake XHR, wait for requests, or call abort()
290 $activeLen = $.active;
291 if ( $activeLen !== undefined && $activeLen !== 0 ) {
292 pending = $.grep( ajaxRequests, function ( ajax ) {
293 return ajax.xhr.state() === 'pending';
294 } );
295 if ( pending.length !== $activeLen ) {
296 mw.log.warn( 'Pending requests does not match jQuery.active count' );
297 }
298 // Force requests to stop to give the next test a clean start
299 $.each( pending, function ( i, ajax ) {
300 mw.log.warn( 'Pending AJAX request #' + i, ajax.options );
301 ajax.xhr.abort();
302 } );
303 ajaxRequests = [];
304
305 throw new Error( 'Pending AJAX requests: ' + pending.length + ' (active: ' + $activeLen + ')' );
306 }
307 }
308 };
309 };
310 }() );
311
312 // $.when stops as soon as one fails, which makes sense in most
313 // practical scenarios, but not in a unit test where we really do
314 // need to wait until all of them are finished.
315 QUnit.whenPromisesComplete = function () {
316 var altPromises = [];
317
318 $.each( arguments, function ( i, arg ) {
319 var alt = $.Deferred();
320 altPromises.push( alt );
321
322 // Whether this one fails or not, forwards it to
323 // the 'done' (resolve) callback of the alternative promise.
324 arg.always( alt.resolve );
325 } );
326
327 return $.when.apply( $, altPromises );
328 };
329
330 /**
331 * Recursively convert a node to a plain object representing its structure.
332 * Only considers attributes and contents (elements and text nodes).
333 * Attribute values are compared strictly and not normalised.
334 *
335 * @param {Node} node
336 * @return {Object|string} Plain JavaScript value representing the node.
337 */
338 function getDomStructure( node ) {
339 var $node, children, processedChildren, i, len, el;
340 $node = $( node );
341 if ( node.nodeType === Node.ELEMENT_NODE ) {
342 children = $node.contents();
343 processedChildren = [];
344 for ( i = 0, len = children.length; i < len; i++ ) {
345 el = children[ i ];
346 if ( el.nodeType === Node.ELEMENT_NODE || el.nodeType === Node.TEXT_NODE ) {
347 processedChildren.push( getDomStructure( el ) );
348 }
349 }
350
351 return {
352 tagName: node.tagName,
353 attributes: $node.getAttrs(),
354 contents: processedChildren
355 };
356 } else {
357 // Should be text node
358 return $node.text();
359 }
360 }
361
362 /**
363 * Gets structure of node for this HTML.
364 *
365 * @param {string} html HTML markup for one or more nodes.
366 */
367 function getHtmlStructure( html ) {
368 var el = $( '<div>' ).append( html )[ 0 ];
369 return getDomStructure( el );
370 }
371
372 /**
373 * Add-on assertion helpers
374 */
375 // Define the add-ons
376 addons = {
377
378 // Expect boolean true
379 assertTrue: function ( actual, message ) {
380 QUnit.push( actual === true, actual, true, message );
381 },
382
383 // Expect boolean false
384 assertFalse: function ( actual, message ) {
385 QUnit.push( actual === false, actual, false, message );
386 },
387
388 // Expect numerical value less than X
389 lt: function ( actual, expected, message ) {
390 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
391 },
392
393 // Expect numerical value less than or equal to X
394 ltOrEq: function ( actual, expected, message ) {
395 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
396 },
397
398 // Expect numerical value greater than X
399 gt: function ( actual, expected, message ) {
400 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
401 },
402
403 // Expect numerical value greater than or equal to X
404 gtOrEq: function ( actual, expected, message ) {
405 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
406 },
407
408 /**
409 * Asserts that two HTML strings are structurally equivalent.
410 *
411 * @param {string} actualHtml Actual HTML markup.
412 * @param {string} expectedHtml Expected HTML markup
413 * @param {string} message Assertion message.
414 */
415 htmlEqual: function ( actualHtml, expectedHtml, message ) {
416 var actual = getHtmlStructure( actualHtml ),
417 expected = getHtmlStructure( expectedHtml );
418
419 QUnit.push(
420 QUnit.equiv(
421 actual,
422 expected
423 ),
424 actual,
425 expected,
426 message
427 );
428 },
429
430 /**
431 * Asserts that two HTML strings are not structurally equivalent.
432 *
433 * @param {string} actualHtml Actual HTML markup.
434 * @param {string} expectedHtml Expected HTML markup.
435 * @param {string} message Assertion message.
436 */
437 notHtmlEqual: function ( actualHtml, expectedHtml, message ) {
438 var actual = getHtmlStructure( actualHtml ),
439 expected = getHtmlStructure( expectedHtml );
440
441 QUnit.push(
442 !QUnit.equiv(
443 actual,
444 expected
445 ),
446 actual,
447 expected,
448 message
449 );
450 }
451 };
452
453 $.extend( QUnit.assert, addons );
454
455 /**
456 * Small test suite to confirm proper functionality of the utilities and
457 * initializations defined above in this file.
458 */
459 QUnit.module( 'test.mediawiki.qunit.testrunner', QUnit.newMwEnvironment( {
460 setup: function () {
461 this.mwHtmlLive = mw.html;
462 mw.html = {
463 escape: function () {
464 return 'mocked';
465 }
466 };
467 },
468 teardown: function () {
469 mw.html = this.mwHtmlLive;
470 },
471 config: {
472 testVar: 'foo'
473 },
474 messages: {
475 testMsg: 'Foo.'
476 }
477 } ) );
478
479 QUnit.test( 'Setup', 3, function ( assert ) {
480 assert.equal( mw.html.escape( 'foo' ), 'mocked', 'setup() callback was ran.' );
481 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
482 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
483
484 mw.config.set( 'testVar', 'bar' );
485 mw.messages.set( 'testMsg', 'Bar.' );
486 } );
487
488 QUnit.test( 'Teardown', 2, function ( assert ) {
489 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
490 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
491 } );
492
493 QUnit.test( 'Loader status', 2, function ( assert ) {
494 var i, len, state,
495 modules = mw.loader.getModuleNames(),
496 error = [],
497 missing = [];
498
499 for ( i = 0, len = modules.length; i < len; i++ ) {
500 state = mw.loader.getState( modules[ i ] );
501 if ( state === 'error' ) {
502 error.push( modules[ i ] );
503 } else if ( state === 'missing' ) {
504 missing.push( modules[ i ] );
505 }
506 }
507
508 assert.deepEqual( error, [], 'Modules in error state' );
509 assert.deepEqual( missing, [], 'Modules in missing state' );
510 } );
511
512 QUnit.test( 'htmlEqual', 8, function ( assert ) {
513 assert.htmlEqual(
514 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
515 '<div><p data-length=\'10\' class=\'some classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
516 'Attribute order, spacing and quotation marks (equal)'
517 );
518
519 assert.notHtmlEqual(
520 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
521 '<div><p data-length=\'10\' class=\'some more classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
522 'Attribute order, spacing and quotation marks (not equal)'
523 );
524
525 assert.htmlEqual(
526 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
527 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
528 'Multiple root nodes (equal)'
529 );
530
531 assert.notHtmlEqual(
532 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
533 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="important" >Last</label><input id="lastname" />',
534 'Multiple root nodes (not equal, last label node is different)'
535 );
536
537 assert.htmlEqual(
538 'fo&quot;o<br/>b&gt;ar',
539 'fo"o<br/>b>ar',
540 'Extra escaping is equal'
541 );
542 assert.notHtmlEqual(
543 'foo&lt;br/&gt;bar',
544 'foo<br/>bar',
545 'Text escaping (not equal)'
546 );
547
548 assert.htmlEqual(
549 'foo<a href="http://example.com">example</a>bar',
550 'foo<a href="http://example.com">example</a>bar',
551 'Outer text nodes are compared (equal)'
552 );
553
554 assert.notHtmlEqual(
555 'foo<a href="http://example.com">example</a>bar',
556 'foo<a href="http://example.com">example</a>quux',
557 'Outer text nodes are compared (last text node different)'
558 );
559
560 } );
561
562 QUnit.module( 'test.mediawiki.qunit.testrunner-after', QUnit.newMwEnvironment() );
563
564 QUnit.test( 'Teardown', 3, function ( assert ) {
565 assert.equal( mw.html.escape( '<' ), '&lt;', 'teardown() callback was ran.' );
566 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
567 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
568 } );
569
570 }( jQuery, mediaWiki, QUnit ) );