mediawiki.util: Simplify test-only setOptions methods
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 8 Jul 2019 21:54:53 +0000 (22:54 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 8 Jul 2019 21:56:15 +0000 (22:56 +0100)
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
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index 66c1fe7..36a0195 100644 (file)
@@ -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
         */
        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
                 *
                }
        };
 
+       // 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
         */
index 3679ed7..17672db 100644 (file)
        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