Enable testing fp/convert in the browser.

This commit is contained in:
John-David Dalton
2016-02-08 11:41:39 -08:00
parent 285b667c3e
commit e36f7e7161

View File

@@ -44,10 +44,21 @@
QUnitExtras.runInContext(root); QUnitExtras.runInContext(root);
} }
var convert = root.fp || (function() { var convert = (function() {
var baseConvert = load('../fp/_baseConvert.js'); var baseConvert = root.fp || load('../fp/_baseConvert.js');
if (!root.fp) {
return function(name, func, options) {
return baseConvert(_, name, func, options);
};
}
return function(name, func, options) { return function(name, func, options) {
return baseConvert(_, name, func, options); if (typeof func != 'function') {
func = name;
name = undefined;
}
return name === undefined
? baseConvert(func, options)
: baseConvert(_.runInContext(), options)[name];
}; };
}()); }());
@@ -82,90 +93,70 @@
QUnit.test('should accept an `options` argument', function(assert) { QUnit.test('should accept an `options` argument', function(assert) {
assert.expect(3); assert.expect(3);
if (!document) { var array = [1, 2, 3, 4];
var remove = convert('remove', _.remove, {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
});
var array = [1, 2, 3, 4]; var remove = convert('remove', _.remove, {
'cap': false,
'curry': false,
'fixed': false,
'immutable': false,
'rearg': false
});
var actual = remove(array, function(n) { var actual = remove(array, function(n, index) {
return n % 2 == 0; return index % 2 == 0;
}); });
assert.deepEqual(array, [1, 3]); assert.deepEqual(array, [2, 4]);
assert.deepEqual(actual, [2, 4]); assert.deepEqual(actual, [1, 3]);
assert.deepEqual(remove(), []); assert.deepEqual(remove(), []);
}
else {
skipTest(assert, 3);
}
}); });
QUnit.test('should respect the `cap` option', function(assert) { QUnit.test('should respect the `cap` option', function(assert) {
assert.expect(1); assert.expect(1);
if (!document) { var iteratee = convert('iteratee', _.iteratee, {
var iteratee = convert('iteratee', _.iteratee, { 'cap': false
'cap': false });
});
var func = iteratee(function(a, b, c) { var func = iteratee(function(a, b, c) {
return [a, b, c]; return [a, b, c];
}, 3); }, 3);
assert.deepEqual(func(1, 2, 3), [1, 2, 3]); assert.deepEqual(func(1, 2, 3), [1, 2, 3]);
}
else {
skipTest(assert);
}
}); });
QUnit.test('should respect the `rearg` option', function(assert) { QUnit.test('should respect the `rearg` option', function(assert) {
assert.expect(1); assert.expect(1);
if (!document) { var add = convert('add', _.add, {
var add = convert('add', _.add, { 'rearg': true
'rearg': true });
});
assert.strictEqual(add('2')('1'), '12'); assert.strictEqual(add('2')('1'), '12');
}
else {
skipTest(assert);
}
}); });
QUnit.test('should use `options` in `runInContext`', function(assert) { QUnit.test('should use `options` in `runInContext`', function(assert) {
assert.expect(3); assert.expect(3);
if (!document) { var runInContext = convert('runInContext', _.runInContext, {
var runInContext = convert('runInContext', _.runInContext, { 'cap': false,
'cap': false, 'curry': false,
'curry': false, 'fixed': false,
'fixed': false, 'immutable': false,
'immutable': false, 'rearg': false
'rearg': false });
});
var array = [1, 2, 3, 4], var array = [1, 2, 3, 4],
lodash = runInContext(); lodash = runInContext();
var actual = lodash.remove(array, function(n) { var actual = lodash.remove(array, function(n) {
return n % 2 == 0; return n % 2 == 0;
}); });
assert.deepEqual(array, [1, 3]); assert.deepEqual(array, [1, 3]);
assert.deepEqual(actual, [2, 4]); assert.deepEqual(actual, [2, 4]);
assert.deepEqual(lodash.remove(), []); assert.deepEqual(lodash.remove(), []);
}
else {
skipTest(assert, 3);
}
}); });
}()); }());
@@ -356,81 +347,71 @@
QUnit.test('should provide the correct `iteratee` arguments', function(assert) { QUnit.test('should provide the correct `iteratee` arguments', function(assert) {
assert.expect(4); assert.expect(4);
if (!document) { var args,
var args, array = [1, 2, 3],
array = [1, 2, 3], object = { 'a': 1, 'b': 2 },
object = { 'a': 1, 'b': 2 }, isFIFO = _.keys(object)[0] == 'a',
isFIFO = _.keys(object)[0] == 'a', map = convert('map', _.map),
map = convert('map', _.map), reduce = convert('reduce', _.reduce);
reduce = convert('reduce', _.reduce);
map(function() { map(function() {
args || (args = slice.call(arguments)); args || (args = slice.call(arguments));
})(array); })(array);
assert.deepEqual(args, [1]); assert.deepEqual(args, [1]);
args = undefined; args = undefined;
map(function() { map(function() {
args || (args = slice.call(arguments)); args || (args = slice.call(arguments));
})(object); })(object);
assert.deepEqual(args, isFIFO ? [1] : [2]); assert.deepEqual(args, isFIFO ? [1] : [2]);
args = undefined; args = undefined;
reduce(function() { reduce(function() {
args || (args = slice.call(arguments)); args || (args = slice.call(arguments));
})(0, array); })(0, array);
assert.deepEqual(args, [0, 1]); assert.deepEqual(args, [0, 1]);
args = undefined; args = undefined;
reduce(function() { reduce(function() {
args || (args = slice.call(arguments)); args || (args = slice.call(arguments));
})(0, object); })(0, object);
assert.deepEqual(args, isFIFO ? [0, 1] : [0, 2]); assert.deepEqual(args, isFIFO ? [0, 1] : [0, 2]);
}
else {
skipTest(assert, 4);
}
}); });
QUnit.test('should not support shortcut fusion', function(assert) { QUnit.test('should not support shortcut fusion', function(assert) {
assert.expect(3); assert.expect(3);
if (!document) { var array = fp.range(0, LARGE_ARRAY_SIZE),
var array = fp.range(0, LARGE_ARRAY_SIZE), filterCount = 0,
filterCount = 0, mapCount = 0;
mapCount = 0;
var iteratee = function(value) { var iteratee = function(value) {
mapCount++; mapCount++;
return value * value; return value * value;
}; };
var predicate = function(value) { var predicate = function(value) {
filterCount++; filterCount++;
return value % 2 == 0; return value % 2 == 0;
}; };
var map1 = convert('map', _.map), var map1 = convert('map', _.map),
filter1 = convert('filter', _.filter), filter1 = convert('filter', _.filter),
take1 = convert('take', _.take); take1 = convert('take', _.take);
var filter2 = filter1(predicate), var filter2 = filter1(predicate),
map2 = map1(iteratee), map2 = map1(iteratee),
take2 = take1(2); take2 = take1(2);
var combined = fp.flow(map2, filter2, fp.compact, take2); var combined = fp.flow(map2, filter2, fp.compact, take2);
assert.deepEqual(combined(array), [4, 16]); assert.deepEqual(combined(array), [4, 16]);
assert.strictEqual(filterCount, 200, 'filterCount'); assert.strictEqual(filterCount, 200, 'filterCount');
assert.strictEqual(mapCount, 200, 'mapCount'); assert.strictEqual(mapCount, 200, 'mapCount');
}
else {
skipTest(assert, 3);
}
}); });
}()); }());
@@ -757,17 +738,12 @@
var object = { 'a': 1 }; var object = { 'a': 1 };
if (!document) { var extend = convert('extend', _.extend),
var extend = convert('extend', _.extend), value = _.clone(object),
value = _.clone(object), actual = extend(value, new Foo);
actual = extend(value, new Foo);
assert.deepEqual(value, object); assert.deepEqual(value, object);
assert.deepEqual(actual, { 'a': 1, 'b': 2 }); assert.deepEqual(actual, { 'a': 1, 'b': 2 });
}
else {
skipTest(assert, 2);
}
}); });
}()); }());
@@ -883,15 +859,10 @@
QUnit.test('should convert by name', function(assert) { QUnit.test('should convert by name', function(assert) {
assert.expect(1); assert.expect(1);
if (!document) { var iteratee = convert('iteratee', _.iteratee),
var iteratee = convert('iteratee', _.iteratee), func = iteratee(function(a, b, c) { return [a, b, c]; }, undefined, 3);
func = iteratee(function(a, b, c) { return [a, b, c]; }, undefined, 3);
assert.deepEqual(func(1, 2, 3), [1, undefined, undefined]); assert.deepEqual(func(1, 2, 3), [1, undefined, undefined]);
}
else {
skipTest(assert);
}
}); });
}()); }());
@@ -1012,22 +983,17 @@
QUnit.test('should convert by name', function(assert) { QUnit.test('should convert by name', function(assert) {
assert.expect(3); assert.expect(3);
if (!document) { var object = { 'mixin': convert('mixin', _.mixin) };
var object = { 'mixin': convert('mixin', _.mixin) };
function Foo() {} function Foo() {}
Foo.mixin = object.mixin; Foo.mixin = object.mixin;
Foo.mixin(source); Foo.mixin(source);
assert.strictEqual(typeof Foo.a, 'function'); assert.strictEqual(typeof Foo.a, 'function');
assert.notOk('a' in Foo.prototype); assert.notOk('a' in Foo.prototype);
object.mixin(source); object.mixin(source);
assert.strictEqual(typeof object.a, 'function'); assert.strictEqual(typeof object.a, 'function');
}
else {
skipTest(assert, 3);
}
}); });
}()); }());
@@ -1054,19 +1020,14 @@
QUnit.test('`_.' + methodName + '` should convert by name', function(assert) { QUnit.test('`_.' + methodName + '` should convert by name', function(assert) {
assert.expect(1); assert.expect(1);
if (!document) { var expected = isPartial ? [1, 2, 3] : [0, 1, 2],
var expected = isPartial ? [1, 2, 3] : [0, 1, 2], par = convert(methodName, _[methodName]);
par = convert(methodName, _[methodName]);
var actual = par(function(a, b, c) { var actual = par(function(a, b, c) {
return [a, b, c]; return [a, b, c];
})([1, 2])(isPartial ? 3 : 0); })([1, 2])(isPartial ? 3 : 0);
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}
else {
skipTest(assert);
}
}); });
}); });
@@ -1156,13 +1117,8 @@
QUnit.test('should convert by name', function(assert) { QUnit.test('should convert by name', function(assert) {
assert.expect(1); assert.expect(1);
if (!document) { var runInContext = convert('runInContext', _.runInContext);
var runInContext = convert('runInContext', _.runInContext); assert.strictEqual(typeof runInContext({}).curryN, 'function');
assert.strictEqual(typeof runInContext({}).curryN, 'function');
}
else {
skipTest(assert);
}
}); });
}()); }());