1 ( function ( $, mw
, QUnit
, undefined ) {
4 var mwTestIgnore
, mwTester
, addons
;
7 * Add bogus to url to prevent IE crazy caching
9 * @param value {String} a relative path (eg. 'data/foo.js'
10 * or 'data/test.php?foo=bar').
11 * @return {String} Such as 'data/foo.js?131031765087663960'
13 QUnit
.fixurl = function ( value
) {
14 return value
+ (/\?/.test( value
) ? '&' : '?')
15 + String( new Date().getTime() )
16 + String( parseInt( Math
.random() * 100000, 10 ) );
23 // When a test() indicates asynchronicity with stop(),
24 // allow 10 seconds to pass before killing the test(),
25 // and assuming failure.
26 QUnit
.config
.testTimeout
= 10 * 1000;
28 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
29 QUnit
.config
.urlConfig
.push( 'debug' );
32 * Load TestSwarm agent
34 // Only if the current url indicates that there is a TestSwarm instance watching us
35 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
36 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
37 // no point in loading inject.js in that case. Also, make sure that this instance
38 // of MediaWiki has actually been configured with the required url to that inject.js
39 // script. By default it is false.
40 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
41 document
.write( "<scr" + "ipt src='" + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
47 // Adds toggle checkbox to header
48 QUnit
.config
.urlConfig
.push( 'completenesstest' );
50 // Initiate when enabled
51 if ( QUnit
.urlParams
.completenesstest
) {
53 // Return true to ignore
54 mwTestIgnore = function ( val
, tester
, funcPath
) {
56 // Don't record methods of the properties of constructors,
57 // to avoid getting into a loop (prototype.constructor.prototype..).
58 // Since we're therefor skipping any injection for
59 // "new mw.Foo()", manually set it to true here.
60 if ( val
instanceof mw
.Map
) {
61 tester
.methodCallTracker
.Map
= true;
64 if ( val
instanceof mw
.Title
) {
65 tester
.methodCallTracker
.Title
= true;
69 // Don't record methods of the properties of a jQuery object
70 if ( val
instanceof $ ) {
77 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
81 * Test environment recommended for all QUnit test modules
83 // Whether to log environment changes to the console
84 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
87 * Reset mw.config to a fresh copy of the live config for each test();
88 * @param override {Object} [optional]
91 * module( .., newMwEnvironment() );
93 * test( .., function () {
94 * mw.config.set( 'foo', 'bar' ); // just for this test
97 * test( .., function () {
98 * mw.config.get( 'foo' ); // doesn't exist
102 * module( .., newMwEnvironment({ quux: 'corge' }) );
104 * test( .., function () {
105 * mw.config.get( 'quux' ); // "corge"
106 * mw.config.set( 'quux', "grault" );
109 * test( .., function () {
110 * mw.config.get( 'quux' ); // "corge"
114 QUnit
.newMwEnvironment
= ( function () {
115 var log
, liveConfig
, liveMsgs
;
117 liveConfig
= mw
.config
.values
;
118 liveMsgs
= mw
.messages
.values
;
120 function freshConfigCopy( custom
) {
121 // "deep=true" is important here.
122 // Otherwise we just create a new object with values referring to live config.
123 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
124 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
125 // was passed by reference in $.extend's loop.
126 return $.extend({}, liveConfig
, custom
, /*deep=*/true );
129 function freshMsgsCopy( custom
) {
130 return $.extend({}, liveMsgs
, custom
, /*deep=*/true );
133 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
135 return function ( overrideConfig
, overrideMsgs
) {
136 overrideConfig
= overrideConfig
|| {};
137 overrideMsgs
= overrideMsgs
|| {};
141 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
142 + ': ' + QUnit
.config
.current
.testName
+ '"' );
143 // Greetings, mock environment!
144 mw
.config
.values
= freshConfigCopy( overrideConfig
);
145 mw
.messages
.values
= freshMsgsCopy( overrideMsgs
);
148 teardown: function () {
149 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
150 + ': ' + QUnit
.config
.current
.testName
+ '"' );
151 // Farewell, mock environment!
152 mw
.config
.values
= liveConfig
;
153 mw
.messages
.values
= liveMsgs
;
160 * Add-on assertion helpers
162 // Define the add-ons
165 // Expect boolean true
166 assertTrue: function ( actual
, message
) {
167 strictEqual( actual
, true, message
);
170 // Expect boolean false
171 assertFalse: function ( actual
, message
) {
172 strictEqual( actual
, false, message
);
175 // Expect numerical value less than X
176 lt: function ( actual
, expected
, message
) {
177 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
180 // Expect numerical value less than or equal to X
181 ltOrEq: function ( actual
, expected
, message
) {
182 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
185 // Expect numerical value greater than X
186 gt: function ( actual
, expected
, message
) {
187 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
190 // Expect numerical value greater than or equal to X
191 gtOrEq: function ( actual
, expected
, message
) {
192 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
195 // Backwards compatible with new verions of QUnit
196 equals
: window
.equal
,
197 same
: window
.deepEqual
200 $.extend( QUnit
, addons
);
201 $.extend( window
, addons
);
203 })( jQuery
, mediaWiki
, QUnit
);