mediawiki.html: mediawiki.html: Add support for numbers and booleans
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.test.js
1 module( 'mediawiki' );
2
3 test( '-- Initial check', function() {
4 expect(8);
5
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' );
11
12 ok( window.mediaWiki, 'mediaWiki defined' );
13 ok( window.mw, 'mw defined' );
14 strictEqual( window.mw, window.mediaWiki, 'mw alias to mediaWiki' );
15 });
16
17 test( 'mw.Map', function() {
18 expect(17);
19
20 ok( mw.Map, 'mw.Map defined' );
21
22 var conf = new mw.Map(),
23 // Dummy variables
24 funky = function() {},
25 arry = [],
26 nummy = 7;
27
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)' );
37
38 // Multiple values at once
39 var someValues = {
40 'foo': 'bar',
41 'lorem': 'ipsum',
42 'MediaWiki': true
43 };
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'] ), {
46 'foo': 'bar',
47 'lorem': 'ipsum'
48 }, 'Map.get returns multiple values correctly as an object' );
49
50 deepEqual( conf.get( ['foo', 'notExist'] ), {
51 'foo': 'bar',
52 'notExist': null
53 }, 'Map.get return includes keys that were not found as null values' );
54
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' );
57
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)' );
60
61 conf.set( 'globalMapChecker', 'Hi' );
62
63 ok( false === 'globalMapChecker' in window, 'new mw.Map did not store its values in the global window object by default' );
64
65 var globalConf = new mw.Map( true );
66 globalConf.set( 'anotherGlobalMapChecker', 'Hello' );
67
68 ok( 'anotherGlobalMapChecker' in window, 'new mw.Map( true ) did store its values in the global window object' );
69
70 // Whitelist this global variable for QUnit's 'noglobal' mode
71 if ( QUnit.config.noglobals ) {
72 QUnit.config.pollution.push( 'anotherGlobalMapChecker' );
73 }
74 });
75
76 test( 'mw.config', function() {
77 expect(1);
78
79 ok( mw.config instanceof mw.Map, 'mw.config instance of mw.Map' );
80 });
81
82 test( 'mw.message & mw.messages', function() {
83 expect(17);
84
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' );
88
89 var hello = mw.message( 'hello' );
90
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' );
95
96 // Todo
97 ok( hello.params, 'Message prototype "params"' );
98
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"' );
101
102 equal( hello.escaped(), 'Hello &lt;b&gt;awesome&lt;/b&gt; world', 'Message.escaped returns the escaped message' );
103 equal( hello.format, 'escaped', 'Message.escaped correctly updated the "format" property' );
104
105 hello.parse();
106 equal( hello.format, 'parse', 'Message.parse correctly updated the "format" property' );
107
108 hello.plain();
109 equal( hello.format, 'plain', 'Message.plain correctly updated the "format" property' );
110
111 strictEqual( hello.exists(), true, 'Message.exists returns true for existing messages' );
112
113 var goodbye = mw.message( 'goodbye' );
114 strictEqual( goodbye.exists(), false, 'Message.exists returns false for inexisting messages' );
115
116 equal( goodbye.plain(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
117 // bug 30684
118 equal( goodbye.escaped(), '&lt;goodbye&gt;', 'Message.toString returns properly escaped &lt;key&gt; if format is "escaped" and key does not exist' );
119 });
120
121 test( 'mw.msg', function() {
122 expect(3);
123
124 ok( mw.messages.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
125
126 equal( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
127 equal( mw.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (inexisting message)' );
128 });
129
130 test( 'mw.loader', function() {
131 expect(1);
132
133 // Asynchronous ahead
134 stop(5000);
135
136 mw.loader.implement( 'is.awesome', [QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/defineTestCallback.js' )], {}, {} );
137
138 mw.loader.using( 'is.awesome', function() {
139
140 // /sample/awesome.js declares the "mw.loader.testCallback" function
141 // which contains a call to start() and ok()
142 mw.loader.testCallback();
143 mw.loader.testCallback = undefined;
144
145 }, function() {
146 start();
147 ok( false, 'Error callback fired while implementing "is.awesome" module' );
148 });
149
150 });
151
152 test( 'mw.loader.bug29107' , function() {
153 expect(2);
154
155 // Message doesn't exist already
156 ok( !mw.messages.exists( 'bug29107' ) );
157
158 // Async! Include a timeout, as failure in this test leads to neither the
159 // success nor failure callbacks getting called.
160 stop(5000);
161
162 mw.loader.implement( 'bug29107.messages-only', [], {}, {'bug29107': 'loaded'} );
163 mw.loader.using( 'bug29107.messages-only', function() {
164 start();
165 ok( mw.messages.exists( 'bug29107' ), 'Bug 29107: messages-only module should implement ok' );
166 }, function() {
167 start();
168 ok( false, 'Error callback fired while implementing "bug29107.messages-only" module' );
169 });
170 });
171
172 test( 'mw.html', function() {
173 expect(11);
174
175 raises( function(){
176 mw.html.escape();
177 }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
178
179 equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
180 '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'html.escape escapes html snippet' );
181
182 equal( mw.html.element(),
183 '<undefined/>', 'html.element Always return a valid html string (even without arguments)' );
184
185 equal( mw.html.element( 'div' ), '<div/>', 'html.element DIV (simple)' );
186
187 equal(
188 mw.html.element(
189 'div', {
190 id: 'foobar'
191 }
192 ),
193 '<div id="foobar"/>',
194 'html.element DIV (attribs)' );
195
196 equal( mw.html.element( 'p', null, 12 ), '<p>12</p>', 'Numbers are valid content and should be casted to a string' );
197
198 equal( mw.html.element( 'p', { title: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
199
200 equal(
201 mw.html.element(
202 'option', {
203 selected: true
204 }, 'Foo'
205 ),
206 '<option selected="selected">Foo</option>',
207 'Attributes may have boolean values. True copies the attribute name to the value.'
208 );
209
210 equal(
211 mw.html.element(
212 'option', {
213 value: 'foo',
214 selected: false
215 }, 'Foo'
216 ),
217 '<option value="foo">Foo</option>',
218 'Attributes may have boolean values. False keeps the attribute from output.'
219 );
220
221 equal( mw.html.element( 'div',
222 null, 'a' ),
223 '<div>a</div>',
224 'html.element DIV (content)' );
225
226 equal( mw.html.element( 'a',
227 { href: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
228 '<a href="http://mediawiki.org/w/index.php?title=RL&amp;action=history">a</a>',
229 'html.element DIV (attribs + content)' );
230
231 });