| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { Utils } = require( 'mmv.ui.reuse' ); |
| 19 |
|
| 20 |
( function () { |
| 21 |
QUnit.module( 'mmv.ui.reuse.utils', QUnit.newMwEnvironment() ); |
| 22 |
|
| 23 |
QUnit.test( 'createSelectMenu():', ( assert ) => { |
| 24 |
const menuItems = [ 'original', 'small', 'medium', 'large' ]; |
| 25 |
const def = 'large'; |
| 26 |
const $select = Utils.createSelectMenu( |
| 27 |
menuItems, |
| 28 |
def |
| 29 |
); |
| 30 |
const $options = $select.children(); |
| 31 |
|
| 32 |
assert.strictEqual( $options.length, 4, 'Menu has correct number of items.' ); |
| 33 |
|
| 34 |
for ( let i = 0; i < menuItems.length; i++ ) { |
| 35 |
const $option = $( $options[ i ] ); |
| 36 |
|
| 37 |
assert.strictEqual( $option.data( 'name' ), menuItems[ i ], 'Correct item name on the list.' ); |
| 38 |
assert.strictEqual( $option.data( 'height' ), undefined, 'Correct item height on the list.' ); |
| 39 |
assert.strictEqual( $option.data( 'width' ), undefined, 'Correct item width on the list.' ); |
| 40 |
} |
| 41 |
} ); |
| 42 |
|
| 43 |
QUnit.test( 'updateSelectOptions():', ( assert ) => { |
| 44 |
const $select = Utils.createSelectMenu( |
| 45 |
[ 'original', 'small', 'medium', 'large' ], |
| 46 |
'original' |
| 47 |
); |
| 48 |
const $options = $select.children(); |
| 49 |
const width = 700; |
| 50 |
const height = 500; |
| 51 |
const sizes = Utils.getPossibleImageSizesForHtml( width, height ); |
| 52 |
const oldMessage = mw.message; |
| 53 |
|
| 54 |
mw.message = function ( messageKey ) { |
| 55 |
assert.true( /^multimediaviewer-(small|medium|large|original|embed-dimensions)/.test( messageKey ), 'messageKey passed correctly.' ); |
| 56 |
|
| 57 |
return { text: function () {} }; |
| 58 |
}; |
| 59 |
|
| 60 |
Utils.updateSelectOptions( sizes, $options ); |
| 61 |
|
| 62 |
mw.message = oldMessage; |
| 63 |
} ); |
| 64 |
|
| 65 |
QUnit.test( 'getPossibleImageSizesForHtml()', ( assert ) => { |
| 66 |
const exampleSizes = [ |
| 67 |
{ |
| 68 |
test: 'Extra large wide image', |
| 69 |
width: 6000, height: 4000, |
| 70 |
expected: { |
| 71 |
small: { width: 500, height: 333 }, |
| 72 |
medium: { width: 1080, height: 720 }, |
| 73 |
large: { width: 1620, height: 1080 }, |
| 74 |
xl: { width: 3240, height: 2160 }, |
| 75 |
original: { width: 6000, height: 4000 } |
| 76 |
} |
| 77 |
}, |
| 78 |
|
| 79 |
{ |
| 80 |
test: 'Big wide image', |
| 81 |
width: 2048, height: 1536, |
| 82 |
expected: { |
| 83 |
small: { width: 500, height: 375 }, |
| 84 |
medium: { width: 960, height: 720 }, |
| 85 |
large: { width: 1440, height: 1080 }, |
| 86 |
original: { width: 2048, height: 1536 } |
| 87 |
} |
| 88 |
}, |
| 89 |
|
| 90 |
{ |
| 91 |
test: 'Big tall image', |
| 92 |
width: 201, height: 1536, |
| 93 |
expected: { |
| 94 |
small: { width: 63, height: 480 }, |
| 95 |
medium: { width: 94, height: 720 }, |
| 96 |
large: { width: 141, height: 1080 }, |
| 97 |
original: { width: 201, height: 1536 } |
| 98 |
} |
| 99 |
}, |
| 100 |
|
| 101 |
{ |
| 102 |
test: 'Very small image', |
| 103 |
width: 15, height: 20, |
| 104 |
expected: { |
| 105 |
original: { width: 15, height: 20 } |
| 106 |
} |
| 107 |
} |
| 108 |
]; |
| 109 |
for ( let i = 0; i < exampleSizes.length; i++ ) { |
| 110 |
const cursize = exampleSizes[ i ]; |
| 111 |
const opts = Utils.getPossibleImageSizesForHtml( cursize.width, cursize.height ); |
| 112 |
assert.deepEqual( opts, cursize.expected, 'Size calculation for "' + cursize.test + '" gives expected results' ); |
| 113 |
} |
| 114 |
} ); |
| 115 |
|
| 116 |
}() ); |