| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { createLocalStorage, getFakeLocalStorage, getUnsupportedLocalStorage } = require( '../mmv.testhelpers.js' ); |
| 19 |
const { MetadataPanelScroller } = require( 'mmv' ); |
| 20 |
const storage = mw.storage; |
| 21 |
|
| 22 |
( function () { |
| 23 |
QUnit.module( 'mmv.ui.metadataPanelScroller', QUnit.newMwEnvironment( { |
| 24 |
beforeEach: function () { |
| 25 |
this.clock = this.sandbox.useFakeTimers(); |
| 26 |
}, |
| 27 |
afterEach: function () { |
| 28 |
mw.storage = storage; |
| 29 |
} |
| 30 |
} ) ); |
| 31 |
|
| 32 |
QUnit.test( 'empty()', ( assert ) => { |
| 33 |
const $qf = $( '#qunit-fixture' ); |
| 34 |
mw.storage = getFakeLocalStorage(); |
| 35 |
const scroller = new MetadataPanelScroller( $qf, $( '<div>' ).appendTo( $qf ) ); |
| 36 |
|
| 37 |
scroller.empty(); |
| 38 |
assert.strictEqual( scroller.$container.hasClass( 'invite' ), false, 'We successfully reset the invite' ); |
| 39 |
} ); |
| 40 |
|
| 41 |
QUnit.test( 'Metadata div is only animated once', ( assert ) => { |
| 42 |
const $qf = $( '#qunit-fixture' ); |
| 43 |
let displayCount = null; |
| 44 |
mw.storage = createLocalStorage( { |
| 45 |
|
| 46 |
getItem: function () { |
| 47 |
return displayCount; |
| 48 |
}, |
| 49 |
setItem: function ( _, val ) { |
| 50 |
displayCount = val; |
| 51 |
}, |
| 52 |
removeItem: function () { |
| 53 |
displayCount = null; |
| 54 |
} |
| 55 |
} ); |
| 56 |
const scroller = new MetadataPanelScroller( $qf, $( '<div>' ).appendTo( $qf ) ); |
| 57 |
|
| 58 |
scroller.attach(); |
| 59 |
|
| 60 |
scroller.animateMetadataOnce(); |
| 61 |
|
| 62 |
assert.true( scroller.hasAnimatedMetadata, |
| 63 |
'The first call to animateMetadataOnce set hasAnimatedMetadata to true' ); |
| 64 |
assert.true( $qf.hasClass( 'invite' ), |
| 65 |
'The first call to animateMetadataOnce led to an animation' ); |
| 66 |
|
| 67 |
$qf.removeClass( 'invite' ); |
| 68 |
|
| 69 |
scroller.animateMetadataOnce(); |
| 70 |
|
| 71 |
assert.strictEqual( scroller.hasAnimatedMetadata, true, 'The second call to animateMetadataOnce did not change the value of hasAnimatedMetadata' ); |
| 72 |
assert.strictEqual( $qf.hasClass( 'invite' ), false, |
| 73 |
'The second call to animateMetadataOnce did not lead to an animation' ); |
| 74 |
|
| 75 |
scroller.unattach(); |
| 76 |
|
| 77 |
scroller.attach(); |
| 78 |
|
| 79 |
scroller.animateMetadataOnce(); |
| 80 |
assert.true( $qf.hasClass( 'invite' ), |
| 81 |
'After closing and opening the viewer, the panel is animated again' ); |
| 82 |
|
| 83 |
scroller.unattach(); |
| 84 |
} ); |
| 85 |
|
| 86 |
QUnit.test( 'No localStorage', function ( assert ) { |
| 87 |
const $qf = $( '#qunit-fixture' ); |
| 88 |
mw.storage = getUnsupportedLocalStorage(); |
| 89 |
const scroller = new MetadataPanelScroller( $qf, $( '<div>' ).appendTo( $qf ) ); |
| 90 |
|
| 91 |
this.sandbox.stub( $.fn, 'scrollTop', () => 10 ); |
| 92 |
|
| 93 |
scroller.scroll(); |
| 94 |
|
| 95 |
assert.strictEqual( scroller.hasOpenedMetadata, true, 'We store hasOpenedMetadata flag for the session' ); |
| 96 |
} ); |
| 97 |
|
| 98 |
QUnit.test( 'localStorage is full', function ( assert ) { |
| 99 |
const $qf = $( '#qunit-fixture' ); |
| 100 |
mw.storage = createLocalStorage( { |
| 101 |
getItem: this.sandbox.stub().returns( null ), |
| 102 |
setItem: this.sandbox.stub().throwsException( 'I am full' ), |
| 103 |
removeItem: this.sandbox.stub() |
| 104 |
} ); |
| 105 |
const scroller = new MetadataPanelScroller( $qf, $( '<div>' ).appendTo( $qf ) ); |
| 106 |
|
| 107 |
this.sandbox.stub( $.fn, 'scrollTop', () => 10 ); |
| 108 |
|
| 109 |
scroller.attach(); |
| 110 |
|
| 111 |
scroller.scroll(); |
| 112 |
|
| 113 |
assert.strictEqual( scroller.hasOpenedMetadata, true, 'We store hasOpenedMetadata flag for the session' ); |
| 114 |
|
| 115 |
scroller.scroll(); |
| 116 |
|
| 117 |
assert.true( mw.storage.store.setItem.calledOnce, 'localStorage only written once' ); |
| 118 |
|
| 119 |
scroller.unattach(); |
| 120 |
} ); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
function stubScrollFunctions( sandbox, scroller ) { |
| 131 |
let memorizedScrollTop = 0; |
| 132 |
|
| 133 |
sandbox.stub( $.fn, 'scrollTop', function ( scrollTop ) { |
| 134 |
if ( scrollTop !== undefined ) { |
| 135 |
memorizedScrollTop = scrollTop; |
| 136 |
scroller.scroll(); |
| 137 |
return this; |
| 138 |
} else { |
| 139 |
return memorizedScrollTop; |
| 140 |
} |
| 141 |
} ); |
| 142 |
sandbox.stub( $.fn, 'animate', function ( props ) { |
| 143 |
if ( 'scrollTop' in props ) { |
| 144 |
memorizedScrollTop = props.scrollTop; |
| 145 |
scroller.scroll(); |
| 146 |
} |
| 147 |
return this; |
| 148 |
} ); |
| 149 |
} |
| 150 |
|
| 151 |
QUnit.test( 'Metadata scrolling', function ( assert ) { |
| 152 |
const $window = $( window ); |
| 153 |
const $qf = $( '#qunit-fixture' ); |
| 154 |
const $container = $( '<div>' ).css( 'height', 100 ).appendTo( $qf ); |
| 155 |
const $aboveFold = $( '<div>' ).css( 'height', 50 ).appendTo( $container ); |
| 156 |
mw.storage = createLocalStorage( { |
| 157 |
getItem: this.sandbox.stub().returns( null ), |
| 158 |
setItem: function () {}, |
| 159 |
removeItem: function () {} |
| 160 |
} ); |
| 161 |
const scroller = new MetadataPanelScroller( $container, $aboveFold ); |
| 162 |
const keydown = $.Event( 'keydown' ); |
| 163 |
|
| 164 |
stubScrollFunctions( this.sandbox, scroller ); |
| 165 |
|
| 166 |
this.sandbox.stub( mw.storage.store, 'setItem' ); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
scroller.hasAnimatedMetadata = false; |
| 171 |
|
| 172 |
scroller.attach(); |
| 173 |
|
| 174 |
assert.strictEqual( $window.scrollTop(), 0, 'scrollTop should be set to 0' ); |
| 175 |
|
| 176 |
assert.strictEqual( mw.storage.store.setItem.called, false, 'The metadata hasn\'t been open yet, no entry in localStorage' ); |
| 177 |
|
| 178 |
keydown.which = 38; |
| 179 |
scroller.keydown( keydown ); |
| 180 |
|
| 181 |
assert.strictEqual( mw.storage.store.setItem.calledWithExactly( 'mmv.hasOpenedMetadata', '1' ), true, 'localStorage knows that the metadata has been open' ); |
| 182 |
|
| 183 |
keydown.which = 40; |
| 184 |
scroller.keydown( keydown ); |
| 185 |
|
| 186 |
assert.strictEqual( $window.scrollTop(), 0, |
| 187 |
'scrollTop should be set to 0 after pressing down arrow' ); |
| 188 |
|
| 189 |
|
| 190 |
scroller.unattach(); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
scroller.attach(); |
| 195 |
|
| 196 |
|
| 197 |
assert.strictEqual( $window.scrollTop(), 0, 'Page scrollTop should be set to 0' ); |
| 198 |
|
| 199 |
|
| 200 |
$window.scrollTop( 20 ); |
| 201 |
this.clock.tick( 100 ); |
| 202 |
|
| 203 |
|
| 204 |
scroller.attach(); |
| 205 |
|
| 206 |
|
| 207 |
assert.strictEqual( $window.scrollTop(), 20, 'Page scrollTop should be set to 20' ); |
| 208 |
|
| 209 |
scroller.unattach(); |
| 210 |
} ); |
| 211 |
}() ); |