6fc856736b82a7cf12521bf458ee4bdf26612440
[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(1);
128
129 // Asynchronous ahead
130 stop(5000);
131
132 mw.loader.implement( 'is.awesome', [QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/defineTestCallback.js' )], {}, {} );
133
134 mw.loader.using( 'is.awesome', function() {
135
136 // /sample/awesome.js declares the "mw.loader.testCallback" function
137 // which contains a call to start() and ok()
138 mw.loader.testCallback();
139 mw.loader.testCallback = undefined;
140
141 }, function() {
142 start();
143 ok( false, 'Error callback fired while implementing "is.awesome" module' );
144 });
145
146 });
147
148 test( 'mw.loader.bug29107' , function() {
149 expect(2);
150
151 // Message doesn't exist already
152 ok( !mw.messages.exists( 'bug29107' ) );
153
154 // Async! Include a timeout, as failure in this test leads to neither the
155 // success nor failure callbacks getting called.
156 stop(5000);
157
158 mw.loader.implement( 'bug29107.messages-only', [], {}, {'bug29107': 'loaded'} );
159 mw.loader.using( 'bug29107.messages-only', function() {
160 start();
161 ok( mw.messages.exists( 'bug29107' ), 'Bug 29107: messages-only module should implement ok' );
162 }, function() {
163 start();
164 ok( false, 'Error callback fired while implementing "bug29107.messages-only" module' );
165 });
166 });
167
168 test( 'mw.html', function() {
169 expect(7);
170
171 raises( function(){
172 mw.html.escape();
173 }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
174
175 equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
176 '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'html.escape escapes html snippet' );
177
178 equal( mw.html.element(),
179 '<undefined/>', 'html.element Always return a valid html string (even without arguments)' );
180
181 equal( mw.html.element( 'div' ), '<div/>', 'html.element DIV (simple)' );
182
183 equal( mw.html.element( 'div',
184 { id: 'foobar' } ),
185 '<div id="foobar"/>',
186 'html.element DIV (attribs)' );
187
188 equal( mw.html.element( 'div',
189 null, 'a' ),
190 '<div>a</div>',
191 'html.element DIV (content)' );
192
193 equal( mw.html.element( 'a',
194 { href: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
195 '<a href="http://mediawiki.org/w/index.php?title=RL&amp;action=history">a</a>',
196 'html.element DIV (attribs + content)' );
197
198 });