1 /*global CompletenessTest, sinon */
3 ( function ( $, mw
, QUnit
) {
6 var mwTestIgnore
, mwTester
,
13 * Add bogus to url to prevent IE crazy caching
15 * @param value {String} a relative path (eg. 'data/foo.js'
16 * or 'data/test.php?foo=bar').
17 * @return {String} Such as 'data/foo.js?131031765087663960'
19 QUnit
.fixurl = function ( value
) {
20 return value
+ (/\?/.test( value
) ? '&' : '?')
21 + String( new Date().getTime() )
22 + String( parseInt( Math
.random() * 100000, 10 ) );
29 // When a test() indicates asynchronicity with stop(),
30 // allow 10 seconds to pass before killing the test(),
31 // and assuming failure.
32 QUnit
.config
.testTimeout
= 10 * 1000;
34 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
35 QUnit
.config
.urlConfig
.push( {
37 label
: 'Enable ResourceLoaderDebug',
38 tooltip
: 'Enable debug mode in ResourceLoader'
41 QUnit
.config
.requireExpects
= true;
44 * Load TestSwarm agent
46 // Only if the current url indicates that there is a TestSwarm instance watching us
47 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
48 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
49 // no point in loading inject.js in that case. Also, make sure that this instance
50 // of MediaWiki has actually been configured with the required url to that inject.js
51 // script. By default it is false.
52 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
53 jQuery
.getScript( QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) );
59 * Adds toggle checkbox to header
61 QUnit
.config
.urlConfig
.push( {
62 id
: 'completenesstest',
63 label
: 'Run CompletenessTest',
64 tooltip
: 'Run the completeness test'
70 * Glue code for nicer integration with QUnit setup/teardown
71 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
73 * - Work properly with asynchronous QUnit by using module setup/teardown
74 * instead of synchronously wrapping QUnit.test.
76 sinon
.assert
.fail = function ( msg
) {
77 QUnit
.assert
.ok( false, msg
);
79 sinon
.assert
.pass = function ( msg
) {
80 QUnit
.assert
.ok( true, msg
);
85 properties
: ['spy', 'stub', 'mock', 'sandbox'],
86 // Don't fake timers by default
91 var orgModule
= QUnit
.module
;
93 QUnit
.module = function ( name
, localEnv
) {
94 localEnv
= localEnv
|| {};
97 var config
= sinon
.getConfig( sinon
.config
);
98 config
.injectInto
= this;
99 sinon
.sandbox
.create( config
);
101 if ( localEnv
.setup
) {
102 localEnv
.setup
.call( this );
105 teardown: function () {
106 this.sandbox
.verifyAndRestore();
108 if ( localEnv
.teardown
) {
109 localEnv
.teardown
.call( this );
116 // Initiate when enabled
117 if ( QUnit
.urlParams
.completenesstest
) {
119 // Return true to ignore
120 mwTestIgnore = function ( val
, tester
) {
122 // Don't record methods of the properties of constructors,
123 // to avoid getting into a loop (prototype.constructor.prototype..).
124 // Since we're therefor skipping any injection for
125 // "new mw.Foo()", manually set it to true here.
126 if ( val
instanceof mw
.Map
) {
127 tester
.methodCallTracker
.Map
= true;
130 if ( val
instanceof mw
.Title
) {
131 tester
.methodCallTracker
.Title
= true;
135 // Don't record methods of the properties of a jQuery object
136 if ( val
instanceof $ ) {
143 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
147 * Test environment recommended for all QUnit test modules
149 * Whether to log environment changes to the console
151 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
154 * Reset mw.config and others to a fresh copy of the live config for each test(),
155 * and restore it back to the live one afterwards.
156 * @param localEnv {Object} [optional]
157 * @example (see test suite at the bottom of this file)
160 QUnit
.newMwEnvironment
= ( function () {
161 var log
, liveConfig
, liveMessages
;
163 liveConfig
= mw
.config
.values
;
164 liveMessages
= mw
.messages
.values
;
166 function freshConfigCopy( custom
) {
168 // Tests should mock all factors that directly influence the tested code.
169 // For backwards compatibility though we set mw.config to a fresh copy of the live
170 // config. This way any modifications made to mw.config during the test will not
171 // affect other tests, nor the global scope outside the test runner.
172 // This is a shallow copy, since overriding an array or object value via "custom"
173 // should replace it. Setting a config property means you override it, not extend it.
174 // NOTE: It is important that we temporarily disable mw.log#warn as extend() will
175 // trigger MWDeprecationWarning for each of the deprecated properties.
177 mw
.log
.warn
= $.noop
;
179 copy
= $.extend( {}, liveConfig
, custom
);
186 function freshMessagesCopy( custom
) {
187 return $.extend( /*deep=*/true, {}, liveMessages
, custom
);
190 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
192 return function ( localEnv
) {
193 localEnv
= $.extend( {
204 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
205 + ': ' + QUnit
.config
.current
.testName
+ '"' );
207 // Greetings, mock environment!
208 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
209 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
211 localEnv
.setup
.call( this );
214 teardown: function () {
215 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
216 + ': ' + QUnit
.config
.current
.testName
+ '"' );
218 localEnv
.teardown
.call( this );
220 // Farewell, mock environment!
221 mw
.config
.values
= liveConfig
;
222 mw
.messages
.values
= liveMessages
;
228 // $.when stops as soon as one fails, which makes sense in most
229 // practical scenarios, but not in a unit test where we really do
230 // need to wait until all of them are finished.
231 QUnit
.whenPromisesComplete = function () {
232 var altPromises
= [];
234 $.each( arguments
, function ( i
, arg
) {
235 var alt
= $.Deferred();
236 altPromises
.push( alt
);
238 // Whether this one fails or not, forwards it to
239 // the 'done' (resolve) callback of the alternative promise.
240 arg
.always( alt
.resolve
);
243 return $.when
.apply( $, altPromises
);
247 * Recursively convert a node to a plain object representing its structure.
248 * Only considers attributes and contents (elements and text nodes).
249 * Attribute values are compared strictly and not normalised.
252 * @return {Object|string} Plain JavaScript value representing the node.
254 function getDomStructure( node
) {
255 var $node
, children
, processedChildren
, i
, len
, el
;
257 if ( node
.nodeType
=== ELEMENT_NODE
) {
258 children
= $node
.contents();
259 processedChildren
= [];
260 for ( i
= 0, len
= children
.length
; i
< len
; i
++ ) {
262 if ( el
.nodeType
=== ELEMENT_NODE
|| el
.nodeType
=== TEXT_NODE
) {
263 processedChildren
.push( getDomStructure( el
) );
268 tagName
: node
.tagName
,
269 attributes
: $node
.getAttrs(),
270 contents
: processedChildren
273 // Should be text node
279 * Gets structure of node for this HTML.
281 * @param {string} html HTML markup for one or more nodes.
283 function getHtmlStructure( html
) {
284 var el
= $( '<div>' ).append( html
)[0];
285 return getDomStructure( el
);
289 * Add-on assertion helpers
291 // Define the add-ons
294 // Expect boolean true
295 assertTrue: function ( actual
, message
) {
296 QUnit
.push( actual
=== true, actual
, true, message
);
299 // Expect boolean false
300 assertFalse: function ( actual
, message
) {
301 QUnit
.push( actual
=== false, actual
, false, message
);
304 // Expect numerical value less than X
305 lt: function ( actual
, expected
, message
) {
306 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
309 // Expect numerical value less than or equal to X
310 ltOrEq: function ( actual
, expected
, message
) {
311 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
314 // Expect numerical value greater than X
315 gt: function ( actual
, expected
, message
) {
316 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
319 // Expect numerical value greater than or equal to X
320 gtOrEq: function ( actual
, expected
, message
) {
321 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
325 * Asserts that two HTML strings are structurally equivalent.
327 * @param {string} actualHtml Actual HTML markup.
328 * @param {string} expectedHtml Expected HTML markup
329 * @param {string} message Assertion message.
331 htmlEqual: function ( actualHtml
, expectedHtml
, message
) {
332 var actual
= getHtmlStructure( actualHtml
),
333 expected
= getHtmlStructure( expectedHtml
);
347 * Asserts that two HTML strings are not structurally equivalent.
349 * @param {string} actualHtml Actual HTML markup.
350 * @param {string} expectedHtml Expected HTML markup.
351 * @param {string} message Assertion message.
353 notHtmlEqual: function ( actualHtml
, expectedHtml
, message
) {
354 var actual
= getHtmlStructure( actualHtml
),
355 expected
= getHtmlStructure( expectedHtml
);
369 $.extend( QUnit
.assert
, addons
);
372 * Small test suite to confirm proper functionality of the utilities and
373 * initializations defined above in this file.
376 QUnit
.module( 'test.mediawiki.qunit.testrunner', QUnit
.newMwEnvironment( {
379 this.mwHtmlLive
= mw
.html
;
381 escape: function () {
382 return 'mocked-' + envExecCount
;
386 teardown: function () {
387 mw
.html
= this.mwHtmlLive
;
397 QUnit
.test( 'Setup', 3, function ( assert
) {
398 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
399 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
400 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
402 mw
.config
.set( 'testVar', 'bar' );
403 mw
.messages
.set( 'testMsg', 'Bar.' );
406 QUnit
.test( 'Teardown', 3, function ( assert
) {
407 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
408 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
409 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
412 QUnit
.test( 'Loader status', 2, function ( assert
) {
414 modules
= mw
.loader
.getModuleNames(),
418 for ( i
= 0, len
= modules
.length
; i
< len
; i
++ ) {
419 state
= mw
.loader
.getState( modules
[i
] );
420 if ( state
=== 'error' ) {
421 error
.push( modules
[i
] );
422 } else if ( state
=== 'missing' ) {
423 missing
.push( modules
[i
] );
427 assert
.deepEqual( error
, [], 'Modules in error state' );
428 assert
.deepEqual( missing
, [], 'Modules in missing state' );
431 QUnit
.test( 'htmlEqual', 8, function ( assert
) {
433 '<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>',
434 '<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>',
435 'Attribute order, spacing and quotation marks (equal)'
439 '<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>',
440 '<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>',
441 'Attribute order, spacing and quotation marks (not equal)'
445 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
446 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
447 'Multiple root nodes (equal)'
451 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
452 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="important" >Last</label><input id="lastname" />',
453 'Multiple root nodes (not equal, last label node is different)'
457 'fo"o<br/>b>ar',
459 'Extra escaping is equal'
464 'Text escaping (not equal)'
468 'foo<a href="http://example.com">example</a>bar',
469 'foo<a href="http://example.com">example</a>bar',
470 'Outer text nodes are compared (equal)'
474 'foo<a href="http://example.com">example</a>bar',
475 'foo<a href="http://example.com">example</a>quux',
476 'Outer text nodes are compared (last text node different)'
481 QUnit
.module( 'test.mediawiki.qunit.testrunner-after', QUnit
.newMwEnvironment() );
483 QUnit
.test( 'Teardown', 3, function ( assert
) {
484 assert
.equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
485 assert
.equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
486 assert
.equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
489 }( jQuery
, mediaWiki
, QUnit
) );