3 QUnit
.module( 'mediawiki', QUnit
.newMwEnvironment() );
5 QUnit
.test( 'Initial check', 8, function ( assert
) {
6 assert
.ok( window
.jQuery
, 'jQuery defined' );
7 assert
.ok( window
.$, '$j defined' );
8 assert
.ok( window
.$j
, '$j defined' );
9 assert
.strictEqual( window
.$, window
.jQuery
, '$ alias to jQuery' );
10 assert
.strictEqual( window
.$j
, window
.jQuery
, '$j alias to jQuery' );
12 assert
.ok( window
.mediaWiki
, 'mediaWiki defined' );
13 assert
.ok( window
.mw
, 'mw defined' );
14 assert
.strictEqual( window
.mw
, window
.mediaWiki
, 'mw alias to mediaWiki' );
17 QUnit
.test( 'mw.Map', 17, function ( assert
) {
18 var arry
, conf
, funky
, globalConf
, nummy
, someValues
;
20 assert
.ok( mw
.Map
, 'mw.Map defined' );
24 funky = function () {};
28 // Tests for input validation
29 assert
.strictEqual( conf
.get( 'inexistantKey' ), null, 'Map.get returns null if selection was a string and the key was not found' );
30 assert
.strictEqual( conf
.set( 'myKey', 'myValue' ), true, 'Map.set returns boolean true if a value was set for a valid key string' );
31 assert
.strictEqual( conf
.set( funky
, 'Funky' ), false, 'Map.set returns boolean false if key was invalid (Function)' );
32 assert
.strictEqual( conf
.set( arry
, 'Arry' ), false, 'Map.set returns boolean false if key was invalid (Array)' );
33 assert
.strictEqual( conf
.set( nummy
, 'Nummy' ), false, 'Map.set returns boolean false if key was invalid (Number)' );
34 assert
.equal( conf
.get( 'myKey' ), 'myValue', 'Map.get returns a single value value correctly' );
35 assert
.strictEqual( conf
.get( nummy
), null, 'Map.get ruturns null if selection was invalid (Number)' );
36 assert
.strictEqual( conf
.get( funky
), null, 'Map.get ruturns null if selection was invalid (Function)' );
38 // Multiple values at once
44 assert
.strictEqual( conf
.set( someValues
), true, 'Map.set returns boolean true if multiple values were set by passing an object' );
45 assert
.deepEqual( conf
.get( ['foo', 'lorem'] ), {
48 }, 'Map.get returns multiple values correctly as an object' );
50 assert
.deepEqual( conf
.get( ['foo', 'notExist'] ), {
53 }, 'Map.get return includes keys that were not found as null values' );
55 assert
.strictEqual( conf
.exists( 'foo' ), true, 'Map.exists returns boolean true if a key exists' );
56 assert
.strictEqual( conf
.exists( 'notExist' ), false, 'Map.exists returns boolean false if a key does not exists' );
58 // Interacting with globals and accessing the values object
59 assert
.strictEqual( conf
.get(), conf
.values
, 'Map.get returns the entire values object by reference (if called without arguments)' );
61 conf
.set( 'globalMapChecker', 'Hi' );
63 assert
.ok( false === 'globalMapChecker' in window
, 'new mw.Map did not store its values in the global window object by default' );
65 globalConf
= new mw
.Map( true );
66 globalConf
.set( 'anotherGlobalMapChecker', 'Hello' );
68 assert
.ok( 'anotherGlobalMapChecker' in window
, 'new mw.Map( true ) did store its values in the global window object' );
70 // Whitelist this global variable for QUnit's 'noglobal' mode
71 if ( QUnit
.config
.noglobals
) {
72 QUnit
.config
.pollution
.push( 'anotherGlobalMapChecker' );
76 QUnit
.test( 'mw.config', 1, function ( assert
) {
77 assert
.ok( mw
.config
instanceof mw
.Map
, 'mw.config instance of mw.Map' );
80 QUnit
.test( 'mw.message & mw.messages', 20, function ( assert
) {
81 var goodbye
, hello
, pluralMessage
;
83 assert
.ok( mw
.messages
, 'messages defined' );
84 assert
.ok( mw
.messages
instanceof mw
.Map
, 'mw.messages instance of mw.Map' );
85 assert
.ok( mw
.messages
.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
87 hello
= mw
.message( 'hello' );
89 assert
.equal( hello
.format
, 'plain', 'Message property "format" defaults to "plain"' );
90 assert
.strictEqual( hello
.map
, mw
.messages
, 'Message property "map" defaults to the global instance in mw.messages' );
91 assert
.equal( hello
.key
, 'hello', 'Message property "key" (currect key)' );
92 assert
.deepEqual( hello
.parameters
, [], 'Message property "parameters" defaults to an empty array' );
95 assert
.ok( hello
.params
, 'Message prototype "params"' );
97 hello
.format
= 'plain';
98 assert
.equal( hello
.toString(), 'Hello <b>awesome</b> world', 'Message.toString returns the message as a string with the current "format"' );
100 assert
.equal( hello
.escaped(), 'Hello <b>awesome</b> world', 'Message.escaped returns the escaped message' );
101 assert
.equal( hello
.format
, 'escaped', 'Message.escaped correctly updated the "format" property' );
104 assert
.equal( hello
.format
, 'parse', 'Message.parse correctly updated the "format" property' );
107 assert
.equal( hello
.format
, 'plain', 'Message.plain correctly updated the "format" property' );
109 assert
.strictEqual( hello
.exists(), true, 'Message.exists returns true for existing messages' );
111 goodbye
= mw
.message( 'goodbye' );
112 assert
.strictEqual( goodbye
.exists(), false, 'Message.exists returns false for nonexistent messages' );
114 assert
.equal( goodbye
.plain(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
116 assert
.equal( goodbye
.escaped(), '<goodbye>', 'Message.toString returns properly escaped <key> if format is "escaped" and key does not exist' );
118 assert
.ok( mw
.messages
.set( 'pluraltestmsg', 'There {{PLURAL:$1|is|are}} $1 {{PLURAL:$1|result|results}}' ), 'mw.messages.set: Register' );
119 pluralMessage
= mw
.message( 'pluraltestmsg' , 6 );
120 assert
.equal( pluralMessage
.plain(), 'There are 6 results', 'plural get resolved when format is plain' );
121 assert
.equal( pluralMessage
.parse(), 'There are 6 results', 'plural get resolved when format is parse' );
125 QUnit
.test( 'mw.msg', 11, function ( assert
) {
126 assert
.ok( mw
.messages
.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
127 assert
.equal( mw
.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
128 assert
.equal( mw
.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (nonexistent message)' );
130 assert
.ok( mw
.messages
.set( 'plural-item' , 'Found $1 {{PLURAL:$1|item|items}}' ) );
131 assert
.equal( mw
.msg( 'plural-item', 5 ), 'Found 5 items', 'Apply plural for count 5' );
132 assert
.equal( mw
.msg( 'plural-item', 0 ), 'Found 0 items', 'Apply plural for count 0' );
133 assert
.equal( mw
.msg( 'plural-item', 1 ), 'Found 1 item', 'Apply plural for count 1' );
135 assert
.ok( mw
.messages
.set('gender-plural-msg' , '{{GENDER:$1|he|she|they}} {{PLURAL:$2|is|are}} awesome' ) );
136 assert
.equal( mw
.msg( 'gender-plural-msg', 'male', 1 ), 'he is awesome', 'Gender test for male, plural count 1' );
137 assert
.equal( mw
.msg( 'gender-plural-msg', 'female', '1' ), 'she is awesome', 'Gender test for female, plural count 1' );
138 assert
.equal( mw
.msg( 'gender-plural-msg', 'unknown', 10 ), 'they are awesome', 'Gender test for neutral, plural count 10' );
143 * The sync style load test (for @import). This is, in a way, also an open bug for
144 * ResourceLoader ("execute js after styles are loaded"), but browsers don't offer a
145 * way to get a callback from when a stylesheet is loaded (that is, including any
146 * @import rules inside). To work around this, we'll have a little time loop to check
147 * if the styles apply.
148 * Note: This test originally used new Image() and onerror to get a callback
149 * when the url is loaded, but that is fragile since it doesn't monitor the
150 * same request as the css @import, and Safari 4 has issues with
151 * onerror/onload not being fired at all in weird cases like this.
153 function assertStyleAsync( assert
, $element
, prop
, val
, fn
) {
155 el
= $element
.get( 0 ),
156 styleTestTimeout
= ( QUnit
.config
.testTimeout
- 200 ) || 5000;
158 function isCssImportApplied() {
159 // Trigger reflow, repaint, redraw, whatever (cross-browser)
160 var x
= $element
.css( 'height' );
162 el
.className
= el
.className
;
163 x
= document
.documentElement
.clientHeight
;
165 return $element
.css( prop
) === val
;
168 function styleTestLoop() {
169 var styleTestSince
= new Date().getTime() - styleTestStart
;
170 // If it is passing or if we timed out, run the real test and stop the loop
171 if ( isCssImportApplied() || styleTestSince
> styleTestTimeout
) {
172 assert
.equal( $element
.css( prop
), val
,
173 'style "' + prop
+ ': ' + val
+ '" from url is applied (after ' + styleTestSince
+ 'ms)'
182 // Otherwise, keep polling
183 setTimeout( styleTestLoop
, 150 );
187 styleTestStart
= new Date().getTime();
191 function urlStyleTest( selector
, prop
, val
) {
193 mw
.config
.get( 'wgScriptPath' ) +
194 '/tests/qunit/data/styleTest.css.php?' +
203 QUnit
.asyncTest( 'mw.loader', 2, function ( assert
) {
206 mw
.loader
.testCallback = function () {
208 assert
.strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined');
209 isAwesomeDone
= true;
212 mw
.loader
.implement( 'test.callback', [QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' )], {}, {} );
214 mw
.loader
.using( 'test.callback', function () {
216 // /sample/awesome.js declares the "mw.loader.testCallback" function
217 // which contains a call to start() and ok()
218 assert
.strictEqual( isAwesomeDone
, true, "test.callback module should've caused isAwesomeDone to be true" );
219 delete mw
.loader
.testCallback
;
223 assert
.ok( false, 'Error callback fired while loader.using "test.callback" module' );
227 QUnit
.test( 'mw.loader.implement( styles={ "css": [text, ..] } )', 2, function ( assert
) {
228 var $element
= $( '<div class="mw-test-implement-a"></div>' ).appendTo( '#qunit-fixture' );
231 $element
.css( 'float' ),
240 $element
.css( 'float' ),
246 'all': '.mw-test-implement-a { float: right; }'
256 QUnit
.asyncTest( 'mw.loader.implement( styles={ "url": { <media>: [url, ..] } } )', 7, function ( assert
) {
257 var $element1
= $( '<div class="mw-test-implement-b1"></div>' ).appendTo( '#qunit-fixture' ),
258 $element2
= $( '<div class="mw-test-implement-b2"></div>' ).appendTo( '#qunit-fixture' ),
259 $element3
= $( '<div class="mw-test-implement-b3"></div>' ).appendTo( '#qunit-fixture' );
262 $element1
.css( 'text-align' ),
267 $element2
.css( 'float' ),
272 $element3
.css( 'text-align' ),
280 assertStyleAsync( assert
, $element2
, 'float', 'left', function () {
281 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
285 assertStyleAsync( assert
, $element3
, 'float', 'right', function () {
286 assert
.notEqual( $element1
.css( 'text-align' ), 'center', 'print style is not applied' );
293 'print': [urlStyleTest( '.mw-test-implement-b1', 'text-align', 'center' )],
295 // bug 40834: Make sure it actually works with more than 1 stylesheet reference
296 urlStyleTest( '.mw-test-implement-b2', 'float', 'left' ),
297 urlStyleTest( '.mw-test-implement-b3', 'float', 'right' )
309 // Backwards compatibility
310 QUnit
.test( 'mw.loader.implement( styles={ <media>: text } ) (back-compat)', 2, function ( assert
) {
311 var $element
= $( '<div class="mw-test-implement-c"></div>' ).appendTo( '#qunit-fixture' );
314 $element
.css( 'float' ),
323 $element
.css( 'float' ),
329 'all': '.mw-test-implement-c { float: right; }'
339 // Backwards compatibility
340 QUnit
.asyncTest( 'mw.loader.implement( styles={ <media>: [url, ..] } ) (back-compat)', 4, function ( assert
) {
341 var $element
= $( '<div class="mw-test-implement-d"></div>' ).appendTo( '#qunit-fixture' ),
342 $element2
= $( '<div class="mw-test-implement-d2"></div>' ).appendTo( '#qunit-fixture' );
345 $element
.css( 'float' ),
350 $element2
.css( 'text-align' ),
358 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
360 assert
.notEqual( $element2
.css( 'text-align' ), 'center', 'print style is not applied (bug 40500)' );
366 'all': [urlStyleTest( '.mw-test-implement-d', 'float', 'right' )],
367 'print': [urlStyleTest( '.mw-test-implement-d2', 'text-align', 'center' )]
377 // @import (bug 31676)
378 QUnit
.asyncTest( 'mw.loader.implement( styles has @import)', 5, function ( assert
) {
379 var isJsExecuted
, $element
;
382 'test.implement.import',
384 assert
.strictEqual( isJsExecuted
, undefined, 'javascript not executed multiple times' );
387 assert
.equal( mw
.loader
.getState( 'test.implement.import' ), 'ready', 'module state is "ready" while implement() is executing javascript' );
389 $element
= $( '<div class="mw-test-implement-import">Foo bar</div>' ).appendTo( '#qunit-fixture' );
391 assert
.equal( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'Messages are loaded before javascript execution' );
393 assertStyleAsync( assert
, $element
, 'float', 'right', function () {
394 assert
.equal( $element
.css( 'text-align' ),'center',
395 'CSS styles after the @import rule are working'
404 + urlStyleTest( '.mw-test-implement-import', 'float', 'right' )
406 + '.mw-test-implement-import { text-align: center; }'
410 'test-foobar': 'Hello Foobar, $1!'
414 mw
.loader
.load( 'test.implement' );
418 QUnit
.asyncTest( 'mw.loader.implement( only messages )' , 2, function ( assert
) {
419 assert
.assertFalse( mw
.messages
.exists( 'bug_29107' ), 'Verify that the test message doesn\'t exist yet' );
421 mw
.loader
.implement( 'test.implement.msgs', [], {}, { 'bug_29107': 'loaded' } );
422 mw
.loader
.using( 'test.implement.msgs', function() {
424 assert
.ok( mw
.messages
.exists( 'bug_29107' ), 'Bug 29107: messages-only module should implement ok' );
427 assert
.ok( false, 'Error callback fired while implementing "test.implement.msgs" module' );
431 QUnit
.test( 'mw.loader erroneous indirect dependency', 3, function ( assert
) {
432 mw
.loader
.register( [
433 ['test.module1', '0'],
434 ['test.module2', '0', ['test.module1']],
435 ['test.module3', '0', ['test.module2']]
437 mw
.loader
.implement( 'test.module1', function () { throw new Error( 'expected' ); }, {}, {} );
438 assert
.strictEqual( mw
.loader
.getState( 'test.module1' ), 'error', 'Expected "error" state for test.module1' );
439 assert
.strictEqual( mw
.loader
.getState( 'test.module2' ), 'error', 'Expected "error" state for test.module2' );
440 assert
.strictEqual( mw
.loader
.getState( 'test.module3' ), 'error', 'Expected "error" state for test.module3' );
443 QUnit
.test( 'mw.loader out-of-order implementation', 9, function ( assert
) {
444 mw
.loader
.register( [
445 ['test.module4', '0'],
446 ['test.module5', '0', ['test.module4']],
447 ['test.module6', '0', ['test.module5']]
449 mw
.loader
.implement( 'test.module4', function () {}, {}, {} );
450 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
451 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
452 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'registered', 'Expected "registered" state for test.module6' );
453 mw
.loader
.implement( 'test.module6', function () {}, {}, {} );
454 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
455 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'registered', 'Expected "registered" state for test.module5' );
456 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'loaded', 'Expected "loaded" state for test.module6' );
457 mw
.loader
.implement( 'test.module5', function() {}, {}, {} );
458 assert
.strictEqual( mw
.loader
.getState( 'test.module4' ), 'ready', 'Expected "ready" state for test.module4' );
459 assert
.strictEqual( mw
.loader
.getState( 'test.module5' ), 'ready', 'Expected "ready" state for test.module5' );
460 assert
.strictEqual( mw
.loader
.getState( 'test.module6' ), 'ready', 'Expected "ready" state for test.module6' );
463 QUnit
.test( 'mw.loader missing dependency', 13, function ( assert
) {
464 mw
.loader
.register( [
465 ['test.module7', '0'],
466 ['test.module8', '0', ['test.module7']],
467 ['test.module9', '0', ['test.module8']]
469 mw
.loader
.implement( 'test.module8', function () {}, {}, {} );
470 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
471 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
472 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
473 mw
.loader
.state( 'test.module7', 'missing' );
474 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
475 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
476 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
477 mw
.loader
.implement( 'test.module9', function () {}, {}, {} );
478 assert
.strictEqual( mw
.loader
.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
479 assert
.strictEqual( mw
.loader
.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
480 assert
.strictEqual( mw
.loader
.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
484 assert
.ok( false, "Success fired despite missing dependency" );
485 assert
.ok( true , "QUnit expected() count dummy" );
487 function ( e
, dependencies
) {
488 assert
.strictEqual( $.isArray( dependencies
), true, 'Expected array of dependencies' );
489 assert
.deepEqual( dependencies
, ['test.module7'], 'Error callback called with module test.module7' );
495 assert
.ok( false, "Success fired despite missing dependency" );
496 assert
.ok( true , "QUnit expected() count dummy" );
498 function ( e
, dependencies
) {
499 assert
.strictEqual( $.isArray( dependencies
), true, 'Expected array of dependencies' );
503 ['test.module7', 'test.module8', 'test.module9'],
504 'Error callback called with all three modules as dependencies'
510 QUnit
.asyncTest( 'mw.loader dependency handling', 5, function ( assert
) {
514 loadScript
: QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
518 mw
.loader
.register( [
519 // [module, version, dependencies, group, source]
520 ['testMissing', '1', [], null, 'testloader'],
521 ['testUsesMissing', '1', ['testMissing'], null, 'testloader'],
522 ['testUsesNestedMissing', '1', ['testUsesMissing'], null, 'testloader']
525 function verifyModuleStates() {
526 assert
.equal( mw
.loader
.getState( 'testMissing' ), 'missing', 'Module not known to server must have state "missing"' );
527 assert
.equal( mw
.loader
.getState( 'testUsesMissing' ), 'error', 'Module with missing dependency must have state "error"' );
528 assert
.equal( mw
.loader
.getState( 'testUsesNestedMissing' ), 'error', 'Module with indirect missing dependency must have state "error"' );
531 mw
.loader
.using( ['testUsesNestedMissing'],
533 assert
.ok( false, 'Error handler should be invoked.' );
534 assert
.ok( true ); // Dummy to reach QUnit expect()
536 verifyModuleStates();
540 function ( e
, badmodules
) {
541 assert
.ok( true, 'Error handler should be invoked.' );
542 // As soon as server spits out state('testMissing', 'missing');
543 // it will bubble up and trigger the error callback.
544 // Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
545 assert
.deepEqual( badmodules
, ['testMissing'], 'Bad modules as expected.' );
547 verifyModuleStates();
554 QUnit
.asyncTest( 'mw.loader( "//protocol-relative" ) (bug 30825)', 2, function ( assert
) {
555 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
556 // Test is for regressions!
558 // Forge an URL to the test callback script
559 var target
= QUnit
.fixurl(
560 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
563 // Confirm that mw.loader.load() works with protocol-relative URLs
564 target
= target
.replace( /https?:/, '' );
566 assert
.equal( target
.substr( 0, 2 ), '//',
567 'URL must be relative to test relative URLs!'
571 // The target calls QUnit.start
572 mw
.loader
.load( target
);
575 QUnit
.test( 'mw.html', 13, function ( assert
) {
576 assert
.throws( function () {
578 }, TypeError
, 'html.escape throws a TypeError if argument given is not a string' );
580 assert
.equal( mw
.html
.escape( '<mw awesome="awesome" value=\'test\' />' ),
581 '<mw awesome="awesome" value='test' />', 'escape() escapes special characters to html entities' );
583 assert
.equal( mw
.html
.element(),
584 '<undefined/>', 'element() always returns a valid html string (even without arguments)' );
586 assert
.equal( mw
.html
.element( 'div' ), '<div/>', 'element() Plain DIV (simple)' );
588 assert
.equal( mw
.html
.element( 'div', {}, '' ), '<div></div>', 'element() Basic DIV (simple)' );
596 '<div id="foobar"/>',
597 'html.element DIV (attribs)' );
599 assert
.equal( mw
.html
.element( 'p', null, 12 ), '<p>12</p>', 'Numbers are valid content and should be casted to a string' );
601 assert
.equal( mw
.html
.element( 'p', { title
: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
603 // Example from https://www.mediawiki.org/wiki/ResourceLoader/Default_modules#mediaWiki.html
609 mw
.html
.element( 'img', { src
: '<' } )
612 '<div><img src="<"/></div>',
613 'Raw inclusion of another element'
622 '<option selected="selected">Foo</option>',
623 'Attributes may have boolean values. True copies the attribute name to the value.'
633 '<option value="foo">Foo</option>',
634 'Attributes may have boolean values. False keeps the attribute from output.'
637 assert
.equal( mw
.html
.element( 'div',
640 'html.element DIV (content)' );
642 assert
.equal( mw
.html
.element( 'a',
643 { href
: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
644 '<a href="http://mediawiki.org/w/index.php?title=RL&action=history">a</a>',
645 'html.element DIV (attribs + content)' );