Fix ULS QUnit tests
Tests were failing for two reasons: wrong modules as dependencies of the test module and test timing out due to incorrect usage of QUnit.asyncTest and QUnit.start() and QUnit.Stop. While at it, did some small cleanups and converted the number of assertions to use non-deprecated QUnit.expect() instead. Options are saved using postWithToken of mw.API. This also rises minimum MediaWiki version to 1.22. This allowed removing hundred lines of code. MLEB is already 1.22 and above. Change-Id: I46a1674e4ede48a0e331c8d201d1d847db51d9dd
This commit is contained in:
committed by
Santhosh
parent
c5bca60536
commit
d25f7f3307
@@ -102,7 +102,7 @@ class UniversalLanguageSelectorHooks {
|
||||
public static function addTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
|
||||
$testModules['qunit']['ext.uls.tests'] = array(
|
||||
'scripts' => array( 'tests/qunit/ext.uls.tests.js' ),
|
||||
'dependencies' => array( 'ext.uls.init', 'ext.uls.interface' ),
|
||||
'dependencies' => array( 'jquery.uls', 'ext.uls.preferences' ),
|
||||
'localBasePath' => __DIR__,
|
||||
'remoteExtPath' => 'UniversalLanguageSelector',
|
||||
);
|
||||
|
||||
@@ -20,86 +20,7 @@
|
||||
( function ( $, mw ) {
|
||||
'use strict';
|
||||
|
||||
var ULSPreferences,
|
||||
cachedOptionsToken = null;
|
||||
|
||||
/**
|
||||
* Post to options API with correct token.
|
||||
* If we have no token, get one and try to post.
|
||||
* If we have a cached token try using that,
|
||||
* and if it fails, blank out the cached token and start over.
|
||||
*
|
||||
* @param params {Object} API parameters
|
||||
* @param ok {Function} callback for success
|
||||
* @param err {Function} [optional] error callback
|
||||
* @return {jqXHR}
|
||||
*/
|
||||
function saveOptionsWithToken( params, ok, err ) {
|
||||
if ( cachedOptionsToken === null ) {
|
||||
// We don't have a valid cached token, so get a fresh one and try posting.
|
||||
// We do not trap any 'badtoken' or 'notoken' errors, because we don't want
|
||||
// an infinite loop. If this fresh token is bad, something else is very wrong.
|
||||
return getOptionsToken( function ( token ) {
|
||||
params.token = token;
|
||||
new mw.Api().post( params, ok, err );
|
||||
}, err );
|
||||
} else {
|
||||
params.token = cachedOptionsToken;
|
||||
|
||||
return new mw.Api().post( params, {
|
||||
ok: ok,
|
||||
err: function ( code, result ) {
|
||||
// We do have a token, but it might be expired.
|
||||
// So if it is 'bad', then start over with a new token.
|
||||
if ( code === 'badtoken' ) {
|
||||
// force a new token, clear any old one
|
||||
cachedOptionsToken = null;
|
||||
saveOptionsWithToken( params, ok, err );
|
||||
} else {
|
||||
err( code, result );
|
||||
}
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Api helper to grab an options token
|
||||
*
|
||||
* token callback has signature ( String token )
|
||||
* error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
|
||||
* Note that xhr and exception are only available for 'http_*' errors
|
||||
* code may be any http_* error code (see mw.Api), or 'token_missing'
|
||||
*
|
||||
* @param tokenCallback {Function} received token callback
|
||||
* @param err {Function} error callback
|
||||
* @return {jqXHR}
|
||||
*/
|
||||
function getOptionsToken( tokenCallback, err ) {
|
||||
return new mw.Api().get( {
|
||||
action: 'tokens',
|
||||
type: 'options'
|
||||
}, {
|
||||
ok: function ( data ) {
|
||||
var token;
|
||||
|
||||
// If token type is not available for this user,
|
||||
// key 'translationreviewtoken' is missing or can contain Boolean false
|
||||
if ( data.tokens && data.tokens.optionstoken ) {
|
||||
token = data.tokens.optionstoken;
|
||||
cachedOptionsToken = token;
|
||||
tokenCallback( token );
|
||||
} else {
|
||||
err( 'token-missing', data );
|
||||
}
|
||||
},
|
||||
err: err,
|
||||
// Due to the API assuming we're logged out if we pass the callback-parameter,
|
||||
// we have to disable jQuery's callback system, and instead parse JSON string,
|
||||
// by setting 'jsonp' to false.
|
||||
jsonp: false
|
||||
} );
|
||||
}
|
||||
var ULSPreferences;
|
||||
|
||||
/**
|
||||
* Wrapper for localStorage, falls back to cookie
|
||||
@@ -225,15 +146,14 @@
|
||||
preferenceStore().set( this.preferenceName, this.preferences );
|
||||
callback.call( this, true );
|
||||
} else {
|
||||
|
||||
// Logged in user. Use MW APIs to change preferences
|
||||
saveOptionsWithToken( {
|
||||
new mw.Api().postWithToken( 'options', {
|
||||
action: 'options',
|
||||
optionname: ulsPreferences.preferenceName,
|
||||
optionvalue: JSON.stringify( ulsPreferences.preferences )
|
||||
}, function () {
|
||||
} ).done( function () {
|
||||
callback.call( this, true );
|
||||
}, function () {
|
||||
} ).fail( function () {
|
||||
callback.call( this, false );
|
||||
} );
|
||||
}
|
||||
|
||||
@@ -22,18 +22,24 @@
|
||||
|
||||
QUnit.module( 'ext.uls', QUnit.newMwEnvironment() );
|
||||
|
||||
QUnit.test( '-- Initial check', 1, function ( assert ) {
|
||||
QUnit.test( 'Initial check', function ( assert ) {
|
||||
QUnit.expect( 1 );
|
||||
|
||||
assert.ok( $.fn.uls, '$.fn.uls is defined' );
|
||||
} );
|
||||
|
||||
QUnit.test( '-- Custom langdb', 1, function ( assert ) {
|
||||
QUnit.test( 'Custom langdb', function ( assert ) {
|
||||
QUnit.expect( 1 );
|
||||
|
||||
// This is a custom non-standard language code used in MW.
|
||||
// If it's not defined, then, for example,
|
||||
// its direction cannot be acquired using the langdb utils.
|
||||
assert.strictEqual( $.uls.data.getDir( 'als' ), 'ltr', 'The direction of custom MW language als is ltr.' );
|
||||
} );
|
||||
|
||||
QUnit.asyncTest( '-- User preferences', 2, function ( assert ) {
|
||||
QUnit.test( 'User preferences', function ( assert ) {
|
||||
QUnit.expect( 2 );
|
||||
|
||||
// 'gofanim' means "fonts" in Hebrew.
|
||||
// Here it's used as a meaningless word, to test
|
||||
// the preferences without changing anything useful.
|
||||
@@ -43,30 +49,39 @@
|
||||
readPrefs;
|
||||
|
||||
prefsToSave[prefName] = {
|
||||
'fonts': {
|
||||
'qqy': 'Megafont'
|
||||
},
|
||||
'webfonts-enabled': true
|
||||
fonts: {
|
||||
qqy: 'Megafont'
|
||||
}
|
||||
};
|
||||
|
||||
prefs.set( prefName, prefsToSave );
|
||||
|
||||
readPrefs = prefs.get( prefName );
|
||||
assert.strictEqual(
|
||||
readPrefs[prefName].fonts.qqy,
|
||||
'Megafont',
|
||||
'Correct value for the font name'
|
||||
);
|
||||
|
||||
QUnit.stop();
|
||||
prefs.save( function ( successSave ) {
|
||||
QUnit.start();
|
||||
assert.ok( successSave, 'Options saving API did not produce an error.' );
|
||||
|
||||
// Delete old options
|
||||
prefs.set( prefName, undefined );
|
||||
QUnit.stop();
|
||||
prefs.save( function () {
|
||||
QUnit.start();
|
||||
} );
|
||||
} );
|
||||
|
||||
readPrefs = prefs.get( prefName );
|
||||
assert.strictEqual( readPrefs[prefName].fonts.qqy, 'Megafont', 'Correct value for the font name' );
|
||||
|
||||
// Delete old options
|
||||
prefs.set( prefName, undefined );
|
||||
prefs.save();
|
||||
} );
|
||||
|
||||
QUnit.test( '-- Common languages', 1, function ( assert ) {
|
||||
QUnit.test( 'Common languages', function ( assert ) {
|
||||
var i, foundTagalog, languagesInPH;
|
||||
|
||||
QUnit.expect( 1 );
|
||||
|
||||
// Bug 49847
|
||||
foundTagalog = false;
|
||||
languagesInPH = mw.uls.getFrequentLanguageList( 'PH' );
|
||||
|
||||
Reference in New Issue
Block a user