Renaming qunit test files to end in ".test.js" (finally!)
[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(16);
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, 'parse', 'Message property "format" defaults to "parse"' );
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.toString(), '<goodbye>', 'Message.toString returns <key> if key does not exist' );
117 });
118
119 test( 'mw.msg', function() {
120 expect(2);
121
122 equal( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
123 equal( mw.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (inexisting message)' );
124 });
125
126 test( 'mw.loader', function() {
127 expect(5);
128
129 // Regular expression to extract the path for the QUnit tests
130 // Takes into account that tests could be run from a file:// URL
131 // by excluding the 'index.html' part from the URL
132 var rePath = /(?:[^#\?](?!index.html))*\/?/;
133
134 // Four assertions to test the above regular expression:
135 equal(
136 rePath.exec( 'http://path/to/tests/?foobar' )[0],
137 "http://path/to/tests/",
138 "Extracting path from http URL with query"
139 );
140 equal(
141 rePath.exec( 'http://path/to/tests/#frag' )[0],
142 "http://path/to/tests/",
143 "Extracting path from http URL with fragment"
144 );
145 equal(
146 rePath.exec( 'file://path/to/tests/index.html?foobar' )[0],
147 "file://path/to/tests/",
148 "Extracting path from local URL (file://) with query"
149 );
150 equal(
151 rePath.exec( 'file://path/to/tests/index.html#frag' )[0],
152 "file://path/to/tests/",
153 "Extracting path from local URL (file://) with fragment"
154 );
155
156 // Asynchronous ahead
157 stop(5000);
158
159 // Extract path
160 var tests_path = rePath.exec( location.href );
161
162 mw.loader.implement( 'is.awesome', [QUnit.fixurl( tests_path + 'data/defineTestCallback.js')], {}, {} );
163
164 mw.loader.using( 'is.awesome', function() {
165
166 // /sample/awesome.js declares the "mw.loader.testCallback" function
167 // which contains a call to start() and ok()
168 mw.loader.testCallback();
169 mw.loader.testCallback = undefined;
170
171 }, function() {
172 start();
173 ok( false, 'Error callback fired while implementing "is.awesome" module' );
174 });
175
176 });
177
178 test( 'mw.loader.bug29107' , function() {
179 expect(2);
180
181 // Message doesn't exist already
182 ok( !mw.messages.exists( 'bug29107' ) );
183
184 // Async! Include a timeout, as failure in this test leads to neither the
185 // success nor failure callbacks getting called.
186 stop(5000);
187
188 mw.loader.implement( 'bug29107.messages-only', [], {}, {'bug29107': 'loaded'} );
189 mw.loader.using( 'bug29107.messages-only', function() {
190 start();
191 ok( mw.messages.exists( 'bug29107' ), 'Bug 29107: messages-only module should implement ok' );
192 }, function() {
193 start();
194 ok( false, 'Error callback fired while implementing "bug29107.messages-only" module' );
195 });
196 });
197
198 test( 'mw.html', function() {
199 expect(7);
200
201 raises( function(){
202 mw.html.escape();
203 }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
204
205 equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
206 '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'html.escape escapes html snippet' );
207
208 equal( mw.html.element(),
209 '<undefined/>', 'html.element Always return a valid html string (even without arguments)' );
210
211 equal( mw.html.element( 'div' ), '<div/>', 'html.element DIV (simple)' );
212
213 equal( mw.html.element( 'div',
214 { id: 'foobar' } ),
215 '<div id="foobar"/>',
216 'html.element DIV (attribs)' );
217
218 equal( mw.html.element( 'div',
219 null, 'a' ),
220 '<div>a</div>',
221 'html.element DIV (content)' );
222
223 equal( mw.html.element( 'a',
224 { href: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
225 '<a href="http://mediawiki.org/w/index.php?title=RL&amp;action=history">a</a>',
226 'html.element DIV (attribs + content)' );
227
228 });