Ejegg has submitted this change and it was merged.

Change subject: Basic QUnit tests
......................................................................


Basic QUnit tests

Covers a tiny bit of bannerController.

Change-Id: I281f576b4a75458b15071167220c1c404e7bec6d
---
M CentralNotice.hooks.php
A tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
2 files changed, 118 insertions(+), 1 deletion(-)

Approvals:
  Ejegg: Looks good to me, approved



diff --git a/CentralNotice.hooks.php b/CentralNotice.hooks.php
index f4c3dd3..f7f8898 100644
--- a/CentralNotice.hooks.php
+++ b/CentralNotice.hooks.php
@@ -111,7 +111,9 @@
        $wgAPIListModules[ 'centralnoticelogs' ] = 'ApiCentralNoticeLogs';
 
        // Register hooks
-       $wgHooks[ 'UnitTestsList' ][ ] = 'efCentralNoticeUnitTests';
+       // TODO: replace ef- global functions with static methods in 
CentralNoticeHooks
+       $wgHooks['ResourceLoaderTestModules'][] = 
'efCentralNoticeResourceLoaderTestModules';
+       $wgHooks['UnitTestsList'][] = 'efCentralNoticeUnitTests';
 
        // If CentralNotice banners should be shown on this wiki, load the 
components we need for
        // showing banners. For discussion of banner loading strategies, see
@@ -374,6 +376,53 @@
 
 /**
  * Place CentralNotice ResourceLoader modules onto mobile pages.
+ * ResourceLoaderTestModules hook handler
+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules
+ *
+ * @param array $testModules
+ * @param ResourceLoader $resourceLoader
+ * @return bool
+ */
+function efCentralNoticeResourceLoaderTestModules( array &$testModules,
+       ResourceLoader $resourceLoader
+) {
+       global $wgResourceModules;
+
+       $testModuleBoilerplate = array(
+               'localBasePath' => __DIR__,
+               'remoteExtPath' => 'CentralNotice',
+       );
+
+       // TODO: Something similar should be provided by core.
+       // find test files for every RL module
+       $prefix = 'ext.centralNotice';
+       foreach ( $wgResourceModules as $key => $module ) {
+               if ( substr( $key, 0, strlen( $prefix ) ) === $prefix && isset( 
$module['scripts'] ) ) {
+                       $testFiles = array();
+                       foreach ( ((array) $module['scripts'] ) as $script ) {
+                               $testFile = 'tests/qunit/' . $key . '/' . 
basename( $script );
+                               $testFile = preg_replace( '/.js$/', 
'.tests.js', $testFile );
+                               // if a test file exists for a given JS file, 
add it
+                               if ( file_exists( __DIR__ . '/' . $testFile ) ) 
{
+                                       $testFiles[] = $testFile;
+                               }
+                       }
+                       // if test files exist for given module, create a 
corresponding test module
+                       if ( count( $testFiles ) > 0 ) {
+                               $testModules['qunit']["$key.tests"] = 
$testModuleBoilerplate + array(
+                                       'dependencies' => array( $key ),
+                                       'scripts' => $testFiles,
+                               );
+                       }
+               }
+       }
+
+       return true;
+}
+
+/**
+ * EnableMobileModules callback for placing the CN resourceloader
+ * modules onto mobile pages.
  *
  * @param Skin $skin
  * @param array $modules
diff --git 
a/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js 
b/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
new file mode 100644
index 0000000..4f619e4
--- /dev/null
+++ b/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
@@ -0,0 +1,68 @@
+( function ( mw, $ ) {
+       'use strict';
+
+       var bannerJson = {
+                       bannerName: 'test_banner',
+                       campaign: 'test_campaign',
+                       category: 'test',
+                       bannerHtml: '<div id="test_banner"></div>'
+               };
+
+       QUnit.module( 'ext.centralNotice.bannerController', 
QUnit.newMwEnvironment( {
+               setup: function () {
+                       var realLoadBanner = mw.centralNotice.loadBanner;
+
+                       // Remove any existing div#siteNotice, so we are not 
testing the skin.
+                       $( '#siteNotice' ).remove();
+
+                       // Reset in case the testing page itself ran 
CentralNotice.
+                       mw.centralNotice.alreadyRan = false;
+
+                       // Fool code that prevents CentralNotice from running 
on Special pages.
+                       mw.config.set( 'wgNamespaceNumber', 0 );
+
+                       // Prevent banner load during initialize().
+                       mw.centralNotice.loadBanner = function () {};
+
+                       // Suppress GeoIP call
+                       mw.centralNotice.data.getVars.country = 'US';
+
+                       mw.centralNotice.initialize();
+
+                       mw.centralNotice.loadBanner = realLoadBanner;
+
+                       // Create normalized siteNotice.
+                       $( "#qunit-fixture" ).append(
+                               '<div id=siteNotice><div 
id=centralNotice></div></div>'
+                       );
+               }
+       } ) );
+
+       QUnit.test( 'hasAlreadyRan', 1, function( assert ) {
+               assert.ok( mw.centralNotice.alreadyRan );
+       } );
+
+       QUnit.test( 'canInsertBanner', 1, function( assert ) {
+               mw.centralNotice.insertBanner( bannerJson );
+               assert.equal( $( 'div#test_banner' ).length, 1 );
+       } );
+
+       QUnit.test( 'canPreloadHide', 1, function( assert ) {
+               mw.centralNotice.bannerData.preload = function () {
+                       return false;
+               };
+
+               mw.centralNotice.insertBanner( bannerJson );
+               assert.equal( $( 'div#test_banner' ).length, 0 );
+       } );
+
+       QUnit.test( 'canPreloadShow', 1, function( assert ) {
+               mw.centralNotice.bannerData.preload = function () {
+                       return true;
+               };
+
+               mw.centralNotice.insertBanner( bannerJson );
+               assert.equal( $( 'div#test_banner' ).length, 1 );
+       } );
+
+}( mediaWiki, jQuery ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I281f576b4a75458b15071167220c1c404e7bec6d
Gerrit-PatchSet: 18
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Cmcmahon <[email protected]>
Gerrit-Reviewer: Dduvall <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Mwalker <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to