Allow test.'s to support rhino -require with modularized builds.

This commit is contained in:
John-David Dalton
2013-09-28 17:13:24 -07:00
parent 4b26b46f6a
commit da782398a2

View File

@@ -104,6 +104,9 @@
/** Detects if running in a PhantomJS web page */ /** Detects if running in a PhantomJS web page */
var isPhantomPage = typeof callPhantom == 'function'; var isPhantomPage = typeof callPhantom == 'function';
/** Detect if running in Rhino */
var isRhino = root.java && typeof global == 'function' && global().Array === root.Array;
/** Use a single "load" function */ /** Use a single "load" function */
var load = !amd && typeof require == 'function' ? require : root.load; var load = !amd && typeof require == 'function' ? require : root.load;
@@ -830,125 +833,160 @@
(function() { (function() {
test('subsequent "immediate" debounced calls return the last `func` result', function() { test('subsequent "immediate" debounced calls return the last `func` result', function() {
var debounced = _.debounce(_.identity, 32, true), if (!(isRhino && isModularize)) {
result = [debounced('x'), debounced('y')]; var debounced = _.debounce(_.identity, 32, true),
result = [debounced('x'), debounced('y')];
deepEqual(result, ['x', 'x']); deepEqual(result, ['x', 'x']);
}
else {
skipTest();
}
}); });
asyncTest('subsequent debounced calls return the last `func` result', function() { asyncTest('subsequent debounced calls return the last `func` result', function() {
var debounced = _.debounce(_.identity, 32); if (!(isRhino && isModularize)) {
debounced('x'); var debounced = _.debounce(_.identity, 32);
debounced('x');
setTimeout(function() { setTimeout(function() {
equal(debounced('y'), 'x'); equal(debounced('y'), 'x');
QUnit.start();
}, 64);
}
else {
skipTest();
QUnit.start(); QUnit.start();
}, 64); }
}); });
asyncTest('should apply default options correctly', function() { asyncTest('should apply default options correctly', function() {
var count = 0; if (!(isRhino && isModularize)) {
var count = 0;
var debounced = _.debounce(function(value) { var debounced = _.debounce(function(value) {
count++; count++;
return value; return value;
}, 32, {}); }, 32, {});
strictEqual(debounced('x'), undefined); strictEqual(debounced('x'), undefined);
setTimeout(function() { setTimeout(function() {
strictEqual(count, 1); strictEqual(count, 1);
QUnit.start();
}, 64);
}
else {
skipTest(2);
QUnit.start(); QUnit.start();
}, 64); }
}); });
asyncTest('should work with `leading` option', function() { asyncTest('should work with `leading` option', function() {
var withLeadingAndTrailing, if (!(isRhino && isModularize)) {
counts = [0, 0, 0]; var withLeadingAndTrailing,
counts = [0, 0, 0];
_.forEach([true, { 'leading': true }], function(options, index) { _.forEach([true, { 'leading': true }], function(options, index) {
var debounced = _.debounce(function(value) { var debounced = _.debounce(function(value) {
counts[index]++; counts[index]++;
return value; return value;
}, 32, options); }, 32, options);
if (index == 1) { if (index == 1) {
withLeadingAndTrailing = debounced; withLeadingAndTrailing = debounced;
} }
equal(debounced('x'), 'x'); equal(debounced('x'), 'x');
}); });
_.times(2, _.debounce(function() { counts[2]++; }, 32, { 'leading': true })); _.times(2, _.debounce(function() { counts[2]++; }, 32, { 'leading': true }));
strictEqual(counts[2], 1); strictEqual(counts[2], 1);
_.forEach([false, { 'leading': false }], function(options) { _.forEach([false, { 'leading': false }], function(options) {
var withoutLeading = _.debounce(_.identity, 32, options); var withoutLeading = _.debounce(_.identity, 32, options);
strictEqual(withoutLeading('x'), undefined); strictEqual(withoutLeading('x'), undefined);
}); });
setTimeout(function() { setTimeout(function() {
deepEqual(counts, [1, 1, 2]); deepEqual(counts, [1, 1, 2]);
withLeadingAndTrailing('x'); withLeadingAndTrailing('x');
equal(counts[1], 2); equal(counts[1], 2);
QUnit.start();
}, 64);
}
else {
skipTest(5);
QUnit.start(); QUnit.start();
}, 64); }
}); });
asyncTest('should work with `trailing` option', function() { asyncTest('should work with `trailing` option', function() {
var withCount = 0, if (!(isRhino && isModularize)) {
withoutCount = 0; var withCount = 0,
withoutCount = 0;
var withTrailing = _.debounce(function(value) { var withTrailing = _.debounce(function(value) {
withCount++; withCount++;
return value; return value;
}, 32, { 'trailing': true }); }, 32, { 'trailing': true });
var withoutTrailing = _.debounce(function(value) { var withoutTrailing = _.debounce(function(value) {
withoutCount++; withoutCount++;
return value; return value;
}, 32, { 'trailing': false }); }, 32, { 'trailing': false });
strictEqual(withTrailing('x'), undefined); strictEqual(withTrailing('x'), undefined);
strictEqual(withoutTrailing('x'), undefined); strictEqual(withoutTrailing('x'), undefined);
setTimeout(function() { setTimeout(function() {
strictEqual(withCount, 1); strictEqual(withCount, 1);
strictEqual(withoutCount, 0); strictEqual(withoutCount, 0);
QUnit.start();
}, 64);
}
else {
skipTest(4);
QUnit.start(); QUnit.start();
}, 64); }
}); });
asyncTest('should work with `maxWait` option', function() { asyncTest('should work with `maxWait` option', function() {
var limit = 100, if (!(isRhino && isModularize)) {
withCount = 0, var limit = 100,
withoutCount = 0; withCount = 0,
withoutCount = 0;
var withMaxWait = _.debounce(function() { var withMaxWait = _.debounce(function() {
withCount++; withCount++;
}, 32, { 'maxWait': 64 }); }, 32, { 'maxWait': 64 });
var withoutMaxWait = _.debounce(function() { var withoutMaxWait = _.debounce(function() {
withoutCount++; withoutCount++;
}, 32); }, 32);
var start = new Date; var start = new Date;
while ((new Date - start) < limit) { while ((new Date - start) < limit) {
withMaxWait(); withMaxWait();
withoutMaxWait(); withoutMaxWait();
}
strictEqual(withCount, 1);
strictEqual(withoutCount, 0);
var lastWithCount = withCount,
lastWithoutCount = withoutCount;
setTimeout(function() {
ok(withCount > lastWithCount);
ok(withoutCount > lastWithoutCount && withoutCount < withCount);
QUnit.start();
}, 64);
} }
strictEqual(withCount, 1); else {
strictEqual(withoutCount, 0); skipTest(4);
var lastWithCount = withCount,
lastWithoutCount = withoutCount;
setTimeout(function() {
ok(withCount > lastWithCount);
ok(withoutCount > lastWithoutCount && withoutCount < withCount);
QUnit.start(); QUnit.start();
}, 64); }
}); });
}()); }());
@@ -974,10 +1012,15 @@
(function() { (function() {
asyncTest('should accept additional arguments', function() { asyncTest('should accept additional arguments', function() {
_.defer(function() { if (!(isRhino && isModularize)) {
deepEqual(slice.call(arguments), [1, 2, 3]); _.defer(function() {
deepEqual(slice.call(arguments), [1, 2, 3]);
QUnit.start();
}, 1, 2, 3);
} else {
skipTest();
QUnit.start(); QUnit.start();
}, 1, 2, 3); }
}); });
}()); }());
@@ -3809,10 +3852,15 @@
(function() { (function() {
test('subsequent calls should return the result of the first call', function() { test('subsequent calls should return the result of the first call', function() {
var throttled = _.throttle(function(value) { return value; }, 32), if (!(isRhino && isModularize)) {
result = [throttled('x'), throttled('y')]; var throttled = _.throttle(function(value) { return value; }, 32),
result = [throttled('x'), throttled('y')];
deepEqual(result, ['x', 'x']); deepEqual(result, ['x', 'x']);
}
else {
skipTest();
}
}); });
test('should clear timeout when `func` is called', function() { test('should clear timeout when `func` is called', function() {
@@ -3842,127 +3890,168 @@
}); });
asyncTest('supports recursive calls', function() { asyncTest('supports recursive calls', function() {
var count = 0; if (!(isRhino && isModularize)) {
var throttled = _.throttle(function() { var count = 0;
count++; var throttled = _.throttle(function() {
if (count < 10) { count++;
throttled(); if (count < 10) {
} throttled();
}, 32); }
}, 32);
throttled(); throttled();
equal(count, 1); equal(count, 1);
setTimeout(function() { setTimeout(function() {
ok(count < 3) ok(count < 3)
QUnit.start();
}, 32);
}
else {
skipTest(2);
QUnit.start(); QUnit.start();
}, 32); }
}); });
asyncTest('should not trigger a trailing call when invoked once', function() { asyncTest('should not trigger a trailing call when invoked once', function() {
var count = 0, if (!(isRhino && isModularize)) {
throttled = _.throttle(function() { count++; }, 32); var count = 0,
throttled = _.throttle(function() { count++; }, 32);
throttled(); throttled();
equal(count, 1);
setTimeout(function() {
equal(count, 1); equal(count, 1);
setTimeout(function() {
equal(count, 1);
QUnit.start();
}, 96);
}
else {
skipTest(2);
QUnit.start(); QUnit.start();
}, 96); }
}); });
_.times(2, function(index) { _.times(2, function(index) {
asyncTest('should trigger trailing call when invoked repeatedly' + (index ? ' and `leading` is `false`' : '') , function() { asyncTest('should trigger trailing call when invoked repeatedly' + (index ? ' and `leading` is `false`' : '') , function() {
var count = 0, if (!(isRhino && isModularize)) {
limit = 160, var count = 0,
options = index ? { 'leading': false } : {}, limit = 160,
throttled = _.throttle(function() { count++; }, 64, options), options = index ? { 'leading': false } : {},
start = new Date; throttled = _.throttle(function() { count++; }, 64, options),
start = new Date;
while ((new Date - start) < limit) { while ((new Date - start) < limit) {
throttled(); throttled();
}
var lastCount = count;
ok(count > 1);
setTimeout(function() {
ok(count > lastCount);
QUnit.start();
}, 96);
} }
var lastCount = count; else {
ok(count > 1); skipTest(2);
setTimeout(function() {
ok(count > lastCount);
QUnit.start(); QUnit.start();
}, 96); }
}); });
}); });
asyncTest('should apply default options correctly', function() { asyncTest('should apply default options correctly', function() {
var count = 0; if (!(isRhino && isModularize)) {
var count = 0;
var throttled = _.throttle(function(value) { var throttled = _.throttle(function(value) {
count++; count++;
return value; return value;
}, 32, {}); }, 32, {});
_.times(2, function() { _.times(2, function() {
equal(throttled('x'), 'x'); equal(throttled('x'), 'x');
}); });
setTimeout(function() { setTimeout(function() {
strictEqual(count, 2); strictEqual(count, 2);
QUnit.start();
}, 64);
}
else {
skipTest(3);
QUnit.start(); QUnit.start();
}, 64); }
}); });
test('should work with `leading` option', function() { test('should work with `leading` option', function() {
_.forEach([true, { 'leading': true }], function(options) { if (!(isRhino && isModularize)) {
var withLeading = _.throttle(_.identity, 32, options); _.forEach([true, { 'leading': true }], function(options) {
equal(withLeading('x'), 'x'); var withLeading = _.throttle(_.identity, 32, options);
}); equal(withLeading('x'), 'x');
});
_.forEach([false, { 'leading': false }], function(options) { _.forEach([false, { 'leading': false }], function(options) {
var withoutLeading = _.throttle(_.identity, 32, options); var withoutLeading = _.throttle(_.identity, 32, options);
strictEqual(withoutLeading('x'), undefined); strictEqual(withoutLeading('x'), undefined);
}); });
}
else {
skipTest(4);
}
}); });
asyncTest('should work with `trailing` option', function() { asyncTest('should work with `trailing` option', function() {
var withCount = 0, if (!(isRhino && isModularize)) {
withoutCount = 0; var withCount = 0,
withoutCount = 0;
var withTrailing = _.throttle(function(value) { var withTrailing = _.throttle(function(value) {
withCount++; withCount++;
return value; return value;
}, 32, { 'trailing': true }); }, 32, { 'trailing': true });
var withoutTrailing = _.throttle(function(value) { var withoutTrailing = _.throttle(function(value) {
withoutCount++; withoutCount++;
return value; return value;
}, 32, { 'trailing': false }); }, 32, { 'trailing': false });
_.times(2, function() { _.times(2, function() {
equal(withTrailing('x'), 'x'); equal(withTrailing('x'), 'x');
equal(withoutTrailing('x'), 'x'); equal(withoutTrailing('x'), 'x');
}); });
setTimeout(function() { setTimeout(function() {
equal(withCount, 2); equal(withCount, 2);
strictEqual(withoutCount, 1); strictEqual(withoutCount, 1);
QUnit.start();
}, 64);
}
else {
skipTest(6);
QUnit.start(); QUnit.start();
}, 64); }
}); });
asyncTest('should not update `lastCalled`, at the end of the timeout, when `trailing` is `false`', function() { asyncTest('should not update `lastCalled`, at the end of the timeout, when `trailing` is `false`', function() {
var count = 0; if (!(isRhino && isModularize)) {
var count = 0;
var throttled = _.throttle(function() { var throttled = _.throttle(function() {
count++; count++;
}, 64, { 'trailing': false }); }, 64, { 'trailing': false });
_.times(2, throttled); _.times(2, throttled);
setTimeout(function() { _.times(2, throttled); }, 100); setTimeout(function() { _.times(2, throttled); }, 100);
setTimeout(function() { setTimeout(function() {
equal(count, 2); equal(count, 2);
QUnit.start();
}, 128);
}
else {
skipTest();
QUnit.start(); QUnit.start();
}, 128); }
}); });
}()); }());