1 /*global CompletenessTest, sinon */
2 ( function ( $, mw
, QUnit
) {
5 var mwTestIgnore
, mwTester
, addons
;
8 * Add bogus to url to prevent IE crazy caching
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'
14 QUnit
.fixurl = function ( value
) {
15 return value
+ ( /\?/.test( value
) ? '&' : '?' )
16 + String( new Date().getTime() )
17 + String( parseInt( Math
.random() * 100000, 10 ) );
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;
28 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
29 QUnit
.config
.urlConfig
.push( {
31 label
: 'Enable ResourceLoaderDebug',
32 tooltip
: 'Enable debug mode in ResourceLoader',
39 * Adds toggle checkbox to header
41 QUnit
.config
.urlConfig
.push( {
42 id
: 'completenesstest',
43 label
: 'Run CompletenessTest',
44 tooltip
: 'Run the completeness test'
50 * Glue code for nicer integration with QUnit setup/teardown
51 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
53 * - Work properly with asynchronous QUnit by using module setup/teardown
54 * instead of synchronously wrapping QUnit.test.
56 sinon
.assert
.fail = function ( msg
) {
57 QUnit
.assert
.ok( false, msg
);
59 sinon
.assert
.pass = function ( msg
) {
60 QUnit
.assert
.ok( true, msg
);
65 properties
: [ 'spy', 'stub', 'mock', 'sandbox' ],
66 // Don't fake timers by default
71 var orgModule
= QUnit
.module
;
73 QUnit
.module = function ( name
, localEnv
) {
74 localEnv
= localEnv
|| {};
77 var config
= sinon
.getConfig( sinon
.config
);
78 config
.injectInto
= this;
79 sinon
.sandbox
.create( config
);
81 if ( localEnv
.setup
) {
82 localEnv
.setup
.call( this );
85 teardown: function () {
86 if ( localEnv
.teardown
) {
87 localEnv
.teardown
.call( this );
90 this.sandbox
.verifyAndRestore();
96 // Extend QUnit.module to provide a fixture element.
98 var orgModule
= QUnit
.module
;
100 QUnit
.module = function ( name
, localEnv
) {
102 localEnv
= localEnv
|| {};
105 fixture
= document
.createElement( 'div' );
106 fixture
.id
= 'qunit-fixture';
107 document
.body
.appendChild( fixture
);
109 if ( localEnv
.setup
) {
110 localEnv
.setup
.call( this );
113 teardown: function () {
114 if ( localEnv
.teardown
) {
115 localEnv
.teardown
.call( this );
118 fixture
.parentNode
.removeChild( fixture
);
124 // Initiate when enabled
125 if ( QUnit
.urlParams
.completenesstest
) {
127 // Return true to ignore
128 mwTestIgnore = function ( val
, tester
) {
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;
138 if ( val
instanceof mw
.Title
) {
139 tester
.methodCallTracker
.Title
= true;
143 // Don't record methods of the properties of a jQuery object
144 if ( val
instanceof $ ) {
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
) {
157 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
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.
164 * @param {Object} [localEnv]
165 * @example (see test suite at the bottom of this file)
168 QUnit
.newMwEnvironment
= ( function () {
169 var warn
, error
, liveConfig
, liveMessages
,
170 MwMap
= mw
.config
.constructor, // internal use only
173 liveConfig
= mw
.config
;
174 liveMessages
= mw
.messages
;
176 function suppressWarnings() {
178 error
= mw
.log
.error
;
179 mw
.log
.warn
= mw
.log
.error
= $.noop
;
182 function restoreWarnings() {
183 // Guard against calls not balanced with suppressWarnings()
184 if ( warn
!== undefined ) {
186 mw
.log
.error
= error
;
187 warn
= error
= undefined;
191 function freshConfigCopy( custom
) {
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.
202 copy
= $.extend( {}, liveConfig
.get(), custom
);
208 function freshMessagesCopy( custom
) {
209 return $.extend( /*deep=*/true, {}, liveMessages
.get(), custom
);
213 * @param {jQuery.Event} event
214 * @param {jqXHR} jqXHR
215 * @param {Object} ajaxOptions
217 function trackAjax( event
, jqXHR
, ajaxOptions
) {
218 ajaxRequests
.push( { xhr
: jqXHR
, options
: ajaxOptions
} );
221 return function ( localEnv
) {
222 localEnv
= $.extend( {
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
244 this.suppressWarnings
= suppressWarnings
;
245 this.restoreWarnings
= restoreWarnings
;
247 // Start tracking ajax requests
248 $( document
).on( 'ajaxSend', trackAjax
);
250 localEnv
.setup
.call( this );
253 teardown: function () {
254 var timers
, pending
, $activeLen
;
256 localEnv
.teardown
.call( this );
258 // Stop tracking ajax requests
259 $( document
).off( 'ajaxSend', trackAjax
);
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
269 // As a convenience feature, automatically restore warnings if they're
270 // still suppressed by the end of the test.
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() )
283 // Force animations to stop to give the next test a clean start
286 throw new Error( 'Unfinished animations: ' + timers
);
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';
295 if ( pending
.length
!== $activeLen
) {
296 mw
.log
.warn( 'Pending requests does not match jQuery.active count' );
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
);
305 throw new Error( 'Pending AJAX requests: ' + pending
.length
+ ' (active: ' + $activeLen
+ ')' );
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
= [];
318 $.each( arguments
, function ( i
, arg
) {
319 var alt
= $.Deferred();
320 altPromises
.push( alt
);
322 // Whether this one fails or not, forwards it to
323 // the 'done' (resolve) callback of the alternative promise.
324 arg
.always( alt
.resolve
);
327 return $.when
.apply( $, altPromises
);
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.
336 * @return {Object|string} Plain JavaScript value representing the node.
338 function getDomStructure( node
) {
339 var $node
, children
, processedChildren
, i
, len
, el
;
341 if ( node
.nodeType
=== Node
.ELEMENT_NODE
) {
342 children
= $node
.contents();
343 processedChildren
= [];
344 for ( i
= 0, len
= children
.length
; i
< len
; i
++ ) {
346 if ( el
.nodeType
=== Node
.ELEMENT_NODE
|| el
.nodeType
=== Node
.TEXT_NODE
) {
347 processedChildren
.push( getDomStructure( el
) );
352 tagName
: node
.tagName
,
353 attributes
: $node
.getAttrs(),
354 contents
: processedChildren
357 // Should be text node
363 * Gets structure of node for this HTML.
365 * @param {string} html HTML markup for one or more nodes.
367 function getHtmlStructure( html
) {
368 var el
= $( '<div>' ).append( html
)[ 0 ];
369 return getDomStructure( el
);
373 * Add-on assertion helpers
375 // Define the add-ons
378 // Expect boolean true
379 assertTrue: function ( actual
, message
) {
380 QUnit
.push( actual
=== true, actual
, true, message
);
383 // Expect boolean false
384 assertFalse: function ( actual
, message
) {
385 QUnit
.push( actual
=== false, actual
, false, message
);
388 // Expect numerical value less than X
389 lt: function ( actual
, expected
, message
) {
390 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
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
);
398 // Expect numerical value greater than X
399 gt: function ( actual
, expected
, message
) {
400 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
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
);
409 * Asserts that two HTML strings are structurally equivalent.
411 * @param {string} actualHtml Actual HTML markup.
412 * @param {string} expectedHtml Expected HTML markup
413 * @param {string} message Assertion message.
415 htmlEqual: function ( actualHtml
, expectedHtml
, message
) {
416 var actual
= getHtmlStructure( actualHtml
),
417 expected
= getHtmlStructure( expectedHtml
);
431 * Asserts that two HTML strings are not structurally equivalent.
433 * @param {string} actualHtml Actual HTML markup.
434 * @param {string} expectedHtml Expected HTML markup.
435 * @param {string} message Assertion message.
437 notHtmlEqual: function ( actualHtml
, expectedHtml
, message
) {
438 var actual
= getHtmlStructure( actualHtml
),
439 expected
= getHtmlStructure( expectedHtml
);
453 $.extend( QUnit
.assert
, addons
);
456 * Small test suite to confirm proper functionality of the utilities and
457 * initializations defined above in this file.
459 QUnit
.module( 'test.mediawiki.qunit.testrunner', QUnit
.newMwEnvironment( {
461 this.mwHtmlLive
= mw
.html
;
463 escape: function () {
468 teardown: function () {
469 mw
.html
= this.mwHtmlLive
;
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' );
484 mw
.config
.set( 'testVar', 'bar' );
485 mw
.messages
.set( 'testMsg', 'Bar.' );
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()' );
493 QUnit
.test( 'Loader status', 2, function ( assert
) {
495 modules
= mw
.loader
.getModuleNames(),
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
] );
508 assert
.deepEqual( error
, [], 'Modules in error state' );
509 assert
.deepEqual( missing
, [], 'Modules in missing state' );
512 QUnit
.test( 'htmlEqual', 8, function ( assert
) {
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)'
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)'
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)'
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)'
538 'fo"o<br/>b>ar',
540 'Extra escaping is equal'
545 'Text escaping (not equal)'
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)'
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)'
562 QUnit
.module( 'test.mediawiki.qunit.testrunner-after', QUnit
.newMwEnvironment() );
564 QUnit
.test( 'Teardown', 3, function ( assert
) {
565 assert
.equal( mw
.html
.escape( '<' ), '<', '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()' );
570 }( jQuery
, mediaWiki
, QUnit
) );