Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.metadataPanelScroller.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer 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 * MediaViewer 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 MediaViewer. If not, see <http://www.gnu.org/licenses/>.
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; // pretend it doesn't exist at first
44 mw.storage = createLocalStorage( {
45 // We simulate localStorage to avoid test side-effects
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 * We need to set up a proxy on the jQuery scrollTop function and the jQuery.scrollTo plugin,
124 * that will let us pretend that the document really scrolled and that will return values
125 * as if the scroll happened.
126 *
127 * @param {sinon.sandbox} sandbox
128 * @param {MetadataPanelScroller} scroller
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 // First phase of the test: up and down arrows
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; // Up arrow
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; // Down arrow
184 scroller.keydown( keydown );
185
186 assert.strictEqual( $window.scrollTop(), 0,
187 'scrollTop should be set to 0 after pressing down arrow' );
188
189 // Unattach lightbox from document
190 scroller.unattach();
191
192 // Second phase of the test: scroll memory
193
194 scroller.attach();
195
196 // To make sure that the details are out of view, the lightbox is supposed to scroll to the top when open
197 assert.strictEqual( $window.scrollTop(), 0, 'Page scrollTop should be set to 0' );
198
199 // Scroll down to check that the scrollTop memory doesn't affect prev/next (bug 59861)
200 $window.scrollTop( 20 );
201 this.clock.tick( 100 );
202
203 // This extra attach() call simulates the effect of prev/next seen in bug 59861
204 scroller.attach();
205
206 // The lightbox was already open at this point, the scrollTop should be left untouched
207 assert.strictEqual( $window.scrollTop(), 20, 'Page scrollTop should be set to 20' );
208
209 scroller.unattach();
210 } );
211 }() );