| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { ReuseDialog } = require( 'mmv.ui.reuse' ); |
| 19 |
|
| 20 |
( function () { |
| 21 |
function makeReuseDialog( sandbox ) { |
| 22 |
const $fixture = $( '#qunit-fixture' ); |
| 23 |
const config = { getFromLocalStorage: sandbox.stub(), setInLocalStorage: sandbox.stub() }; |
| 24 |
return new ReuseDialog( $fixture, $( '<div>' ).appendTo( $fixture ), config ); |
| 25 |
} |
| 26 |
|
| 27 |
QUnit.module( 'mmv.ui.reuse.Dialog', QUnit.newMwEnvironment() ); |
| 28 |
|
| 29 |
QUnit.test( 'Sense test, object creation and UI construction', function ( assert ) { |
| 30 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 31 |
|
| 32 |
assert.true( reuseDialog instanceof ReuseDialog, 'Reuse UI element is created.' ); |
| 33 |
assert.strictEqual( reuseDialog.$dialog.length, 1, 'Reuse dialog div created.' ); |
| 34 |
} ); |
| 35 |
|
| 36 |
QUnit.test( 'handleOpenCloseClick():', function ( assert ) { |
| 37 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 38 |
|
| 39 |
reuseDialog.openDialog = function () { |
| 40 |
assert.true( true, 'openDialog called.' ); |
| 41 |
}; |
| 42 |
reuseDialog.closeDialog = function () { |
| 43 |
assert.true( false, 'closeDialog should not have been called.' ); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
reuseDialog.handleOpenCloseClick(); |
| 48 |
|
| 49 |
reuseDialog.openDialog = function () { |
| 50 |
assert.true( false, 'openDialog should not have been called.' ); |
| 51 |
}; |
| 52 |
reuseDialog.closeDialog = function () { |
| 53 |
assert.true( true, 'closeDialog called.' ); |
| 54 |
}; |
| 55 |
reuseDialog.isOpen = true; |
| 56 |
|
| 57 |
|
| 58 |
reuseDialog.handleOpenCloseClick(); |
| 59 |
} ); |
| 60 |
|
| 61 |
QUnit.test( 'attach()/unattach():', function ( assert ) { |
| 62 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 63 |
|
| 64 |
reuseDialog.handleOpenCloseClick = function () { |
| 65 |
assert.true( false, 'handleOpenCloseClick should not have been called.' ); |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
$( document ).trigger( 'mmv-reuse-open' ); |
| 70 |
|
| 71 |
reuseDialog.handleOpenCloseClick = function () { |
| 72 |
assert.true( true, 'handleOpenCloseClick called.' ); |
| 73 |
}; |
| 74 |
|
| 75 |
reuseDialog.attach(); |
| 76 |
|
| 77 |
|
| 78 |
$( document ).trigger( 'mmv-reuse-open' ); |
| 79 |
|
| 80 |
|
| 81 |
reuseDialog.handleOpenCloseClick = function () { |
| 82 |
assert.true( false, 'handleOpenCloseClick should not have been called.' ); |
| 83 |
}; |
| 84 |
|
| 85 |
reuseDialog.unattach(); |
| 86 |
|
| 87 |
|
| 88 |
$( document ).trigger( 'mmv-reuse-open' ); |
| 89 |
} ); |
| 90 |
|
| 91 |
QUnit.test( 'start/stopListeningToOutsideClick():', function ( assert ) { |
| 92 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 93 |
const realCloseDialog = reuseDialog.closeDialog; |
| 94 |
|
| 95 |
function clickOutsideDialog() { |
| 96 |
const event = new $.Event( 'click', { target: reuseDialog.$container[ 0 ] } ); |
| 97 |
reuseDialog.$container.trigger( event ); |
| 98 |
return event; |
| 99 |
} |
| 100 |
function clickInsideDialog() { |
| 101 |
const event = new $.Event( 'click', { target: reuseDialog.$dialog[ 0 ] } ); |
| 102 |
reuseDialog.$dialog.trigger( event ); |
| 103 |
return event; |
| 104 |
} |
| 105 |
|
| 106 |
function assertDialogDoesNotCatchClicks() { |
| 107 |
reuseDialog.closeDialog = () => assert.true( false, 'Dialog is not affected by click' ); |
| 108 |
const event = clickOutsideDialog(); |
| 109 |
assert.strictEqual( event.isDefaultPrevented(), false, 'Dialog does not affect click' ); |
| 110 |
assert.strictEqual( event.isPropagationStopped(), false, 'Dialog does not affect click propagation' ); |
| 111 |
} |
| 112 |
function assertDialogCatchesOutsideClicksOnly() { |
| 113 |
reuseDialog.closeDialog = () => assert.true( false, 'Dialog is not affected by inside click' ); |
| 114 |
let event = clickInsideDialog(); |
| 115 |
assert.strictEqual( event.isDefaultPrevented(), false, 'Dialog does not affect inside click' ); |
| 116 |
assert.strictEqual( event.isPropagationStopped(), false, 'Dialog does not affect inside click propagation' ); |
| 117 |
reuseDialog.closeDialog = () => assert.true( true, 'Dialog is closed by outside click' ); |
| 118 |
event = clickOutsideDialog(); |
| 119 |
assert.strictEqual( event.isDefaultPrevented(), true, 'Dialog catches outside click' ); |
| 120 |
assert.strictEqual( event.isPropagationStopped(), true, 'Dialog stops outside click propagation' ); |
| 121 |
} |
| 122 |
|
| 123 |
assertDialogDoesNotCatchClicks(); |
| 124 |
reuseDialog.openDialog(); |
| 125 |
assertDialogCatchesOutsideClicksOnly(); |
| 126 |
realCloseDialog.call( reuseDialog ); |
| 127 |
assertDialogDoesNotCatchClicks(); |
| 128 |
reuseDialog.openDialog(); |
| 129 |
reuseDialog.unattach(); |
| 130 |
assertDialogDoesNotCatchClicks(); |
| 131 |
} ); |
| 132 |
|
| 133 |
QUnit.test( 'set()/empty() sense check:', function ( assert ) { |
| 134 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 135 |
reuseDialog.embed.resetCurrentSizeMenuToDefault = () => {}; |
| 136 |
const title = mw.Title.newFromText( 'File:Foobar.jpg' ); |
| 137 |
const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg'; |
| 138 |
const url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg'; |
| 139 |
const image = { |
| 140 |
title: title, |
| 141 |
url: src, |
| 142 |
descriptionUrl: url, |
| 143 |
width: 100, |
| 144 |
height: 80 |
| 145 |
}; |
| 146 |
reuseDialog.set( image, 'caption' ); |
| 147 |
reuseDialog.empty(); |
| 148 |
|
| 149 |
assert.true( true, 'Set/empty did not cause an error.' ); |
| 150 |
} ); |
| 151 |
|
| 152 |
QUnit.test( 'openDialog()/closeDialog():', function ( assert ) { |
| 153 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 154 |
reuseDialog.embed.resetCurrentSizeMenuToDefault = () => {}; |
| 155 |
const title = mw.Title.newFromText( 'File:Foobar.jpg' ); |
| 156 |
const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg'; |
| 157 |
const url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg'; |
| 158 |
const image = { |
| 159 |
title: title, |
| 160 |
url: src, |
| 161 |
descriptionUrl: url, |
| 162 |
width: 100, |
| 163 |
height: 80 |
| 164 |
}; |
| 165 |
|
| 166 |
reuseDialog.set( image, 'caption' ); |
| 167 |
reuseDialog.setValues = undefined; |
| 168 |
|
| 169 |
assert.strictEqual( reuseDialog.isOpen, false, 'Dialog closed by default.' ); |
| 170 |
|
| 171 |
reuseDialog.openDialog(); |
| 172 |
|
| 173 |
assert.strictEqual( reuseDialog.isOpen, true, 'Dialog open now.' ); |
| 174 |
|
| 175 |
reuseDialog.closeDialog(); |
| 176 |
|
| 177 |
assert.strictEqual( reuseDialog.isOpen, false, 'Dialog closed now.' ); |
| 178 |
} ); |
| 179 |
|
| 180 |
QUnit.test( 'getImageWarnings():', function ( assert ) { |
| 181 |
const reuseDialog = makeReuseDialog( this.sandbox ); |
| 182 |
const title = mw.Title.newFromText( 'File:Foobar.jpg' ); |
| 183 |
const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg'; |
| 184 |
const url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg'; |
| 185 |
const image = { |
| 186 |
title: title, |
| 187 |
url: src, |
| 188 |
descriptionUrl: url, |
| 189 |
width: 100, |
| 190 |
height: 80 |
| 191 |
}; |
| 192 |
const imageDeleted = Object.assign( { deletionReason: 'deleted file test' }, image ); |
| 193 |
|
| 194 |
|
| 195 |
assert.strictEqual( reuseDialog.getImageWarnings( image ).length, 1, 'Lack of license detected' ); |
| 196 |
|
| 197 |
|
| 198 |
assert.strictEqual( reuseDialog.getImageWarnings( imageDeleted ).length, 1, 'Deletion detected' ); |
| 199 |
} ); |
| 200 |
|
| 201 |
}() ); |