Sumit has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/260330

Change subject: Enable skipped router tests
......................................................................

Enable skipped router tests

Refactor Router.js to include a 'testHash' for testing and decouple
test_Router.js from window, and instead use this 'testHash'.

Bug: T98731
Change-Id: I8c089700b1280b64d180de0f78630654a9dbdccf
---
M resources/mobile.startup/Router.js
M tests/qunit/mobile.startup/test_Router.js
2 files changed, 58 insertions(+), 42 deletions(-)


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

diff --git a/resources/mobile.startup/Router.js 
b/resources/mobile.startup/Router.js
index 865fda7..5e9d7d8 100644
--- a/resources/mobile.startup/Router.js
+++ b/resources/mobile.startup/Router.js
@@ -35,12 +35,18 @@
                this.routes = {};
                this._enabled = true;
                this._oldHash = this.getPath();
+               // custom hash, only use with tests
+               this.testHash = '';
 
                $( window ).on( 'popstate', function () {
                        self.emit( 'popstate' );
                } );
 
                $( window ).on( 'hashchange', function () {
+                       self.emit( 'hashchange' );
+               } );
+
+               this.on( 'hashchange', function(){
                        // ev.originalEvent.newURL is undefined on Android 2.x
                        var routeEv;
 
@@ -56,7 +62,7 @@
                                        // if route was prevented, ignore the 
next hash change and revert the
                                        // hash to its old value
                                        self._enabled = false;
-                                       window.location.hash = self._oldHash;
+                                       self.navigate( self._oldHash );
                                }
                        } else {
                                self._enabled = true;
@@ -117,6 +123,22 @@
        };
 
        /**
+        * Sets the current hash of the router.
+        * Note that this method is only for setting hash during tests
+        * @param {String} hash Current hash in the test
+        */
+       Router.prototype.setHash = function( hash ) {
+               this.testHash = hash;
+       }
+
+       /**
+        * Triggers back on the window
+        */
+       Router.prototype.goBack = function() {
+               window.history.back();
+       }
+
+       /**
         * Navigate to the previous route. This is a wrapper for 
window.history.back
         * @method
         * @return {jQuery.Deferred}
@@ -131,7 +153,7 @@
                        deferredRequest.resolve();
                } );
 
-               window.history.back();
+               this.goBack();
 
                // If for some reason (old browser, bug in IE/windows 8.1, etc) 
popstate doesn't fire,
                // resolve manually. Since we don't know for sure which 
browsers besides IE10/11 have
diff --git a/tests/qunit/mobile.startup/test_Router.js 
b/tests/qunit/mobile.startup/test_Router.js
index 89b06c8..941e959 100644
--- a/tests/qunit/mobile.startup/test_Router.js
+++ b/tests/qunit/mobile.startup/test_Router.js
@@ -3,54 +3,44 @@
                hashQueue = [],
                interval, router;
 
-       // we can't change hash too quickly because hashchange callbacks are 
async
-       // (don't fire immediately after the hash is changed) and all the 
callbacks
-       // would get the same (latest) hash; see setup and teardown too
-       function setHash( hash ) {
-               hashQueue.push( hash );
-       }
-
        QUnit.module( 'MobileFrontend Router', {
                setup: function () {
                        router = new Router();
-                       interval = setInterval( function () {
-                               var hash = hashQueue.pop();
-                               if ( hash !== undefined ) {
-                                       window.location.hash = hash;
-                               }
-                       }, 10 );
+                       this.stub( router, 'getPath', function(){
+                               return router.testHash.slice(1);
+                       } );
+                       this.stub( router, 'navigate', function( path ){
+                               router.setHash( path )
+                       } );
                },
 
                teardown: function () {
-                       // hashchange is async, we need to wait
-                       $( window ).one( 'hashchange.test', function () {
-                               $( window ).off( 'hashchange.test' );
-                               clearInterval( interval );
-                               QUnit.start();
-                       } );
-                       setHash( '' );
-                       QUnit.stop();
+                       router.setHash( '' );
                }
        } );
 
-       QUnit.skip( '#route, string', 1, function ( assert ) {
+       QUnit.test( '#route, string', 1, function ( assert ) {
+               router.setHash( '' );
                router.route( 'teststring', function () {
                        assert.ok( true, 'run callback for route' );
-                       QUnit.start();
                } );
-               setHash( '#teststring' );
+               router.setHash( '#teststring' );
+               router.emit( 'hashchange' );
        } );
 
-       QUnit.skip( '#route, RegExp', 1, function ( assert ) {
+       QUnit.test( '#route, RegExp', 1, function ( assert ) {
+               router.setHash( '' );
                router.route( /^testre-(\d+)$/, function ( param ) {
                        assert.strictEqual( param, '123', 'run callback for 
route with correct params' );
-                       QUnit.start();
                } );
-               setHash( '#testre-abc' );
-               setHash( '#testre-123' );
+               router.setHash( '#testre-abc' );
+               router.emit( 'hashchange' );
+               router.setHash( '#testre-123' );
+               router.emit( 'hashchange' );
        } );
 
-       QUnit.skip( 'on route', 2, function ( assert ) {
+       QUnit.test( 'on route', 2, function ( assert ) {
+               router.setHash( '' );
                var count = 0,
                        spy = this.sandbox.spy();
 
@@ -58,36 +48,40 @@
 
                // try preventing second route (#testprevent)
                router.once( 'route', function () {
-                       setHash( '#testprevent' );
+                       router.setHash( '#testprevent' );
                        router.once( 'route', function ( ev ) {
                                ev.preventDefault();
                        } );
                } );
-               setHash( '#initial' );
+               router.setHash( '#initial' );
 
-               $( window ).on( 'hashchange.test', function () {
+               router.on( 'hashchange.test', function () {
                        ++count;
                        if ( count === 3 ) {
-                               assert.strictEqual( window.location.hash, 
'#initial', 'reset hash' );
+                               assert.strictEqual( router.testHash, 
'#initial', 'reset hash' );
                                assert.ok( !spy.called, 'don\'t run callback 
for prevented route' );
-                               QUnit.start();
                        }
                } );
+               // emit a hashchange thrice to check if the hash has changed or 
not
+               router.emit( 'hashchange.test' );
+               router.emit( 'hashchange.test' );
+               router.emit( 'hashchange.test' );
        } );
 
-       QUnit.skip( 'on back', 2, function ( assert ) {
+       QUnit.test( 'on back', 2, function ( assert ) {
+               this.sandbox.stub( router, 'goBack' );
                router.back().done( function () {
                        assert.ok( true, 'back 1 complete' );
                } );
                router.back().done( function () {
                        assert.ok( true, 'back 2 complete' );
                } );
-               QUnit.start();
+               router.emit( 'popstate' );
        } );
 
-       QUnit.skip( 'on back without popstate', 2, function ( assert ) {
-               var historyStub = this.sandbox.stub( window.history, 'back' );  
// do not emit popstate
-
+       QUnit.test( 'on back without popstate', 2, function ( assert ) {
+               var historyStub = this.sandbox.stub( router, 'goBack' );  // do 
not emit popstate
+               var done = assert.async();
                router.on( 'popstate', function () {
                        assert.ok( false, 'this assertion is not supposed to 
get called' );
                } );
@@ -95,7 +89,7 @@
                router.back().done( function () {
                        assert.ok( historyStub.called, 'history back has been 
called' );
                        assert.ok( true, 'back without popstate complete' );
-                       QUnit.start();
+                       done();
                } );
        } );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8c089700b1280b64d180de0f78630654a9dbdccf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Sumit <asthana.sumi...@gmail.com>

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

Reply via email to