Jdlrobson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/352614 )

Change subject: False references should fall back to hash change
......................................................................

False references should fall back to hash change

If a reference doesn't exist e.g. ReferenceGateway::getReference
returns false then hide the drawer and update the hash fragment

Additional changes:
* showReference now returns a deferred object

Bug: T154861
Change-Id: I7434d9b0bcdbdc7a076db5127ed62866019ce99b
---
M resources/mobile.references/ReferencesDrawer.js
M resources/skins.minerva.scripts/references.js
A tests/qunit/mobile.references/test_ReferencesDrawer.js
3 files changed, 61 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/14/352614/1

diff --git a/resources/mobile.references/ReferencesDrawer.js 
b/resources/mobile.references/ReferencesDrawer.js
index 243c3d0..0a2922f 100644
--- a/resources/mobile.references/ReferencesDrawer.js
+++ b/resources/mobile.references/ReferencesDrawer.js
@@ -79,17 +79,25 @@
                 * @param {string} id of the reference to be retrieved
                 * @param {Page} page to locate reference for
                 * @param {string} refNumber the number it identifies as in the 
page
+                * @return {jQuery.Deferred}
                 */
                showReference: function ( id, page, refNumber ) {
                        var drawer = this;
 
                        // Save the page in case we have to show a nested 
reference.
                        this.options.page = page;
-                       this.options.gateway.getReference( id, page ).done( 
function ( reference ) {
-                               drawer.render( {
-                                       title: refNumber,
-                                       text: reference.text
-                               } );
+                       return this.options.gateway.getReference( id, page 
).done( function ( reference ) {
+                               if ( reference ) {
+                                       drawer.render( {
+                                               title: refNumber,
+                                               text: reference.text
+                                       } );
+                                       // use setTimeout so that browser 
calculates dimensions before show()
+                                       setTimeout( $.proxy( drawer, 'show' ), 
0 );
+                               } else {
+                                       window.location.hash = id;
+                                       drawer.hide();
+                               }
                        } ).fail( function () {
                                drawer.render( {
                                        error: true,
diff --git a/resources/skins.minerva.scripts/references.js 
b/resources/skins.minerva.scripts/references.js
index 4fd0e79..91d8233 100644
--- a/resources/skins.minerva.scripts/references.js
+++ b/resources/skins.minerva.scripts/references.js
@@ -63,8 +63,6 @@
                        drawer.render( {
                                text: undefined
                        } );
-                       // use setTimeout so that browser calculates dimensions 
before show()
-                       setTimeout( $.proxy( drawer, 'show' ), 0 );
                }
        }
 
diff --git a/tests/qunit/mobile.references/test_ReferencesDrawer.js 
b/tests/qunit/mobile.references/test_ReferencesDrawer.js
new file mode 100644
index 0000000..d474893
--- /dev/null
+++ b/tests/qunit/mobile.references/test_ReferencesDrawer.js
@@ -0,0 +1,48 @@
+( function ( $, M ) {
+
+       var ReferenceDrawer = M.require( 'mobile.references/ReferencesDrawer' ),
+               Page = M.require( 'mobile.startup/Page' );
+
+       QUnit.module( 'MobileFrontend: ReferencesDrawer', {
+               setup: function () {
+                       this.gateway = {
+                               getReference: $.noop
+                       };
+                       this.page = new Page( { title: 'reference test' } );
+                       this.drawer = new ReferenceDrawer( {
+                               page: this.page,
+                               gateway: this.gateway
+                       } );
+               }
+       } );
+
+
+       QUnit.test( 'Bad reference not shown', 1, function ( assert ) {
+               var done = assert.async( 1 ),
+                       hideSpy = this.sandbox.spy( this.drawer, 'hide' );
+
+               this.sandbox.stub( this.gateway, 'getReference' ).returns(
+                       $.Deferred().resolve( false )
+               );
+               this.drawer.showReference( '#cite_note-bad', this.page, '1' 
).done( function ( ref ) {
+                       assert.ok( hideSpy.calledOnce, 'Hide is called.' );
+                       done();
+               } );
+       } );
+
+       QUnit.test( 'Good reference causes render', 1, function ( assert ) {
+               var done = assert.async( 1 ),
+                       renderSpy = this.sandbox.spy( this.drawer, 'render' );
+
+               this.sandbox.stub( this.gateway, 'getReference' ).returns(
+                       $.Deferred().resolve( {
+                               text: 'I am a reference'
+                       } )
+               );
+               this.drawer.showReference( '#cite_note-good', this.page, '1' 
).done( function ( ref ) {
+                       assert.ok( renderSpy.calledOnce, 'Hide is called.' );
+                       done();
+               } );
+       } );
+
+}( jQuery, mw.mobileFrontend ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/352614
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7434d9b0bcdbdc7a076db5127ed62866019ce99b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to