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