From 8d12ef9a8032854e5ecbef46cf4e2c0262285266 Mon Sep 17 00:00:00 2001 From: jdlrobson Date: Tue, 5 Aug 2014 15:18:38 -0700 Subject: [PATCH] The first ever Echo QUnit tests Also change coding conventions of Resources to allows use array for scripts key. Change-Id: I0c9f358ce3eb62adb07b68623e97c07993c9c45c --- Echo.php | 1 + Hooks.php | 51 +++++++++++++++ Resources.php | 8 ++- tests/qunit/overlay/test_ext.echo.overlay.js | 67 ++++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 tests/qunit/overlay/test_ext.echo.overlay.js diff --git a/Echo.php b/Echo.php index 02c92af39..6e878efac 100644 --- a/Echo.php +++ b/Echo.php @@ -137,6 +137,7 @@ $wgHooks['BeforePageDisplay'][] = 'EchoHooks::beforePageDisplay'; $wgHooks['MakeGlobalVariablesScript'][] = 'EchoHooks::makeGlobalVariablesScript'; $wgHooks['UnitTestsList'][] = 'EchoHooks::getUnitTests'; $wgHooks['ResourceLoaderRegisterModules'][] = 'EchoHooks::onResourceLoaderRegisterModules'; +$wgHooks['ResourceLoaderTestModules'][] = 'EchoHooks::onResourceLoaderTestModules'; $wgHooks['UserRights'][] = 'EchoHooks::onUserRights'; $wgHooks['UserLoadOptions'][] = 'EchoHooks::onUserLoadOptions'; $wgHooks['UserSaveOptions'][] = 'EchoHooks::onUserSaveOptions'; diff --git a/Hooks.php b/Hooks.php index 8f98c5879..8a4648cea 100644 --- a/Hooks.php +++ b/Hooks.php @@ -34,6 +34,50 @@ class EchoHooks { } + /** + * ResourceLoaderTestModules hook handler + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules + * + * @param array $testModules + * @param ResourceLoader $resourceLoader + * @return bool + */ + public static function onResourceLoaderTestModules( array &$testModules, + ResourceLoader $resourceLoader + ) { + global $wgResourceModules; + + $testModuleBoilerplate = array( + 'localBasePath' => __DIR__, + 'remoteExtPath' => 'Echo', + 'targets' => array( 'desktop', 'mobile' ), + ); + + // find test files for every RL module + $prefix = 'ext.echo'; + foreach ( $wgResourceModules as $key => $module ) { + if ( substr( $key, 0, strlen( $prefix ) ) === $prefix && isset( $module['scripts'] ) ) { + $testFiles = array(); + foreach ( $module['scripts'] as $script ) { + $testFile = 'tests/qunit/' . dirname( $script ) . '/test_' . basename( $script ); + // if a test file exists for a given JS file, add it + if ( file_exists( $testModuleBoilerplate['localBasePath'] . '/' . $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; + } + /** * Handler for ResourceLoaderRegisterModules hook */ @@ -702,6 +746,13 @@ class EchoHooks { ); $vars['wgEchoHelpPage'] = $wgEchoHelpPage; $vars['wgEchoConfig'] = $wgEchoConfig; + } else if ( SpecialPage::getTitleFor( 'JavaScriptTest', 'qunit' )->equals( $outputPage->getTitle() ) ) { + // For testing purposes + $vars['wgEchoConfig'] = array( + 'eventlogging' => array( + 'EchoInteraction' => array(), + ), + ); } return true; diff --git a/Resources.php b/Resources.php index fc814ecb8..1a56c2fa1 100644 --- a/Resources.php +++ b/Resources.php @@ -34,7 +34,9 @@ $wgResourceModules += array( // dependencies that do not target mobile. 'ext.echo.base' => $echoResourceTemplate + array( 'styles' => 'base/ext.echo.base.less', - 'scripts' => 'base/ext.echo.base.js', + 'scripts' => array( + 'base/ext.echo.base.js', + ), 'messages' => array( 'echo-error-preference', 'echo-error-token', @@ -42,7 +44,9 @@ $wgResourceModules += array( 'targets' => array( 'desktop', 'mobile' ), ), 'ext.echo.desktop' => $echoResourceTemplate + array( - 'scripts' => 'desktop/ext.echo.desktop.js', + 'scripts' => array( + 'desktop/ext.echo.desktop.js', + ), 'dependencies' => array( 'ext.echo.base', 'mediawiki.api', diff --git a/tests/qunit/overlay/test_ext.echo.overlay.js b/tests/qunit/overlay/test_ext.echo.overlay.js new file mode 100644 index 000000000..31a39c66a --- /dev/null +++ b/tests/qunit/overlay/test_ext.echo.overlay.js @@ -0,0 +1,67 @@ +( function( $, mw ) { + QUnit.module( 'ext.echo.overlay', { + setup: function() { + var ApiStub = function() {}; + ApiStub.prototype = { + post: function() { + return $.Deferred().resolve( { + query: { + echomarkread: { + count: 0 + } + } + } ); + }, + get: function() { + return new $.Deferred().resolve( { + query: { + notifications: { + index: [ 70, 71 ], + count: '1', + rawcount: 1, + list: { + 70: { + '*': 'Jon mentioned you.', + agent: { id: 212, name: 'Jon' }, + category: 'mention', + id: 70, + read: '20140805211446', + timestamp: { + unix: '1407273276' + }, + title: { + full: 'Spiders' + }, + type: 'mention' + }, + 71: { + '*': 'X talked to you.', + category: 'edit-user-talk', + id: 71, + type: 'edit-user-talk' + } + } + } + } + } ); + } + }; + this.sandbox.stub( mw, 'Api', ApiStub ); + } + } ); + + QUnit.test( 'mw.echo.overlay.buildOverlay', 5, function( assert ) { + var $overlay; + mw.echo.overlay.buildOverlay( function( $o ) { + $overlay = $o; + } ); + assert.strictEqual( $overlay.find( 'ul' ).length, 1, 'Overlay contains a list of notifications.' ); + assert.strictEqual( $overlay.find( 'li' ).length, 2, 'There are two notifications.' ); + assert.strictEqual( $overlay.find( '.mw-echo-unread' ).length, 1, 'There is one unread notification.' ); + assert.strictEqual( $overlay.find( '#mw-echo-overlay-footer a' ).length, 2, + 'There is a footer with 2 links to preferences and all notifications.' ); + assert.strictEqual( $overlay.find( 'a#mw-echo-overlay-moreinfo-link' ).length, 1, + 'There is a help link.' ); + } ); + +}( jQuery, mediaWiki ) );