From 720dab42d9dbe9c97f3526f16ef2cb0b934aa7a9 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 8 Jul 2019 22:54:53 +0100 Subject: [PATCH] mediawiki.util: Simplify test-only setOptions methods Follows-up 1c7c9bdf1f. * Define the method only in test context, instead of a run-time check. * Return the old value, and re-use function for restoring the result afterwards. * Make the test define its options by default, so at to not rely on local-wiki defaults in any way. Change-Id: I71e92d5b9ee789b594e6e64ca3e425583fc1ee1c --- resources/src/mediawiki.util.js | 28 +++++++------------ .../mediawiki/mediawiki.util.test.js | 6 +++- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/resources/src/mediawiki.util.js b/resources/src/mediawiki.util.js index 66c1fe74d1..36a0195d84 100644 --- a/resources/src/mediawiki.util.js +++ b/resources/src/mediawiki.util.js @@ -2,8 +2,7 @@ 'use strict'; var util, - config = require( './config.json' ), - origConfig = config; + config = require( './config.json' ); /** * Encode the string like PHP's rawurlencode @@ -50,22 +49,6 @@ */ util = { - /* Main body */ - - setOptionsForTest: function ( opts ) { - if ( !window.QUnit ) { - throw new Error( 'Modifying options not allowed outside unit tests' ); - } - config = $.extend( {}, config, opts ); - }, - - resetOptionsForTest: function () { - if ( !window.QUnit ) { - throw new Error( 'Resetting options not allowed outside unit tests' ); - } - config = origConfig; - }, - /** * Encode the string like PHP's rawurlencode * @@ -533,6 +516,15 @@ } }; + // Not allowed outside unit tests + if ( window.QUnit ) { + util.setOptionsForTest = function ( opts ) { + var oldConfig = config; + config = $.extend( {}, config, opts ); + return oldConfig; + }; + } + /** * Initialisation of mw.util.$content */ diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index 3679ed76f9..17672db80e 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -77,10 +77,14 @@ QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment( { setup: function () { $.fn.updateTooltipAccessKeys.setTestMode( true ); + this.origConfig = mw.util.setOptionsForTest( { + FragmentMode: [ 'legacy', 'html5' ], + LoadScript: '/w/load.php' + } ); }, teardown: function () { $.fn.updateTooltipAccessKeys.setTestMode( false ); - mw.util.resetOptionsForTest(); + mw.util.setOptionsForTest( this.origConfig ); }, messages: { // Used by accessKeyLabel in test for addPortletLink -- 2.20.1