Add web worker unit test. [closes #313]

Former-commit-id: 77d0b907a5ae58ff3f4da8b54196b12949a4efd4
This commit is contained in:
John-David Dalton
2013-07-07 23:22:56 -07:00
parent 9c65d9f957
commit eb6b5755e7
4 changed files with 97 additions and 36 deletions

View File

@@ -22,7 +22,7 @@
Object.keys = function() { return []; };
// 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>
// store Lo-Dash to test for bad shim detection
@@ -33,12 +33,12 @@
delete Object._keys;
// 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
document.write(QUnit.urlParams.loader == 'none'
? '<script src="test.js"><\/script>'
: '<script data-dojo-config="async:1,parseOnLoad:false" src="../' + ui.loaderPath + '"><\/script>'
document.write(ui.urlParams.loader != 'none'
? '<script data-dojo-config="async:1" src="' + ui.loaderPath + '"><\/script>'
: '<script src="test.js"><\/script>'
);
</script>
<script>
@@ -54,7 +54,7 @@
(function() {
var modulePath = ui.buildPath.replace(/\.js$/, '');
return {
'baseUrl': '../' + (/modularize/.test(QUnit.urlParams.build) ? 'modularize/' : ''),
'baseUrl': (/modularize/.test(ui.urlParams.build) ? 'modularize/' : ''),
'urlArgs': 't=' + (+new Date),
'paths': {
'lodash': modulePath,

View File

@@ -1,8 +1,8 @@
;(function(window) {
'use strict';
/** `QUnit.addEvent` shortcut */
var addEvent = QUnit.addEvent;
/** The base path of the builds */
var basePath = '../';
/** The Lo-Dash build to load */
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
ui.buildPath = (function() {
var result;
switch (build) {
case 'lodash-compat': return 'dist/lodash.compat.min.js';
case 'lodash-modern-dev': return 'dist/lodash.js';
case 'lodash-modern': return 'dist/lodash.min.js';
case 'lodash-legacy': return 'dist/lodash.legacy.min.js';
case 'lodash-mobile': return 'dist/lodash.mobile.min.js';
case 'lodash-underscore': return 'dist/lodash.underscore.min.js';
case 'lodash-custom-dev': return 'lodash.custom.js';
case 'lodash-custom': return 'lodash.custom.min.js';
case 'lodash-compat': result = 'dist/lodash.compat.min.js'; break;
case 'lodash-modern-dev': result = 'dist/lodash.js'; break;
case 'lodash-modern': result = 'dist/lodash.min.js'; break;
case 'lodash-legacy': result = 'dist/lodash.legacy.min.js'; break;
case 'lodash-mobile': result = 'dist/lodash.mobile.min.js'; break;
case 'lodash-underscore': result = 'dist/lodash.underscore.min.js'; break;
case 'lodash-custom-dev': result = 'lodash.custom.js'; break;
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
ui.loaderPath = (function() {
var result;
switch (loader) {
case 'curl': return 'vendor/curl/src/curl.js';
case 'dojo': return 'vendor/dojo/dojo.js';
case 'curl': result = 'vendor/curl/src/curl.js'; break;
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
addEvent(window, 'load', function() {
function eventHandler(event) {

View File

@@ -11,7 +11,8 @@
process = window.process,
slice = Array.prototype.slice,
system = window.system,
toString = Object.prototype.toString;
toString = Object.prototype.toString,
Worker = window.Worker;
/** Use a single "load" function */
var load = !amd && typeof require == 'function' ? require : window.load;
@@ -62,15 +63,7 @@
));
/** Used to pass falsey values to methods */
var falsey = [
,
'',
0,
false,
NaN,
null,
undefined
];
var falsey = [, '', 0, false, NaN, null, undefined];
/** Used as the size when optimizations are enabled for large arrays */
var largeArraySize = 75;
@@ -101,6 +94,7 @@
/** The `ui` object */
var ui = window.ui || (window.ui = {
'buildPath': filePath,
'loaderPath': ''
});
@@ -135,6 +129,19 @@
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()`
@@ -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() {
if (document && !phantom) {
notDeepEqual(lodashBadShim.keys({ 'a': 1 }), []);

6
test/worker.js Normal file
View File

@@ -0,0 +1,6 @@
addEventListener('message', function(e) {
if (e.data) {
importScripts(e.data);
postMessage(_.VERSION);
}
});