Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.download.pane.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 { Download: DownloadPane, Utils } = require( 'mmv.ui.reuse' );
19
20 ( function () {
21 QUnit.module( 'mmv.ui.download.pane', QUnit.newMwEnvironment() );
22
23 QUnit.test( 'Sense test, object creation and UI construction', ( assert ) => {
24 const download = new DownloadPane( $( '#qunit-fixture' ) );
25
26 assert.true( download instanceof DownloadPane, 'download UI element is created.' );
27 assert.strictEqual( download.$downloadButton.length, 1, 'Download button created.' );
28 assert.strictEqual( download.$downloadSizeMenu.length, 1, 'Image size pulldown menu created.' );
29 assert.strictEqual( download.$downloadSizeMenu.children().length, 5, 'Image size pulldown menu created.' );
30 assert.strictEqual( download.$previewLink.length, 1, 'Preview link created.' );
31
32 assert.strictEqual( download.$downloadButton.attr( 'href' ), undefined, 'Button href is empty.' );
33 assert.strictEqual( download.$previewLink.attr( 'href' ), undefined, 'Preview link href is empty.' );
34 } );
35
36 QUnit.test( 'set()/empty():', ( assert ) => {
37 const download = new DownloadPane( $( '#qunit-fixture' ) );
38 const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
39 const image = { // fake ImageModel
40 title: new mw.Title( 'File:Foobar.jpg' ),
41 url: src
42 };
43
44 assert.strictEqual( download.imageExtension, undefined, 'Image extension is not set.' );
45
46 const updateMenuOptions = Utils.updateMenuOptions;
47 Utils.updateMenuOptions = function () {
48 assert.true( true, 'Menu options updated.' );
49 };
50
51 download.setAttributionText = () => {};
52 download.set( image );
53
54 assert.strictEqual( download.imageExtension, 'jpg', 'Image extension is set correctly.' );
55
56 download.empty();
57
58 assert.strictEqual( download.imageExtension, undefined, 'Image extension is not set.' );
59
60 Utils.updateMenuOptions = updateMenuOptions;
61 } );
62
63 QUnit.test( 'handleSizeSwitch():', ( assert ) => {
64 const download = new DownloadPane( $( '#qunit-fixture' ) );
65 const newImageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/NewFoobar.jpg';
66
67 const getThumbnailUrlPromise = Utils.getThumbnailUrlPromise;
68 Utils.getThumbnailUrlPromise = function () {
69 return $.Deferred().resolve( { url: newImageUrl } ).promise();
70 };
71
72 download.setDownloadUrl = function ( url ) {
73 assert.strictEqual( url, newImageUrl, 'URL passed to setDownloadUrl is correct' );
74 };
75
76 download.handleSizeSwitch();
77
78 download.image = { url: newImageUrl };
79
80 Utils.getThumbnailUrlPromise = function () {
81 assert.true( false, 'Should not fetch the thumbnail if the image is original size.' );
82 };
83
84 download.handleSizeSwitch();
85
86 Utils.getThumbnailUrlPromise = getThumbnailUrlPromise;
87 } );
88
89 QUnit.test( 'getExtensionFromUrl():', ( assert ) => {
90 const download = new DownloadPane( $( '#qunit-fixture' ) );
91
92 assert.strictEqual( download.getExtensionFromUrl( 'http://example.com/bing/foo.bar.png' ),
93 'png', 'Extension is parsed correctly' );
94 } );
95
96 QUnit.test( 'setDownloadUrl', ( assert ) => {
97 const download = new DownloadPane( $( '#qunit-fixture' ) );
98 const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/NewFoobar.jpg';
99
100 download.setDownloadUrl( imageUrl );
101
102 assert.strictEqual( download.$downloadButton.attr( 'href' ), imageUrl + '?download', 'Download link is set correctly.' );
103 assert.strictEqual( download.$previewLink.attr( 'href' ), imageUrl, 'Preview link is set correctly.' );
104 assert.strictEqual( download.$downloadButton.hasClass( 'disabledLink' ), false, 'Download link is enabled.' );
105 } );
106 }() );