b4134f7554e17446cb459f22023cd3dd213f74cf
1 module( 'mediawiki', QUnit
.newMwEnvironment() );
3 test( '-- Initial check', function() {
6 ok( window
.jQuery
, 'jQuery defined' );
7 ok( window
.$, '$j defined' );
8 ok( window
.$j
, '$j defined' );
9 strictEqual( window
.$, window
.jQuery
, '$ alias to jQuery' );
10 strictEqual( window
.$j
, window
.jQuery
, '$j alias to jQuery' );
12 ok( window
.mediaWiki
, 'mediaWiki defined' );
13 ok( window
.mw
, 'mw defined' );
14 strictEqual( window
.mw
, window
.mediaWiki
, 'mw alias to mediaWiki' );
17 test( 'mw.Map', function() {
20 ok( mw
.Map
, 'mw.Map defined' );
22 var conf
= new mw
.Map(),
24 funky = function() {},
28 // Tests for input validation
29 strictEqual( conf
.get( 'inexistantKey' ), null, 'Map.get returns null if selection was a string and the key was not found' );
30 strictEqual( conf
.set( 'myKey', 'myValue' ), true, 'Map.set returns boolean true if a value was set for a valid key string' );
31 strictEqual( conf
.set( funky
, 'Funky' ), false, 'Map.set returns boolean false if key was invalid (Function)' );
32 strictEqual( conf
.set( arry
, 'Arry' ), false, 'Map.set returns boolean false if key was invalid (Array)' );
33 strictEqual( conf
.set( nummy
, 'Nummy' ), false, 'Map.set returns boolean false if key was invalid (Number)' );
34 equal( conf
.get( 'myKey' ), 'myValue', 'Map.get returns a single value value correctly' );
35 strictEqual( conf
.get( nummy
), null, 'Map.get ruturns null if selection was invalid (Number)' );
36 strictEqual( conf
.get( funky
), null, 'Map.get ruturns null if selection was invalid (Function)' );
38 // Multiple values at once
44 strictEqual( conf
.set( someValues
), true, 'Map.set returns boolean true if multiple values were set by passing an object' );
45 deepEqual( conf
.get( ['foo', 'lorem'] ), {
48 }, 'Map.get returns multiple values correctly as an object' );
50 deepEqual( conf
.get( ['foo', 'notExist'] ), {
53 }, 'Map.get return includes keys that were not found as null values' );
55 strictEqual( conf
.exists( 'foo' ), true, 'Map.exists returns boolean true if a key exists' );
56 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 strictEqual( conf
.get(), conf
.values
, 'Map.get returns the entire values object by reference (if called without arguments)' );
61 conf
.set( 'globalMapChecker', 'Hi' );
63 ok( false === 'globalMapChecker' in window
, 'new mw.Map did not store its values in the global window object by default' );
65 var globalConf
= new mw
.Map( true );
66 globalConf
.set( 'anotherGlobalMapChecker', 'Hello' );
68 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 test( 'mw.config', function() {
79 ok( mw
.config
instanceof mw
.Map
, 'mw.config instance of mw.Map' );
82 test( 'mw.message & mw.messages', function() {
85 ok( mw
.messages
, 'messages defined' );
86 ok( mw
.messages
instanceof mw
.Map
, 'mw.messages instance of mw.Map' );
87 ok( mw
.messages
.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
89 var hello
= mw
.message( 'hello' );
91 equal( hello
.format
, 'plain', 'Message property "format" defaults to "plain"' );
92 strictEqual( hello
.map
, mw
.messages
, 'Message property "map" defaults to the global instance in mw.messages' );
93 equal( hello
.key
, 'hello', 'Message property "key" (currect key)' );
94 deepEqual( hello
.parameters
, [], 'Message property "parameters" defaults to an empty array' );
97 ok( hello
.params
, 'Message prototype "params"' );
99 hello
.format
= 'plain';
100 equal( hello
.toString(), 'Hello <b>awesome</b> world', 'Message.toString returns the message as a string with the current "format"' );
102 equal( hello
.escaped(), 'Hello <b>awesome</b> world', 'Message.escaped returns the escaped message' );
103 equal( hello
.format
, 'escaped', 'Message.escaped correctly updated the "format" property' );
106 equal( hello
.format
, 'parse', 'Message.parse correctly updated the "format" property' );
109 equal( hello
.format
, 'plain', 'Message.plain correctly updated the "format" property' );
111 strictEqual( hello
.exists(), true, 'Message.exists returns true for existing messages' );
113 var goodbye
= mw
.message( 'goodbye' );
114 strictEqual( goodbye
.exists(), false, 'Message.exists returns false for nonexistent messages' );
116 equal( goodbye
.plain(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
118 equal( goodbye
.escaped(), '<goodbye>', 'Message.toString returns properly escaped <key> if format is "escaped" and key does not exist' );
120 ok( mw
.messages
.set( 'pluraltestmsg', 'There {{PLURAL:$1|is|are}} $1 {{PLURAL:$1|result|results}}' ), 'mw.messages.set: Register' );
121 var pluralMessage
= mw
.message( 'pluraltestmsg' , 6 );
122 equal( pluralMessage
.plain(), 'There are 6 results', 'plural get resolved when format is plain' );
123 equal( pluralMessage
.parse(), 'There are 6 results', 'plural get resolved when format is parse' );
127 test( 'mw.msg', function() {
130 ok( mw
.messages
.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
131 equal( mw
.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
132 equal( mw
.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (nonexistent message)' );
134 ok( mw
.messages
.set( 'plural-item' , 'Found $1 {{PLURAL:$1|item|items}}' ) );
135 equal( mw
.msg( 'plural-item', 5 ), 'Found 5 items', 'Apply plural for count 5' );
136 equal( mw
.msg( 'plural-item', 0 ), 'Found 0 items', 'Apply plural for count 0' );
137 equal( mw
.msg( 'plural-item', 1 ), 'Found 1 item', 'Apply plural for count 1' );
139 ok( mw
.messages
.set('gender-plural-msg' , '{{GENDER:$1|he|she|they}} {{PLURAL:$2|is|are}} awesome' ) );
140 equal( mw
.msg( 'gender-plural-msg', 'male', 1 ), 'he is awesome', 'Gender test for male, plural count 1' );
141 equal( mw
.msg( 'gender-plural-msg', 'female', '1' ), 'she is awesome', 'Gender test for female, plural count 1' );
142 equal( mw
.msg( 'gender-plural-msg', 'unknown', 10 ), 'they are awesome', 'Gender test for neutral, plural count 10' );
146 test( 'mw.loader', function() {
154 mw
.loader
.testCallback = function () {
156 strictEqual( isAwesomeDone
, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined');
157 isAwesomeDone
= true;
160 mw
.loader
.implement( 'test.callback', [QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' )], {}, {} );
162 mw
.loader
.using( 'test.callback', function () {
164 // /sample/awesome.js declares the "mw.loader.testCallback" function
165 // which contains a call to start() and ok()
166 strictEqual( isAwesomeDone
, true, "test.callback module should've caused isAwesomeDone to be true" );
167 delete mw
.loader
.testCallback
;
171 ok( false, 'Error callback fired while loader.using "test.callback" module' );
175 test( 'mw.loader.implement', function () {
178 var isJsExecuted
, $element
;
188 strictEqual( isJsExecuted
, undefined, 'javascript not executed multiple times' );
191 equal( mw
.loader
.getState( 'test.implement' ), 'loaded', 'module state is "loaded" while implement() is executing javascript' );
193 $element
= $( '<div class="mw-test-loaderimplement">Foo bar</div>' );
194 $element
.appendTo( 'body' );
196 // @broken: equal( $element.css( 'text-align' ),'center', 'CSS stylesheet was applied in time. ("text-align: center")' );
198 // Marked broken due to a bug in qunit/index.html, via Special:JavaScriptTest/qunit it works fine
200 // CSS @import is easily broken when concatenating stylesheets (bug 34669)
201 // @broken: equal( $element.css( 'float' ), 'right', 'CSS stylesheet was applied in time via @import (bug 34669). ("float: right")' );
203 equal( mw
.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'Messages are loaded in time' );
207 "all": "@import url('"
208 + QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' )
209 + '/tests/qunit/data/styleTest.css.php?'
211 selector
: '.mw-test-loaderimplement',
216 + '.mw-test-loaderimplement { text-align: center; }'
219 "test-foobar": "Hello Foobar, $1!"
223 mw
.loader
.load( 'test.implement' );
227 test( 'mw.loader bug29107' , function () {
230 // Message doesn't exist already
231 ok( !mw
.messages
.exists( 'bug29107' ) );
233 // Async! Failure in this test may lead to neither the success nor error callbacks getting called.
234 // Due to QUnit's timeout feauture we won't hang here forever if this happends.
237 mw
.loader
.implement( 'bug29107.messages-only', [], {}, {'bug29107': 'loaded'} );
238 mw
.loader
.using( 'bug29107.messages-only', function() {
240 ok( mw
.messages
.exists( 'bug29107' ), 'Bug 29107: messages-only module should implement ok' );
243 ok( false, 'Error callback fired while implementing "bug29107.messages-only" module' );
247 test( 'mw.loader.bug30825', function() {
248 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
249 // Test is for regressions!
253 // Forge an URL to the test callback script
254 var target
= QUnit
.fixurl(
255 mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
258 // Confirm that mw.loader.load() works with protocol-relative URLs
259 target
= target
.replace( /https?:/, '' );
261 equal( target
.substr( 0, 2 ), '//',
262 'URL must be relative to test relative URLs!'
267 mw
.loader
.load( target
);
270 test( 'mw.html', function() {
275 }, TypeError
, 'html.escape throws a TypeError if argument given is not a string' );
277 equal( mw
.html
.escape( '<mw awesome="awesome" value=\'test\' />' ),
278 '<mw awesome="awesome" value='test' />', 'html.escape escapes html snippet' );
280 equal( mw
.html
.element(),
281 '<undefined/>', 'html.element Always return a valid html string (even without arguments)' );
283 equal( mw
.html
.element( 'div' ), '<div/>', 'html.element DIV (simple)' );
291 '<div id="foobar"/>',
292 'html.element DIV (attribs)' );
294 equal( mw
.html
.element( 'p', null, 12 ), '<p>12</p>', 'Numbers are valid content and should be casted to a string' );
296 equal( mw
.html
.element( 'p', { title
: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
304 '<option selected="selected">Foo</option>',
305 'Attributes may have boolean values. True copies the attribute name to the value.'
315 '<option value="foo">Foo</option>',
316 'Attributes may have boolean values. False keeps the attribute from output.'
319 equal( mw
.html
.element( 'div',
322 'html.element DIV (content)' );
324 equal( mw
.html
.element( 'a',
325 { href
: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
326 '<a href="http://mediawiki.org/w/index.php?title=RL&action=history">a</a>',
327 'html.element DIV (attribs + content)' );