mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Add web worker unit test. [closes #313]
Former-commit-id: 77d0b907a5ae58ff3f4da8b54196b12949a4efd4
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
Object.keys = function() { return []; };
|
Object.keys = function() { return []; };
|
||||||
|
|
||||||
// load Lo-Dash and expose it to the bad `Object.keys` shim
|
// load Lo-Dash and expose it to the bad `Object.keys` shim
|
||||||
document.write('<script src="../' + ui.buildPath + '"><\/script>');
|
document.write('<script src="' + ui.buildPath + '"><\/script>');
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
// store Lo-Dash to test for bad shim detection
|
// store Lo-Dash to test for bad shim detection
|
||||||
@@ -33,12 +33,12 @@
|
|||||||
delete Object._keys;
|
delete Object._keys;
|
||||||
|
|
||||||
// load Lo-Dash again to overwrite the existing `_` value
|
// load Lo-Dash again to overwrite the existing `_` value
|
||||||
document.write('<script src="../' + ui.buildPath + '"><\/script>');
|
document.write('<script src="' + ui.buildPath + '"><\/script>');
|
||||||
|
|
||||||
// load test.js if not using require.js
|
// load test.js if not using require.js
|
||||||
document.write(QUnit.urlParams.loader == 'none'
|
document.write(ui.urlParams.loader != 'none'
|
||||||
? '<script src="test.js"><\/script>'
|
? '<script data-dojo-config="async:1" src="' + ui.loaderPath + '"><\/script>'
|
||||||
: '<script data-dojo-config="async:1,parseOnLoad:false" src="../' + ui.loaderPath + '"><\/script>'
|
: '<script src="test.js"><\/script>'
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
(function() {
|
(function() {
|
||||||
var modulePath = ui.buildPath.replace(/\.js$/, '');
|
var modulePath = ui.buildPath.replace(/\.js$/, '');
|
||||||
return {
|
return {
|
||||||
'baseUrl': '../' + (/modularize/.test(QUnit.urlParams.build) ? 'modularize/' : ''),
|
'baseUrl': (/modularize/.test(ui.urlParams.build) ? 'modularize/' : ''),
|
||||||
'urlArgs': 't=' + (+new Date),
|
'urlArgs': 't=' + (+new Date),
|
||||||
'paths': {
|
'paths': {
|
||||||
'lodash': modulePath,
|
'lodash': modulePath,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
;(function(window) {
|
;(function(window) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/** `QUnit.addEvent` shortcut */
|
/** The base path of the builds */
|
||||||
var addEvent = QUnit.addEvent;
|
var basePath = '../';
|
||||||
|
|
||||||
/** The Lo-Dash build to load */
|
/** The Lo-Dash build to load */
|
||||||
var build = (/build=([^&]+)/.exec(location.search) || [])[1];
|
var build = (/build=([^&]+)/.exec(location.search) || [])[1];
|
||||||
@@ -15,36 +15,63 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers an event listener on an element.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Element} element The element.
|
||||||
|
* @param {String} eventName The name of the event.
|
||||||
|
* @param {Function} handler The event handler.
|
||||||
|
* @returns {Element} The element.
|
||||||
|
*/
|
||||||
|
function addEvent(element, eventName, handler) {
|
||||||
|
if (typeof element.addEventListener != 'undefined') {
|
||||||
|
element.addEventListener(eventName, handler, false);
|
||||||
|
} else if (typeof element.attachEvent != 'undefined') {
|
||||||
|
element.attachEvent('on' + eventName, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
// expose `ui.urlParams` properties
|
||||||
|
ui.urlParams = {
|
||||||
|
'build': build,
|
||||||
|
'loader': loader
|
||||||
|
};
|
||||||
|
|
||||||
// expose Lo-Dash build file path
|
// expose Lo-Dash build file path
|
||||||
ui.buildPath = (function() {
|
ui.buildPath = (function() {
|
||||||
|
var result;
|
||||||
switch (build) {
|
switch (build) {
|
||||||
case 'lodash-compat': return 'dist/lodash.compat.min.js';
|
case 'lodash-compat': result = 'dist/lodash.compat.min.js'; break;
|
||||||
case 'lodash-modern-dev': return 'dist/lodash.js';
|
case 'lodash-modern-dev': result = 'dist/lodash.js'; break;
|
||||||
case 'lodash-modern': return 'dist/lodash.min.js';
|
case 'lodash-modern': result = 'dist/lodash.min.js'; break;
|
||||||
case 'lodash-legacy': return 'dist/lodash.legacy.min.js';
|
case 'lodash-legacy': result = 'dist/lodash.legacy.min.js'; break;
|
||||||
case 'lodash-mobile': return 'dist/lodash.mobile.min.js';
|
case 'lodash-mobile': result = 'dist/lodash.mobile.min.js'; break;
|
||||||
case 'lodash-underscore': return 'dist/lodash.underscore.min.js';
|
case 'lodash-underscore': result = 'dist/lodash.underscore.min.js'; break;
|
||||||
case 'lodash-custom-dev': return 'lodash.custom.js';
|
case 'lodash-custom-dev': result = 'lodash.custom.js'; break;
|
||||||
case 'lodash-custom': return 'lodash.custom.min.js';
|
case 'lodash-custom': result = 'lodash.custom.min.js'; break;
|
||||||
|
case 'lodash-compat-dev':
|
||||||
|
case undefined: result = 'lodash.js'; break;
|
||||||
|
default: result = build;
|
||||||
}
|
}
|
||||||
return 'lodash.js';
|
return result == build ? result : (basePath + result);
|
||||||
}());
|
}());
|
||||||
|
|
||||||
// expose module loader file path
|
// expose module loader file path
|
||||||
ui.loaderPath = (function() {
|
ui.loaderPath = (function() {
|
||||||
|
var result;
|
||||||
switch (loader) {
|
switch (loader) {
|
||||||
case 'curl': return 'vendor/curl/src/curl.js';
|
case 'curl': result = 'vendor/curl/src/curl.js'; break;
|
||||||
case 'dojo': return 'vendor/dojo/dojo.js';
|
case 'dojo': result = 'vendor/dojo/dojo.js'; break;
|
||||||
|
case 'requirejs':
|
||||||
|
case undefined: result = 'vendor/requirejs/require.js'; break;
|
||||||
|
default: result = loader;
|
||||||
}
|
}
|
||||||
return 'vendor/requirejs/require.js';
|
return result == loader ? result : (basePath + result);
|
||||||
}());
|
}());
|
||||||
|
|
||||||
// assign `QUnit.urlParams` properties
|
|
||||||
QUnit.extend(QUnit.urlParams, {
|
|
||||||
'build': build,
|
|
||||||
'loader': loader
|
|
||||||
});
|
|
||||||
|
|
||||||
// initialize controls
|
// initialize controls
|
||||||
addEvent(window, 'load', function() {
|
addEvent(window, 'load', function() {
|
||||||
function eventHandler(event) {
|
function eventHandler(event) {
|
||||||
|
|||||||
48
test/test.js
48
test/test.js
@@ -11,7 +11,8 @@
|
|||||||
process = window.process,
|
process = window.process,
|
||||||
slice = Array.prototype.slice,
|
slice = Array.prototype.slice,
|
||||||
system = window.system,
|
system = window.system,
|
||||||
toString = Object.prototype.toString;
|
toString = Object.prototype.toString,
|
||||||
|
Worker = window.Worker;
|
||||||
|
|
||||||
/** Use a single "load" function */
|
/** Use a single "load" function */
|
||||||
var load = !amd && typeof require == 'function' ? require : window.load;
|
var load = !amd && typeof require == 'function' ? require : window.load;
|
||||||
@@ -62,15 +63,7 @@
|
|||||||
));
|
));
|
||||||
|
|
||||||
/** Used to pass falsey values to methods */
|
/** Used to pass falsey values to methods */
|
||||||
var falsey = [
|
var falsey = [, '', 0, false, NaN, null, undefined];
|
||||||
,
|
|
||||||
'',
|
|
||||||
0,
|
|
||||||
false,
|
|
||||||
NaN,
|
|
||||||
null,
|
|
||||||
undefined
|
|
||||||
];
|
|
||||||
|
|
||||||
/** Used as the size when optimizations are enabled for large arrays */
|
/** Used as the size when optimizations are enabled for large arrays */
|
||||||
var largeArraySize = 75;
|
var largeArraySize = 75;
|
||||||
@@ -101,6 +94,7 @@
|
|||||||
|
|
||||||
/** The `ui` object */
|
/** The `ui` object */
|
||||||
var ui = window.ui || (window.ui = {
|
var ui = window.ui || (window.ui = {
|
||||||
|
'buildPath': filePath,
|
||||||
'loaderPath': ''
|
'loaderPath': ''
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -135,6 +129,19 @@
|
|||||||
idoc.close();
|
idoc.close();
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
// add web worker
|
||||||
|
(function() {
|
||||||
|
if (!Worker) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var worker = new Worker('./worker.js');
|
||||||
|
worker.addEventListener('message', function(e) {
|
||||||
|
_._VERSION = e.data || '';
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
worker.postMessage(ui.buildPath);
|
||||||
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
// explicitly call `QUnit.module()` instead of `module()`
|
// explicitly call `QUnit.module()` instead of `module()`
|
||||||
@@ -166,6 +173,27 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
asyncTest('supports loading ' + basename + ' in a web worker', function() {
|
||||||
|
if (Worker) {
|
||||||
|
var limit = 1000,
|
||||||
|
start = new Date,
|
||||||
|
wait = 16;
|
||||||
|
|
||||||
|
setTimeout(function attempt() {
|
||||||
|
var actual = _._VERSION;
|
||||||
|
if ((new Date - start) < limit && typeof actual != 'string') {
|
||||||
|
setTimeout(attempt, wait);
|
||||||
|
} else {
|
||||||
|
equal(actual, _.VERSION);
|
||||||
|
QUnit.start();
|
||||||
|
}
|
||||||
|
}, wait);
|
||||||
|
} else {
|
||||||
|
skipTest();
|
||||||
|
QUnit.start();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('avoids overwritten native methods', function() {
|
test('avoids overwritten native methods', function() {
|
||||||
if (document && !phantom) {
|
if (document && !phantom) {
|
||||||
notDeepEqual(lodashBadShim.keys({ 'a': 1 }), []);
|
notDeepEqual(lodashBadShim.keys({ 'a': 1 }), []);
|
||||||
|
|||||||
6
test/worker.js
Normal file
6
test/worker.js
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
addEventListener('message', function(e) {
|
||||||
|
if (e.data) {
|
||||||
|
importScripts(e.data);
|
||||||
|
postMessage(_.VERSION);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user