summaryrefslogtreecommitdiff
path: root/tests/qunit/data/testrunner.js
blob: 03aaf4af700a7503b6f7826a7893ad9b7596297b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*global CompletenessTest, sinon */
/*jshint evil: true */
( function ( $, mw, QUnit ) {
	'use strict';

	var mwTestIgnore, mwTester,
		addons,
		ELEMENT_NODE = 1,
		TEXT_NODE = 3;

	/**
	 * Add bogus to url to prevent IE crazy caching
	 *
	 * @param value {String} a relative path (eg. 'data/foo.js'
	 * or 'data/test.php?foo=bar').
	 * @return {String} Such as 'data/foo.js?131031765087663960'
	 */
	QUnit.fixurl = function ( value ) {
		return value + (/\?/.test( value ) ? '&' : '?')
			+ String( new Date().getTime() )
			+ String( parseInt( Math.random() * 100000, 10 ) );
	};

	/**
	 * Configuration
	 */

	// When a test() indicates asynchronicity with stop(),
	// allow 30 seconds to pass before killing the test(),
	// and assuming failure.
	QUnit.config.testTimeout = 30 * 1000;

	QUnit.config.requireExpects = true;

	// Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
	QUnit.config.urlConfig.push( {
		id: 'debug',
		label: 'Enable ResourceLoaderDebug',
		tooltip: 'Enable debug mode in ResourceLoader',
		value: 'true'
	} );

	/**
	 * CompletenessTest
	 *
	 * Adds toggle checkbox to header
	 */
	QUnit.config.urlConfig.push( {
		id: 'completenesstest',
		label: 'Run CompletenessTest',
		tooltip: 'Run the completeness test'
	} );

	/**
	 * SinonJS
	 *
	 * Glue code for nicer integration with QUnit setup/teardown
	 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
	 * Fixes:
	 * - Work properly with asynchronous QUnit by using module setup/teardown
	 *   instead of synchronously wrapping QUnit.test.
	 */
	sinon.assert.fail = function ( msg ) {
		QUnit.assert.ok( false, msg );
	};
	sinon.assert.pass = function ( msg ) {
		QUnit.assert.ok( true, msg );
	};
	sinon.config = {
		injectIntoThis: true,
		injectInto: null,
		properties: ['spy', 'stub', 'mock', 'sandbox'],
		// Don't fake timers by default
		useFakeTimers: false,
		useFakeServer: false
	};
	( function () {
		var orgModule = QUnit.module;

		QUnit.module = function ( name, localEnv ) {
			localEnv = localEnv || {};
			orgModule( name, {
				setup: function () {
					var config = sinon.getConfig( sinon.config );
					config.injectInto = this;
					sinon.sandbox.create( config );

					if ( localEnv.setup ) {
						localEnv.setup.call( this );
					}
				},
				teardown: function () {
					this.sandbox.verifyAndRestore();

					if ( localEnv.teardown ) {
						localEnv.teardown.call( this );
					}
				}
			} );
		};
	}() );

	// Extend QUnit.module to provide a fixture element.
	( function () {
		var orgModule = QUnit.module;

		QUnit.module = function ( name, localEnv ) {
			var fixture;
			localEnv = localEnv || {};
			orgModule( name, {
				setup: function () {
					fixture = document.createElement( 'div' );
					fixture.id = 'qunit-fixture';
					document.body.appendChild( fixture );

					if ( localEnv.setup ) {
						localEnv.setup.call( this );
					}
				},
				teardown: function () {
					if ( localEnv.teardown ) {
						localEnv.teardown.call( this );
					}

					fixture.parentNode.removeChild( fixture );
				}
			} );
		};
	}() );

	// Initiate when enabled
	if ( QUnit.urlParams.completenesstest ) {

		// Return true to ignore
		mwTestIgnore = function ( val, tester ) {

			// Don't record methods of the properties of constructors,
			// to avoid getting into a loop (prototype.constructor.prototype..).
			// Since we're therefor skipping any injection for
			// "new mw.Foo()", manually set it to true here.
			if ( val instanceof mw.Map ) {
				tester.methodCallTracker.Map = true;
				return true;
			}
			if ( val instanceof mw.Title ) {
				tester.methodCallTracker.Title = true;
				return true;
			}

			// Don't record methods of the properties of a jQuery object
			if ( val instanceof $ ) {
				return true;
			}

			// Don't iterate over the module registry (the 'script' references would
			// be listed as untested methods otherwise)
			if ( val === mw.loader.moduleRegistry ) {
				return true;
			}

			return false;
		};

		mwTester = new CompletenessTest( mw, mwTestIgnore );
	}

	/**
	 * Test environment recommended for all QUnit test modules
	 *
	 * Whether to log environment changes to the console
	 */
	QUnit.config.urlConfig.push( 'mwlogenv' );

	/**
	 * Reset mw.config and others to a fresh copy of the live config for each test(),
	 * and restore it back to the live one afterwards.
	 * @param localEnv {Object} [optional]
	 * @example (see test suite at the bottom of this file)
	 * </code>
	 */
	QUnit.newMwEnvironment = ( function () {
		var warn, log, liveConfig, liveMessages;

		liveConfig = mw.config.values;
		liveMessages = mw.messages.values;

		function suppressWarnings() {
			warn = mw.log.warn;
			mw.log.warn = $.noop;
		}

		function restoreWarnings() {
			if ( warn !== undefined ) {
				mw.log.warn = warn;
				warn = undefined;
			}
		}

		function freshConfigCopy( custom ) {
			var copy;
			// Tests should mock all factors that directly influence the tested code.
			// For backwards compatibility though we set mw.config to a fresh copy of the live
			// config. This way any modifications made to mw.config during the test will not
			// affect other tests, nor the global scope outside the test runner.
			// This is a shallow copy, since overriding an array or object value via "custom"
			// should replace it. Setting a config property means you override it, not extend it.
			// NOTE: It is important that we suppress warnings because extend() will also access
			// deprecated properties and trigger deprecation warnings from mw.log#deprecate.
			suppressWarnings();
			copy = $.extend( {}, liveConfig, custom );
			restoreWarnings();

			return copy;
		}

		function freshMessagesCopy( custom ) {
			return $.extend( /*deep=*/true, {}, liveMessages, custom );
		}

		log = QUnit.urlParams.mwlogenv ? mw.log : function () {};

		return function ( localEnv ) {
			localEnv = $.extend( {
				// QUnit
				setup: $.noop,
				teardown: $.noop,
				// MediaWiki
				config: {},
				messages: {}
			}, localEnv );

			return {
				setup: function () {
					log( 'MwEnvironment> SETUP    for "' + QUnit.config.current.module
						+ ': ' + QUnit.config.current.testName + '"' );

					// Greetings, mock environment!
					mw.config.values = freshConfigCopy( localEnv.config );
					mw.messages.values = freshMessagesCopy( localEnv.messages );
					this.suppressWarnings = suppressWarnings;
					this.restoreWarnings = restoreWarnings;

					localEnv.setup.call( this );
				},

				teardown: function () {
					var timers;
					log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
						+ ': ' + QUnit.config.current.testName + '"' );

					localEnv.teardown.call( this );

					// Farewell, mock environment!
					mw.config.values = liveConfig;
					mw.messages.values = liveMessages;

					// As a convenience feature, automatically restore warnings if they're
					// still suppressed by the end of the test.
					restoreWarnings();

					// Check for incomplete animations/requests/etc and throw
					// error if there are any.
					if ( $.timers && $.timers.length !== 0 ) {
						timers = $.timers.length;
						// Tests shoulld use fake timers or wait for animations to complete
						$.each( $.timers, function ( i, timer ) {
							var node = timer.elem;
							mw.log.warn( 'Unfinished animation #' + i + ' in ' + timer.queue + ' queue on ' +
								mw.html.element( node.nodeName.toLowerCase(), $(node).getAttrs() )
							);
						} );
						// Force animations to stop to give the next test a clean start
						$.fx.stop();

						throw new Error( 'Unfinished animations: ' + timers );
					}
					if ( $.active !== undefined && $.active !== 0 ) {
						// Test may need to use fake XHR, wait for requests or
						// call abort().
						throw new Error( 'Unfinished AJAX requests: ' + $.active );
					}
				}
			};
		};
	}() );

	// $.when stops as soon as one fails, which makes sense in most
	// practical scenarios, but not in a unit test where we really do
	// need to wait until all of them are finished.
	QUnit.whenPromisesComplete = function () {
		var altPromises = [];

		$.each( arguments, function ( i, arg ) {
			var alt = $.Deferred();
			altPromises.push( alt );

			// Whether this one fails or not, forwards it to
			// the 'done' (resolve) callback of the alternative promise.
			arg.always( alt.resolve );
		} );

		return $.when.apply( $, altPromises );
	};

	/**
	 * Recursively convert a node to a plain object representing its structure.
	 * Only considers attributes and contents (elements and text nodes).
	 * Attribute values are compared strictly and not normalised.
	 *
	 * @param {Node} node
	 * @return {Object|string} Plain JavaScript value representing the node.
	 */
	function getDomStructure( node ) {
		var $node, children, processedChildren, i, len, el;
		$node = $( node );
		if ( node.nodeType === ELEMENT_NODE ) {
			children = $node.contents();
			processedChildren = [];
			for ( i = 0, len = children.length; i < len; i++ ) {
				el = children[i];
				if ( el.nodeType === ELEMENT_NODE || el.nodeType === TEXT_NODE ) {
					processedChildren.push( getDomStructure( el ) );
				}
			}

			return {
				tagName: node.tagName,
				attributes: $node.getAttrs(),
				contents: processedChildren
			};
		} else {
			// Should be text node
			return $node.text();
		}
	}

	/**
	 * Gets structure of node for this HTML.
	 *
	 * @param {string} html HTML markup for one or more nodes.
	 */
	function getHtmlStructure( html ) {
		var el = $( '<div>' ).append( html )[0];
		return getDomStructure( el );
	}

	/**
	 * Add-on assertion helpers
	 */
	// Define the add-ons
	addons = {

		// Expect boolean true
		assertTrue: function ( actual, message ) {
			QUnit.push( actual === true, actual, true, message );
		},

		// Expect boolean false
		assertFalse: function ( actual, message ) {
			QUnit.push( actual === false, actual, false, message );
		},

		// Expect numerical value less than X
		lt: function ( actual, expected, message ) {
			QUnit.push( actual < expected, actual, 'less than ' + expected, message );
		},

		// Expect numerical value less than or equal to X
		ltOrEq: function ( actual, expected, message ) {
			QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
		},

		// Expect numerical value greater than X
		gt: function ( actual, expected, message ) {
			QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
		},

		// Expect numerical value greater than or equal to X
		gtOrEq: function ( actual, expected, message ) {
			QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
		},

		/**
		 * Asserts that two HTML strings are structurally equivalent.
		 *
		 * @param {string} actualHtml Actual HTML markup.
		 * @param {string} expectedHtml Expected HTML markup
		 * @param {string} message Assertion message.
		 */
		htmlEqual: function ( actualHtml, expectedHtml, message ) {
			var actual = getHtmlStructure( actualHtml ),
				expected = getHtmlStructure( expectedHtml );

			QUnit.push(
				QUnit.equiv(
					actual,
					expected
				),
				actual,
				expected,
				message
			);
		},

		/**
		 * Asserts that two HTML strings are not structurally equivalent.
		 *
		 * @param {string} actualHtml Actual HTML markup.
		 * @param {string} expectedHtml Expected HTML markup.
		 * @param {string} message Assertion message.
		 */
		notHtmlEqual: function ( actualHtml, expectedHtml, message ) {
			var actual = getHtmlStructure( actualHtml ),
				expected = getHtmlStructure( expectedHtml );

			QUnit.push(
				!QUnit.equiv(
					actual,
					expected
				),
				actual,
				expected,
				message
			);
		}
	};

	$.extend( QUnit.assert, addons );

	/**
	 * Small test suite to confirm proper functionality of the utilities and
	 * initializations defined above in this file.
	 */
	QUnit.module( 'test.mediawiki.qunit.testrunner', QUnit.newMwEnvironment( {
		setup: function () {
			this.mwHtmlLive = mw.html;
			mw.html = {
				escape: function () {
					return 'mocked';
				}
			};
		},
		teardown: function () {
			mw.html = this.mwHtmlLive;
		},
		config: {
			testVar: 'foo'
		},
		messages: {
			testMsg: 'Foo.'
		}
	} ) );

	QUnit.test( 'Setup', 3, function ( assert ) {
		assert.equal( mw.html.escape( 'foo' ), 'mocked', 'setup() callback was ran.' );
		assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
		assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );

		mw.config.set( 'testVar', 'bar' );
		mw.messages.set( 'testMsg', 'Bar.' );
	} );

	QUnit.test( 'Teardown', 2, function ( assert ) {
		assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
		assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
	} );

	QUnit.test( 'Loader status', 2, function ( assert ) {
		var i, len, state,
			modules = mw.loader.getModuleNames(),
			error = [],
			missing = [];

		for ( i = 0, len = modules.length; i < len; i++ ) {
			state = mw.loader.getState( modules[i] );
			if ( state === 'error' ) {
				error.push( modules[i] );
			} else if ( state === 'missing' ) {
				missing.push( modules[i] );
			}
		}

		assert.deepEqual( error, [], 'Modules in error state' );
		assert.deepEqual( missing, [], 'Modules in missing state' );
	} );

	QUnit.test( 'htmlEqual', 8, function ( assert ) {
		assert.htmlEqual(
			'<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>',
			'<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>',
			'Attribute order, spacing and quotation marks (equal)'
		);

		assert.notHtmlEqual(
			'<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>',
			'<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>',
			'Attribute order, spacing and quotation marks (not equal)'
		);

		assert.htmlEqual(
			'<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
			'<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
			'Multiple root nodes (equal)'
		);

		assert.notHtmlEqual(
			'<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
			'<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="important" >Last</label><input id="lastname" />',
			'Multiple root nodes (not equal, last label node is different)'
		);

		assert.htmlEqual(
			'fo&quot;o<br/>b&gt;ar',
			'fo"o<br/>b>ar',
			'Extra escaping is equal'
		);
		assert.notHtmlEqual(
			'foo&lt;br/&gt;bar',
			'foo<br/>bar',
			'Text escaping (not equal)'
		);

		assert.htmlEqual(
			'foo<a href="http://example.com">example</a>bar',
			'foo<a href="http://example.com">example</a>bar',
			'Outer text nodes are compared (equal)'
		);

		assert.notHtmlEqual(
			'foo<a href="http://example.com">example</a>bar',
			'foo<a href="http://example.com">example</a>quux',
			'Outer text nodes are compared (last text node different)'
		);

	} );

	QUnit.module( 'test.mediawiki.qunit.testrunner-after', QUnit.newMwEnvironment() );

	QUnit.test( 'Teardown', 3, function ( assert ) {
		assert.equal( mw.html.escape( '<' ), '&lt;', 'teardown() callback was ran.' );
		assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
		assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
	} );

}( jQuery, mediaWiki, QUnit ) );