mw.loader: Add tests for mw.loader StringSet fallback
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 28 Sep 2016 18:12:52 +0000 (19:12 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 7 Mar 2017 23:14:37 +0000 (15:14 -0800)
Change-Id: Ia00d793f0332531b5afa006fe8453e2e733d40ba

resources/src/mediawiki/mediawiki.js
tests/qunit/suites/resources/mediawiki/mediawiki.loader.test.js

index 33f146b..c2cee7e 100644 (file)
                /* eslint-enable no-bitwise */
        }
 
-       // <https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set>
-       StringSet = window.Set || ( function () {
-               /**
-                * @private
-                * @class
-                */
-               function StringSet() {
-                       this.set = {};
-               }
-               StringSet.prototype.add = function ( value ) {
-                       this.set[ value ] = true;
-               };
-               StringSet.prototype.has = function ( value ) {
-                       return hasOwn.call( this.set, value );
-               };
-               return StringSet;
-       }() );
+       function defineFallbacks() {
+               // <https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set>
+               StringSet = window.Set || ( function () {
+                       /**
+                        * @private
+                        * @class
+                        */
+                       function StringSet() {
+                               this.set = {};
+                       }
+                       StringSet.prototype.add = function ( value ) {
+                               this.set[ value ] = true;
+                       };
+                       StringSet.prototype.has = function ( value ) {
+                               return hasOwn.call( this.set, value );
+                       };
+                       return StringSet;
+               }() );
+       }
 
        /**
         * Create an object that can be read from or written to via methods that allow
                }
        };
 
+       defineFallbacks();
+
        /* eslint-disable no-console */
        log = ( function () {
                // Also update the restoration of methods in mediawiki.log.js
         * @class mw
         */
        mw = {
+               redefineFallbacksForTest: function () {
+                       if ( !window.QUnit ) {
+                               throw new Error( 'Reset not allowed outside unit tests' );
+                       }
+                       defineFallbacks();
+               },
 
                /**
                 * Get the current time, measured in milliseconds since January 1, 1970 (UTC).
index 6f9af76..477b04d 100644 (file)
@@ -5,6 +5,11 @@
                },
                teardown: function () {
                        mw.loader.store.enabled = false;
+                       // Teardown for StringSet shim test
+                       if ( this.nativeSet ) {
+                               window.Set = this.nativeSet;
+                               mw.redefineFallbacksForTest();
+                       }
                }
        } ) );
 
                } );
        } );
 
-       QUnit.test( '.using() Error: Circular dependency', function ( assert ) {
+       // Covers mw.loader#sortDependencies (with native Set if available)
+       QUnit.test( '.using() Error: Circular dependency [StringSet default]', function ( assert ) {
                var done = assert.async();
 
                mw.loader.register( [
                .always( done );
        } );
 
+       // @covers mw.loader#sortDependencies (with fallback shim)
+       QUnit.test( '.using() Error: Circular dependency [StringSet shim]', function ( assert ) {
+               var done = assert.async();
+
+               if ( !window.Set ) {
+                       assert.expect( 0 );
+                       done();
+                       return;
+               }
+
+               this.nativeSet = window.Set;
+               window.Set = undefined;
+               mw.redefineFallbacksForTest();
+
+               mw.loader.register( [
+                       [ 'test.shim.circle1', '0', [ 'test.shim.circle2' ] ],
+                       [ 'test.shim.circle2', '0', [ 'test.shim.circle3' ] ],
+                       [ 'test.shim.circle3', '0', [ 'test.shim.circle1' ] ]
+               ] );
+               mw.loader.using( 'test.shim.circle3' ).then(
+                       function done() {
+                               assert.ok( false, 'Unexpected resolution, expected error.' );
+                       },
+                       function fail( e ) {
+                               assert.ok( /Circular/.test( String( e ) ), 'Detect circular dependency' );
+                       }
+               )
+               .always( done );
+       } );
+
        QUnit.test( '.load() - Error: Circular dependency', function ( assert ) {
                mw.loader.register( [
                        [ 'test.circleA', '0', [ 'test.circleB' ] ],