Phuedx has uploaded a new change for review.

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

Change subject: Hygiene: Extract shared stub user object
......................................................................

Hygiene: Extract shared stub user object

Changes:
* Extract the various shapes of stub user object into the shared
  mw.popups.tests.stubs#createStubUser factory method.
* Register the ext.popups.tests.stubs module in
  PopupsHooks#onResourceLoaderTestModules and make the test suite depend
  on it.

Bug: T152223
Change-Id: I893b11461d8484bcaabfd950c2fa0dc672454a9d
---
M Popups.hooks.php
M tests/qunit/ext.popups/actions.test.js
M tests/qunit/ext.popups/experiment.test.js
A tests/qunit/ext.popups/stubs/index.js
A tests/qunit/ext.popups/stubs/user.js
M tests/qunit/ext.popups/userSettings.test.js
6 files changed, 50 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups 
refs/changes/84/325284/1

diff --git a/Popups.hooks.php b/Popups.hooks.php
index c3559d9..ccc5512 100644
--- a/Popups.hooks.php
+++ b/Popups.hooks.php
@@ -102,10 +102,23 @@
                        return substr( $script, $start );
                }, $scripts );
 
+               $testModules['qunit']['ext.popups.tests.stubs'] = [
+                       'scripts' => [
+                               'tests/qunit/ext.popups/stubs/index.js',
+                               'tests/qunit/ext.popups/stubs/user.js',
+                       ],
+                       'dependencies' => [
+                               'ext.popups', // The mw.popups is required.
+                       ],
+                       'localBasePath' => __DIR__,
+                       'remoteExtPath' => 'Popups',
+               ];
+
                $testModules['qunit']['ext.popups.tests'] = [
                        'scripts' => $scripts,
                        'dependencies' => [
                                'ext.popups',
+                               'ext.popups.tests.stubs',
                        ],
                        'localBasePath' => __DIR__,
                        'remoteExtPath' => 'Popups',
diff --git a/tests/qunit/ext.popups/actions.test.js 
b/tests/qunit/ext.popups/actions.test.js
index 0907426..ff49db7 100644
--- a/tests/qunit/ext.popups/actions.test.js
+++ b/tests/qunit/ext.popups/actions.test.js
@@ -10,22 +10,13 @@
                                return '9876543210';
                        },
                        config = new mw.Map(),
-                       stubUser;
+                       stubUser = mw.popups.tests.stubs.createStubUser( /* 
isAnon = */ true );
 
                config.set( {
                        wgTitle: 'Foo',
                        wgNamespaceNumber: 1,
                        wgArticleId: 2
                } );
-
-               stubUser = {
-                       sessionId: function () {
-                               return '0123456789';
-                       },
-                       isAnon: function () {
-                               return true;
-                       }
-               };
 
                assert.expect( 1 );
 
diff --git a/tests/qunit/ext.popups/experiment.test.js 
b/tests/qunit/ext.popups/experiment.test.js
index 7dd8085..793da46 100644
--- a/tests/qunit/ext.popups/experiment.test.js
+++ b/tests/qunit/ext.popups/experiment.test.js
@@ -1,13 +1,5 @@
 ( function ( mw ) {
 
-       function createStubUser( isAnon ) {
-               return {
-                       isAnon: function () {
-                               return isAnon;
-                       }
-               };
-       }
-
        function createStubUserSettings( hasIsEnabled ) {
                return {
                        getToken: function () {
@@ -25,7 +17,7 @@
        QUnit.module( 'ext.popups/experiment', {
                setup: function () {
                        this.config = new mw.Map();
-                       this.user = createStubUser( /* isAnon = */ true );
+                       this.user = mw.popups.tests.stubs.createStubUser( /* 
isAnon = */ true );
                        this.userSettings = createStubUserSettings( /* 
hasIsEnabled = */ false );
                        this.isUserInCondition = mw.popups.createExperiment( 
this.config, this.user, this.userSettings );
                }
@@ -38,7 +30,7 @@
        } );
 
        QUnit.test( '#isUserInCondition', 2, function ( assert ) {
-               var user = createStubUser( /* isAnon = */ false ),
+               var user = mw.popups.tests.stubs.createStubUser( /* isAnon = */ 
false ),
                        isUserInCondition = mw.popups.createExperiment( 
this.config, user, this.userSettings );
 
                this.config.set( {
diff --git a/tests/qunit/ext.popups/stubs/index.js 
b/tests/qunit/ext.popups/stubs/index.js
new file mode 100644
index 0000000..6c415fb
--- /dev/null
+++ b/tests/qunit/ext.popups/stubs/index.js
@@ -0,0 +1,9 @@
+( function ( mw ) {
+
+       // TODO: This namespace may have to be initialized elsewhere if, say, 
shared
+       // fixtures are extracted.
+       mw.popups.tests = {};
+
+       mw.popups.tests.stubs = {};
+
+}( mediaWiki ) );
diff --git a/tests/qunit/ext.popups/stubs/user.js 
b/tests/qunit/ext.popups/stubs/user.js
new file mode 100644
index 0000000..a7da294
--- /dev/null
+++ b/tests/qunit/ext.popups/stubs/user.js
@@ -0,0 +1,24 @@
+( function ( mw ) {
+
+       /**
+        * Creates a **minimal** stub that can be used in place of an `mw.User`
+        * instance.
+        *
+        * @param {boolean} isAnon The return value of the `#isAnon`.
+        * @return {Object}
+        */
+       mw.popups.tests.stubs.createStubUser = function createStubUser( isAnon 
) {
+               return {
+                       isAnon: function () {
+                               return isAnon;
+                       },
+                       sessionId: function () {
+                               return '0123456789';
+                       },
+                       generateRandomSessionId: function () {
+                               return '9876543210';
+                       }
+               };
+       };
+
+}( mediaWiki ) );
diff --git a/tests/qunit/ext.popups/userSettings.test.js 
b/tests/qunit/ext.popups/userSettings.test.js
index 8790192..7ef0de8 100644
--- a/tests/qunit/ext.popups/userSettings.test.js
+++ b/tests/qunit/ext.popups/userSettings.test.js
@@ -2,11 +2,7 @@
 
        QUnit.module( 'ext.popups/userSettings', {
                setup: function () {
-                       var stubUser = {
-                               generateRandomSessionId: function () {
-                                       return '1234567890';
-                               }
-                       };
+                       var stubUser = mw.popups.tests.stubs.createStubUser( /* 
isAnon = */ true );
 
                        this.storage = new mw.Map();
                        this.userSettings = mw.popups.createUserSettings( 
this.storage, stubUser );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I893b11461d8484bcaabfd950c2fa0dc672454a9d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: mpga
Gerrit-Owner: Phuedx <samsm...@wikimedia.org>

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

Reply via email to