Jdlrobson has uploaded a new change for review.

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

Change subject: Add tests for define and require methods
......................................................................

Add tests for define and require methods

Introduce ModuleLoader class
Bug: T46264
Change-Id: I054d0fa089009cb1f8cf5d26cd2edcd5219528de
---
M javascripts/modules.js
A tests/qunit/test_modules.js
2 files changed, 76 insertions(+), 13 deletions(-)


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

diff --git a/javascripts/modules.js b/javascripts/modules.js
index 1ebca73..6da3f82 100644
--- a/javascripts/modules.js
+++ b/javascripts/modules.js
@@ -1,11 +1,19 @@
-/**
- *
- * @class mw.mobileFrontend
- * @singleton
- */
-( function ( $ ) {
-       mw.mobileFrontend = {
-               _modules: {},
+( function () {
+       var loader;
+
+       /**
+        * Class for managing modules
+        * @class ModuleLoader
+        */
+       function ModuleLoader() {
+               /**
+                * @type {Object} register of defined modules
+                * @private
+                */
+               this._register = {};
+       }
+
+       ModuleLoader.prototype = {
                /**
                 * Require (import) a module previously defined using define().
                 *
@@ -13,10 +21,10 @@
                 * @return {Object} Required module, can be any JavaScript 
object.
                 */
                require: function ( id ) {
-                       if ( !this._modules.hasOwnProperty( id ) ) {
+                       if ( !this._register.hasOwnProperty( id ) ) {
                                throw new Error( 'Module not found: ' + id );
                        }
-                       return this._modules[ id ];
+                       return this._register[ id ];
                },
 
                /**
@@ -26,10 +34,37 @@
                 * @param {Object} obj Defined module body, can be any 
JavaScript object.
                 */
                define: function ( id, obj ) {
-                       if ( this._modules.hasOwnProperty( id ) ) {
+                       if ( this._register.hasOwnProperty( id ) ) {
                                throw new Error( 'Module already exists: ' + id 
);
                        }
-                       this._modules[ id ] = obj;
+                       this._register[ id ] = obj;
                }
        };
-}( jQuery ) );
+
+       loader = new ModuleLoader();
+
+       /**
+        *
+        * FIXME: In a wonderful world all this could run in a file called 
init.js,
+        * all the above code would be core and everyone would be happy.
+        * @class mw.mobileFrontend
+        * @singleton
+        */
+       mw.mobileFrontend = {
+               /**
+                * @see ModuleLoader#define
+                */
+               define: function () {
+                       loader.define.apply( loader, arguments );
+               },
+               /**
+                * @see ModuleLoader#require
+                */
+               require: function () {
+                       return loader.require.apply( loader, arguments );
+               }
+       };
+       // inception to support testing (!!)
+       mw.mobileFrontend.define( 'ModuleLoader', ModuleLoader );
+
+}() );
diff --git a/tests/qunit/test_modules.js b/tests/qunit/test_modules.js
new file mode 100644
index 0000000..fd530c9
--- /dev/null
+++ b/tests/qunit/test_modules.js
@@ -0,0 +1,28 @@
+( function( M ) {
+       var ModuleLoader = M.require( 'ModuleLoader' );
+
+       QUnit.module( 'MobileFrontend ModuleLoader', {
+               setup: function () {
+                       this.loader = new ModuleLoader();
+               }
+       } );
+
+       QUnit.test( '#require', 2, function( assert ) {
+               this.loader.define( 'foo', 1 );
+               this.loader.define( 'bar', 5 );
+
+               assert.strictEqual( this.loader.require( 'foo' ), 1, 'Returns 
appropriate module' );
+               assert.strictEqual( this.loader.require( 'bar' ), 5, 'Returns 
appropriate module' );
+       } );
+
+
+       QUnit.test( '#define', 2, function( assert ) {
+               var loader = this.loader;
+               loader.define( 'foo', 1 );
+               loader.define( 'bar', 5 );
+               assert.throws( function() {
+                       loader.define( 'bar', 50 );
+               }, 'Cannot define two modules with the same name' );
+               assert.strictEqual( this.loader.require( 'bar' ), 5, 'Returns 
first definition of module.' );
+       } );
+}( mw.mantle ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I054d0fa089009cb1f8cf5d26cd2edcd5219528de
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