Merge "Add tests for rendering of thanks button on mobile diff page"

This commit is contained in:
jenkins-bot 2015-01-29 07:18:31 +00:00 committed by Gerrit Code Review
commit dc9ce761a7
4 changed files with 76 additions and 7 deletions

View file

@ -7,6 +7,31 @@
*/
class ThanksHooks {
/**
* 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
) {
if ( class_exists( 'SpecialMobileDiff' ) ) {
$testModules['qunit']['tests.ext.thanks.mobilediff'] = array(
'localBasePath' => __DIR__,
'remoteExtPath' => 'Thanks',
'dependencies' => array( 'ext.thanks.mobilediff' ),
'scripts' => array(
'tests/qunit/test_ext.thanks.mobilediff.js',
),
'targets' => array( 'desktop', 'mobile' ),
);
}
return true;
}
/**
* Handler for HistoryRevisionTools and DiffRevisionTools hooks.
* Inserts 'thank' link into revision interface

View file

@ -75,6 +75,7 @@ $wgHooks['BeforeSpecialMobileDiffDisplay'][] = 'ThanksHooks::onBeforeSpecialMobi
$wgHooks['UnitTestsList'][] = 'ThanksHooks::registerUnitTests';
$wgHooks['GetLogTypesOnUser'][] = 'ThanksHooks::onGetLogTypesOnUser';
$wgHooks['BeforePageDisplay'][] = 'ThanksHooks::onBeforePageDisplay';
$wgHooks['ResourceLoaderTestModules'][] = 'ThanksHooks::onResourceLoaderTestModules';
// Register modules
$wgResourceModules['ext.thanks'] = array(

View file

@ -4,6 +4,13 @@
SchemaMobileWebClickTracking = M.require( 'loggingSchemas/SchemaMobileWebClickTracking' ),
schema = new SchemaMobileWebClickTracking( {}, 'MobileWebDiffClickTracking' );
/**
* Create a thank button for a given edit
*
* @param {String} name The username of the user who made the edit
* @param {String} revision The revision the user created
* @param {String} gender The gender of the user who made the edit
*/
function thankUser( name, revision, gender ) {
var d = $.Deferred();
api.getToken( 'edit' ).done( function( token ) {
@ -37,9 +44,9 @@
/**
* Create a thank button for a given edit
*
* @param name String The username of the user who made the edit
* @param rev String The revision the user created
* @param gender String The gender of the user who made the edit
* @param {String} name The username of the user who made the edit
* @param {String} rev The revision the user created
* @param {String} gender The gender of the user who made the edit
*/
function createThankLink( name, rev, gender ) {
var thankImg = mw.config.get( 'wgExtensionAssetsPath' ) + '/Thanks/WhiteSmiley.png',
@ -74,9 +81,14 @@
}
}
$( function() {
var $user = $( '.mw-mf-user' ),
username = $user.data( 'user-name' ),
/**
* Initialise a thank button in the given container.
*
* @param {jQuery.Object} $user existing element with data attributes associated describing a user.
* @param {jQuery.Object} $container to render button in
*/
function init( $user, $container ) {
var username = $user.data( 'user-name' ),
rev = $user.data( 'revision-id' ),
gender = $user.data( 'user-gender' ),
$thankBtn;
@ -84,8 +96,17 @@
if ( !$user.hasClass( 'mw-mf-anon' ) ) {
$thankBtn = createThankLink( username, rev, gender );
if ( $thankBtn ) {
$thankBtn.prependTo( '#mw-mf-userinfo' );
$thankBtn.prependTo( $container );
}
}
}
$( function () {
init( $( '.mw-mf-user' ), $( '#mw-mf-userinfo' ) );
} );
// Expose for testing purposes
mw.thanks = $.extend( {}, mw.thanks || {}, {
_mobileDiffInit: init
} );
} )( mw.mobileFrontend, jQuery );

View file

@ -0,0 +1,22 @@
( function ( $ ) {
QUnit.module( 'Thanks mobilediff' );
QUnit.test( 'render button for logged in users', 1, function ( assert ) {
var $container = $( '<div>' ),
$user = $( '<div>' ).data( 'user-name', 'jon' )
.data( 'revision-id', 1 )
.data( 'user-gender', 'male' );
mw.thanks._mobileDiffInit( $user, $container );
assert.strictEqual( $container.find( 'button' ).length, 1, 'Thanks button was created.' );
} );
QUnit.test( 'Do not render button for anon users', 1, function ( assert ) {
var $container = $( '<div>' ),
$user = $( '<div class="mw-mf-anon">' );
mw.thanks._mobileDiffInit( $user, $container );
assert.strictEqual( $container.find( 'button' ).length, 0, 'No thanks button was created.' );
} );
}( jQuery ) );