The first ever Echo QUnit tests

Also change coding conventions of Resources to allows use
array for scripts key.

Change-Id: I0c9f358ce3eb62adb07b68623e97c07993c9c45c
This commit is contained in:
jdlrobson 2014-08-05 15:18:38 -07:00
parent 0dc45e6c4a
commit 8d12ef9a80
4 changed files with 125 additions and 2 deletions

View file

@ -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';

View file

@ -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;

View file

@ -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',

View file

@ -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 ) );