Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.reuse.embed.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { Embed, Utils } = require( 'mmv.ui.reuse' );
19
20 ( function () {
21 const $qf = $( '#qunit-fixture' );
22
23 QUnit.module( 'mmv.ui.reuse.Embed', QUnit.newMwEnvironment() );
24
25 QUnit.test( 'Sense test, object creation and UI construction', ( assert ) => {
26 const embed = new Embed( $qf );
27
28 assert.true( embed instanceof Embed, 'Embed UI element is created.' );
29 assert.true( embed.$embedTextHtml[ 0 ] instanceof HTMLElement, 'Html snipped text area created.' );
30 assert.true( embed.$embedTextWikitext[ 0 ] instanceof HTMLElement, 'Wikitext snipped text area created.' );
31 assert.true( embed.$embedSizeSwitchWikitext[ 0 ] instanceof HTMLElement, 'Size selection menu for wikitext created.' );
32 assert.true( embed.$embedSizeSwitchHtml[ 0 ] instanceof HTMLElement, 'Size selection menu for html created.' );
33 } );
34
35 QUnit.test( 'handleSizeSwitch(): Skip if no item selected.', ( assert ) => {
36 const embed = new Embed( $qf );
37
38 assert.expect( 0 );
39
40 embed.$embedSizeSwitchHtml.val( undefined );
41 embed.$embedSizeSwitchWikitext.val( undefined );
42
43 embed.updateEmbedHtml = function () {
44 assert.true( false, 'No item selected, this should not have been called.' );
45 };
46 embed.updateEmbedWikitext = function () {
47 assert.true( false, 'No item selected, this should not have been called.' );
48 };
49
50 embed.handleSizeSwitch();
51 } );
52
53 QUnit.test( 'handleSizeSwitch(): HTML/Wikitext size menu item selected.', ( assert ) => {
54 const embed = new Embed( $qf );
55 const width = '10';
56 const height = '20';
57
58 const $option = embed.$embedSizeSwitchHtml.children().first();
59 $option.data( 'width', width );
60 $option.data( 'height', height );
61 embed.$embedSizeSwitchHtml.val( $option.val() );
62
63 embed.updateEmbedHtml = function ( thumb, w, h ) {
64 assert.strictEqual( thumb.url, undefined, 'Empty thumbnail passed.' );
65 assert.strictEqual( w, width, 'Correct width passed.' );
66 assert.strictEqual( h, height, 'Correct height passed.' );
67 };
68 embed.updateEmbedWikitext = function () {
69 // ignore
70 };
71 embed.select = function () {
72 assert.true( true, 'Item selected after update.' );
73 };
74
75 embed.handleSizeSwitch();
76 } );
77
78 QUnit.test( 'handleSizeSwitch(): Wikitext size menu item selected.', ( assert ) => {
79 const embed = new Embed( $qf );
80 const width = '10';
81 const height = '20';
82
83 const $option = embed.$embedSizeSwitchWikitext.children().first();
84 $option.data( 'width', width );
85 $option.data( 'height', height );
86 embed.$embedSizeSwitchWikitext.val( $option.val() );
87
88 embed.updateEmbedHtml = function () {
89 // ignore
90 };
91 embed.updateEmbedWikitext = function ( w ) {
92 assert.strictEqual( w, width, 'Correct width passed.' );
93 };
94 embed.select = function () {
95 assert.true( true, 'Item selected after update.' );
96 };
97
98 embed.handleSizeSwitch();
99 } );
100
101 QUnit.test( 'updateEmbedHtml(): Do nothing if set() not called before.', ( assert ) => {
102 const embed = new Embed( $qf );
103 const width = 10;
104 const height = 20;
105
106 assert.expect( 0 );
107
108 embed.formatter.getThumbnailHtml = function () {
109 assert.true( false, 'formatter.getThumbnailHtml() should not have been called.' );
110 };
111 embed.updateEmbedHtml( {}, width, height );
112 } );
113
114 QUnit.test( 'updateEmbedHtml():', ( assert ) => {
115 const embed = new Embed( $qf );
116 const title = mw.Title.newFromText( 'File:Foobar.jpg' );
117 const url = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
118 const thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg';
119 const imageInfo = { url, title };
120 const caption = '-';
121 const alt = undefined;
122 const info = {
123 imageInfo,
124 caption,
125 alt
126 };
127 let width = 10;
128 const height = 20;
129
130 embed.resetCurrentSizeMenuToDefault = () => {};
131
132 embed.set( imageInfo, caption );
133
134 // Small image, no thumbnail info is passed
135 embed.formatter.getThumbnailHtml = function ( i, u, w, h ) {
136 assert.deepEqual( i, info, 'Info passed correctly.' );
137 assert.strictEqual( u, url, 'Image URL passed correctly.' );
138 assert.strictEqual( w, width, 'Correct width passed.' );
139 assert.strictEqual( h, height, 'Correct height passed.' );
140 };
141 embed.updateEmbedHtml( {}, width, height );
142
143 // Small image, thumbnail info present
144 embed.formatter.getThumbnailHtml = function ( i, u ) {
145 assert.strictEqual( u, thumbUrl, 'Image src passed correctly.' );
146 };
147 embed.updateEmbedHtml( { url: thumbUrl }, width, height );
148
149 // Big image, thumbnail info present
150 embed.formatter.getThumbnailHtml = function ( i, u ) {
151 assert.strictEqual( u, url, 'Image src passed correctly.' );
152 };
153 width = 1300;
154 embed.updateEmbedHtml( { url: thumbUrl }, width, height );
155 } );
156
157 QUnit.test( 'updateEmbedWikitext(): Do nothing if set() not called before.', ( assert ) => {
158 const embed = new Embed( $qf );
159 const width = 10;
160
161 assert.expect( 0 );
162
163 embed.formatter.getThumbnailWikitext = function () {
164 assert.true( false, 'formatter.getThumbnailWikitext() should not have been called.' );
165 };
166 embed.updateEmbedWikitext( width );
167 } );
168
169 QUnit.test( 'updateEmbedWikitext():', ( assert ) => {
170 const embed = new Embed( $qf );
171 const title = mw.Title.newFromText( 'File:Foobar.jpg' );
172 const url = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
173
174 const imageInfo = { url, title };
175 const caption = '-';
176 const alt = undefined;
177 const info = {
178 imageInfo,
179 caption,
180 alt
181 };
182 const width = 10;
183
184 embed.resetCurrentSizeMenuToDefault = () => {};
185
186 embed.set( imageInfo, caption );
187
188 embed.formatter.getThumbnailWikitextFromEmbedFileInfo = function ( i, w ) {
189 assert.deepEqual( i, info, 'EmbedFileInfo passed correctly.' );
190 assert.strictEqual( w, width, 'Width passed correctly.' );
191 };
192 embed.updateEmbedWikitext( width );
193 } );
194
195 QUnit.test( 'getPossibleImageSizesForWikitext()', ( assert ) => {
196 const embed = new Embed( $qf );
197 const exampleSizes = [
198 // Big wide image
199 {
200 width: 2048, height: 1536,
201 expected: {
202 small: { width: 300, height: 225 },
203 medium: { width: 400, height: 300 },
204 large: { width: 500, height: 375 },
205 default: { width: null, height: null }
206 }
207 },
208
209 // Big tall image
210 {
211 width: 201, height: 1536,
212 expected: {
213 default: { width: null, height: null }
214 }
215 },
216
217 // Very small image
218 {
219 width: 15, height: 20,
220 expected: {
221 default: { width: null, height: null }
222 }
223 }
224 ];
225 for ( let i = 0; i < exampleSizes.length; i++ ) {
226 const cursize = exampleSizes[ i ];
227 const opts = embed.getPossibleImageSizesForWikitext( cursize.width, cursize.height );
228 assert.deepEqual( opts, cursize.expected, 'We got the expected results out of the size calculation function.' );
229 }
230 } );
231
232 QUnit.test( 'set():', ( assert ) => {
233 const embed = new Embed( $qf );
234 const width = 15;
235 const height = 20;
236
237 const updateMenuOptions = Utils.updateMenuOptions;
238 Utils.updateMenuOptions = function ( sizes, options ) {
239 assert.strictEqual( options.length, 4, 'Options passed correctly.' );
240 };
241 embed.resetCurrentSizeMenuToDefault = function () {
242 assert.true( true, 'resetCurrentSizeMenuToDefault() is called.' );
243 };
244 const getThumbnailUrlPromise = Utils.getThumbnailUrlPromise;
245 Utils.getThumbnailUrlPromise = function () {
246 return $.Deferred().resolve().promise();
247 };
248 embed.updateEmbedHtml = function () {
249 assert.true( true, 'updateEmbedHtml() is called after data is collected.' );
250 };
251
252 assert.false( $.isPlainObject( embed.embedFileInfo ), 'embedFileInfo not set yet.' );
253
254 embed.set( { width, height }, 'caption' );
255
256 assert.true( $.isPlainObject( embed.embedFileInfo ), 'embedFileInfo set.' );
257
258 Utils.updateMenuOptions = updateMenuOptions;
259 Utils.getThumbnailUrlPromise = getThumbnailUrlPromise;
260 } );
261
262 QUnit.test( 'empty():', ( assert ) => {
263 const embed = new Embed( $qf );
264 const width = 15;
265 const height = 20;
266
267 embed.formatter = {
268 getThumbnailWikitextFromEmbedFileInfo: function () {
269 return 'wikitext';
270 },
271 getThumbnailHtml: function () {
272 return 'html';
273 }
274 };
275
276 embed.set( {}, {} );
277 embed.updateEmbedHtml( { url: 'x' }, width, height );
278 embed.updateEmbedWikitext( width );
279
280 assert.notStrictEqual( embed.$embedTextHtml.val(), '', 'embedTextHtml is not empty.' );
281 assert.notStrictEqual( embed.$embedTextWikitext.val(), '', 'embedTextWikitext is not empty.' );
282
283 embed.empty();
284
285 assert.strictEqual( embed.$embedTextHtml.val(), '', 'embedTextHtml is empty.' );
286 assert.strictEqual( embed.$embedTextWikitext.val(), '', 'embedTextWikitext is empty.' );
287 assert.strictEqual( embed.$embedSizeSwitchHtml.css( 'display' ), 'none', 'Html size menu should be hidden.' );
288 assert.strictEqual( embed.$embedSizeSwitchWikitext.css( 'display' ), 'none', 'Wikitext size menu should be hidden.' );
289 } );
290
291 QUnit.test( 'attach()/unattach():', ( assert ) => {
292 const embed = new Embed( $qf );
293 const width = 15;
294 const height = 20;
295
296 embed.resetCurrentSizeMenuToDefault = () => {};
297
298 embed.set( { width, height }, 'caption' );
299
300 embed.handleTypeSwitch = function () {
301 assert.true( false, 'handleTypeSwitch should not have been called.' );
302 };
303 embed.handleSizeSwitch = function () {
304 assert.true( false, 'handleTypeSwitch should not have been called.' );
305 };
306
307 // Triggering action events before attaching should do nothing
308 embed.$embedSizeSwitchHtml.trigger( 'change' );
309 embed.$embedSizeSwitchWikitext.trigger( 'change' );
310
311 embed.handleTypeSwitch = function () {
312 assert.true( true, 'handleTypeSwitch was called.' );
313 };
314 embed.handleSizeSwitch = function () {
315 assert.true( true, 'handleTypeSwitch was called.' );
316 };
317
318 embed.attach();
319
320 // Action events should be handled now
321 embed.$embedSizeSwitchHtml.trigger( 'change' );
322 embed.$embedSizeSwitchWikitext.trigger( 'change' );
323
324 // Test the unattach part
325 embed.handleTypeSwitch = function () {
326 assert.true( false, 'handleTypeSwitch should not have been called.' );
327 };
328 embed.handleSizeSwitch = function () {
329 assert.true( false, 'handleTypeSwitch should not have been called.' );
330 };
331
332 embed.unattach();
333
334 // Triggering action events now that we are unattached should do nothing
335 embed.$embedSizeSwitchHtml.trigger( 'change' );
336 embed.$embedSizeSwitchWikitext.trigger( 'change' );
337 } );
338
339 QUnit.test( 'handleTypeSwitch():', ( assert ) => {
340 const embed = new Embed( $qf );
341
342 embed.resetCurrentSizeMenuToDefault = function () {
343 assert.true( true, 'resetCurrentSizeMenuToDefault() called.' );
344 };
345
346 // HTML selected
347 embed.handleTypeSwitch( 'html' );
348
349 assert.strictEqual( embed.$embedSizeSwitchWikitext.css( 'display' ), 'none', 'Wikitext size menu should be hidden.' );
350
351 embed.resetCurrentSizeMenuToDefault = function () {
352 assert.true( false, 'resetCurrentSizeMenuToDefault() should not have been called.' );
353 };
354
355 // Wikitext selected, we are done resetting defaults
356 embed.handleTypeSwitch( 'wikitext' );
357
358 assert.strictEqual( embed.$embedSizeSwitchHtml.css( 'display' ), 'none', 'HTML size menu should be hidden.' );
359 } );
360
361 QUnit.test( 'Logged out', ( assert ) => {
362 const oldUserIsAnon = mw.user.isAnon;
363
364 mw.user.isAnon = () => true;
365
366 const embed = new Embed( $qf );
367
368 embed.attach();
369
370 assert.strictEqual( embed.$embedSizeSwitchWikitext.css( 'display' ), 'none', 'Wikitext widget should be hidden.' );
371 assert.strictEqual( embed.$embedSizeSwitchHtml.css( 'display' ), '', 'HTML widget should be visible.' );
372 assert.strictEqual( embed.$embedTextWikitextDiv.css( 'display' ), 'none', 'Wikitext input should be hidden.' );
373 assert.strictEqual( embed.$embedTextHtmlDiv.css( 'display' ), '', 'HTML input should be visible.' );
374
375 mw.user.isAnon = oldUserIsAnon;
376 } );
377
378 }() );