diff options
author | Richard Gibson <richard.gibson@gmail.com> | 2018-09-07 10:14:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-07 10:14:01 -0400 |
commit | dfa92ccead70d7dd5735a36c6d0dd1af680271cd (patch) | |
tree | 95d7500d4ddcc944f3d388216c0c6bec211461d2 /test | |
parent | f997241f0011ed728be71002bc703c7a0d3f01e5 (diff) | |
download | jquery-dfa92ccead70d7dd5735a36c6d0dd1af680271cd.tar.gz jquery-dfa92ccead70d7dd5735a36c6d0dd1af680271cd.zip |
Tests: Allow Karma to load unminfied source
Closes gh-4128
Diffstat (limited to 'test')
-rw-r--r-- | test/data/testinit.js | 42 | ||||
-rw-r--r-- | test/jquery.js | 31 | ||||
-rw-r--r-- | test/unit/ajax.js | 41 |
3 files changed, 75 insertions, 39 deletions
diff --git a/test/data/testinit.js b/test/data/testinit.js index a91e59b6a..a87ebf206 100644 --- a/test/data/testinit.js +++ b/test/data/testinit.js @@ -1,11 +1,17 @@ /* eslint no-multi-str: "off" */ -// baseURL is intentionally set to "data/" instead of "". -// This is not just for convenience (since most files are in data/) -// but also to ensure that urls without prefix fail. -// Otherwise it's easy to write tests that pass on test/index.html -// but fail in Karma runner (where the baseURL is different). -var baseURL = "data/", +var FILEPATH = "/test/data/testinit.js", + activeScript = [].slice.call( document.getElementsByTagName( "script" ), -1 )[ 0 ], + parentUrl = activeScript && activeScript.src ? + activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" : + "../", + + // baseURL is intentionally set to "data/" instead of "". + // This is not just for convenience (since most files are in data/) + // but also to ensure that urls without prefix fail. + // Otherwise it's easy to write tests that pass on test/index.html + // but fail in Karma runner (where the baseURL is different). + baseURL = parentUrl + "test/data/", supportjQuery = this.jQuery, // see RFC 2606 @@ -271,12 +277,9 @@ this.testIframe = function( title, fileName, func, wrapper ) { }; this.iframeCallback = undefined; -if ( window.__karma__ ) { - // In Karma, files are served from /base - baseURL = "base/test/data/"; -} else { - // Tests are always loaded async - // except when running tests in Karma (See Gruntfile) +// Tests are always loaded async +// except when running tests in Karma (See Gruntfile) +if ( !window.__karma__ ) { QUnit.config.autostart = false; } @@ -295,8 +298,19 @@ moduleTypeSupported(); this.loadTests = function() { + // Directly load tests that need synchronous evaluation + if ( !QUnit.urlParams.amd || document.readyState === "loading" ) { + document.write( "<script src='" + parentUrl + "test/unit/ready.js'><\x2Fscript>" ); + } else { + QUnit.module( "ready", function() { + QUnit.test( "jQuery ready", function( assert ) { + assert.ok( false, "Test should be initialized before DOM ready" ); + } ); + } ); + } + // Get testSubproject from testrunner first - require( [ "data/testrunner.js" ], function() { + require( [ parentUrl + "test/data/testrunner.js" ], function() { var i = 0, tests = [ // A special module with basic tests, meant for @@ -334,7 +348,7 @@ this.loadTests = function() { if ( dep ) { if ( !QUnit.basicTests || i === 1 ) { - require( [ dep ], loadDep ); + require( [ parentUrl + "test/" + dep ], loadDep ); // Support: Android 2.3 only // When running basic tests, replace other modules with dummies to avoid overloading diff --git a/test/jquery.js b/test/jquery.js index d8eaf8629..8ba139e6b 100644 --- a/test/jquery.js +++ b/test/jquery.js @@ -2,8 +2,11 @@ ( function() { /* global loadTests: false */ - var pathname = window.location.pathname, - path = pathname.slice( 0, pathname.lastIndexOf( "test" ) ), + var FILEPATH = "/test/jquery.js", + activeScript = [].slice.call( document.getElementsByTagName( "script" ), -1 )[ 0 ], + parentUrl = activeScript && activeScript.src ? + activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" : + "../", QUnit = window.QUnit || parent.QUnit, require = window.require || parent.require, @@ -17,11 +20,16 @@ // Define configuration parameters controlling how jQuery is loaded if ( QUnit ) { - QUnit.config.urlConfig.push( { - id: "amd", - label: "Load with AMD", - tooltip: "Load the AMD jQuery file (and its dependencies)" - } ); + + // AMD loading is asynchronous and incompatible with synchronous test loading in Karma + if ( !window.__karma__ ) { + QUnit.config.urlConfig.push( { + id: "amd", + label: "Load with AMD", + tooltip: "Load the AMD jQuery file (and its dependencies)" + } ); + } + QUnit.config.urlConfig.push( { id: "dev", label: "Load unminified", @@ -33,7 +41,7 @@ // This doesn't apply to iframes because they synchronously expect jQuery to be there. if ( urlParams.amd && window.QUnit ) { require.config( { - baseUrl: path + baseUrl: parentUrl } ); src = "src/jquery"; @@ -46,12 +54,7 @@ // Otherwise, load synchronously } else { - document.write( "<script id='jquery-js' src='" + path + src + "'><\x2Fscript>" ); - - // Synchronous-only tests (other tests are loaded from the test page) - if ( typeof loadTests !== "undefined" ) { - document.write( "<script src='" + path + "test/unit/ready.js'><\x2Fscript>" ); - } + document.write( "<script id='jquery-js' src='" + parentUrl + src + "'><\x2Fscript>" ); } } )(); diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 72a4e7215..b8b46e245 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -345,12 +345,13 @@ QUnit.module( "ajax", { }; } ); - ajaxTest( "jQuery.ajax() - hash", 4, function( assert ) { + ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) { return [ { url: baseURL + "name.html#foo", beforeSend: function( xhr, settings ) { - assert.equal( settings.url, baseURL + "name.html#foo", "Make sure that the URL has its hash." ); + assert.equal( settings.url, baseURL + "name.html#foo", + "hash preserved for request with no query component." ); return false; }, error: true @@ -358,7 +359,8 @@ QUnit.module( "ajax", { { url: baseURL + "name.html?abc#foo", beforeSend: function( xhr, settings ) { - assert.equal( settings.url, baseURL + "name.html?abc#foo", "Make sure that the URL has its hash." ); + assert.equal( settings.url, baseURL + "name.html?abc#foo", + "hash preserved for request with query component." ); return false; }, error: true @@ -369,7 +371,8 @@ QUnit.module( "ajax", { "test": 123 }, beforeSend: function( xhr, settings ) { - assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo", "Make sure that the URL has its hash." ); + assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo", + "hash preserved for request with query component and data." ); return false; }, error: true @@ -381,9 +384,10 @@ QUnit.module( "ajax", { }, cache: false, beforeSend: function( xhr, settings ) { - // Remove the random number, but ensure the cache-buster param is there - var url = settings.url.replace( /\d+/, "" ); - assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies", "Make sure that the URL has its hash." ); + // Clear the cache-buster param value + var url = settings.url.replace( /_=[^&#]+/, "_=" ); + assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies", + "hash preserved for cache-busting request with query component and data." ); return false; }, error: true @@ -1133,7 +1137,7 @@ QUnit.module( "ajax", { setup: function() { Globals.register( "testBar" ); }, - url: window.location.href.replace( /[^\/]*$/, "" ) + baseURL + "mock.php?action=testbar", + url: url( "mock.php?action=testbar" ), dataType: "script", success: function() { assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" ); @@ -1146,7 +1150,7 @@ QUnit.module( "ajax", { setup: function() { Globals.register( "testBar" ); }, - url: window.location.href.replace( /[^\/]*$/, "" ) + baseURL + "mock.php?action=testbar", + url: url( "mock.php?action=testbar" ), type: "POST", dataType: "script", success: function( data, status ) { @@ -1161,7 +1165,7 @@ QUnit.module( "ajax", { setup: function() { Globals.register( "testBar" ); }, - url: window.location.href.replace( /[^\/]*$/, "" ).replace( /^.*?\/\//, "//" ) + baseURL + "mock.php?action=testbar", + url: url( "mock.php?action=testbar" ), dataType: "script", success: function() { assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" ); @@ -2303,7 +2307,22 @@ if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().re } ); QUnit.asyncTest( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", 2, function( assert ) { - jQuery.getJSON( window.location.href.replace( /[^\/]*$/, "" ) + url( "mock.php?action=json" ), function( json ) { + var absoluteUrl = url( "mock.php?action=json" ); + + // Make a relative URL absolute relative to the document location + if ( !/^[a-z][a-z0-9+.-]*:/i.test( absoluteUrl ) ) { + + // An absolute path replaces everything after the host + if ( absoluteUrl.charAt( 0 ) === "/" ) { + absoluteUrl = window.location.href.replace( /(:\/*[^/]*).*$/, "$1" ) + absoluteUrl; + + // A relative path replaces the last slash-separated path segment + } else { + absoluteUrl = window.location.href.replace( /[^/]*$/, "" ) + absoluteUrl; + } + } + + jQuery.getJSON( absoluteUrl, function( json ) { assert.strictEqual( json.data.lang, "en", "Check JSON: lang" ); assert.strictEqual( json.data.length, 25, "Check JSON: length" ); QUnit.start(); |