From ae92aa200f4eae87c9d32456f55a284c0e2a2208 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 14 Dec 2015 20:37:05 -0800 Subject: [PATCH] Add fp tests for cherry-picked `reduce` and `reduce` iterating an object. --- test/test-fp.js | 62 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/test/test-fp.js b/test/test-fp.js index a8f4b9fb5..75717e7dc 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -218,21 +218,45 @@ (function() { QUnit.test('should provide the correct `iteratee` arguments', function(assert) { - assert.expect(1); + assert.expect(4); if (!document) { var args, array = [1, 2, 3], - map = convert('map', _.map); + object = { 'a': 1, 'b': 2 }, + isFIFO = _.keys(object)[0] == 'a', + map = convert('map', _.map), + reduce = convert('reduce', _.reduce); map(function() { args || (args = slice.call(arguments)); })(array); assert.deepEqual(args, [1]); + + args = undefined; + map(function() { + args || (args = slice.call(arguments)); + })(object); + + assert.deepEqual(args, isFIFO ? [1] : [2]); + + args = undefined; + reduce(function() { + args || (args = slice.call(arguments)); + })(0, array); + + assert.deepEqual(args, [0, 1]); + + args = undefined; + reduce(function() { + args || (args = slice.call(arguments)); + })(0, object); + + assert.deepEqual(args, isFIFO ? [0, 1] : [0, 2]); } else { - skipTest(assert); + skipTest(assert, 4); } }); @@ -571,13 +595,13 @@ _.each(['reduce', 'reduceRight'], function(methodName) { var func = fp[methodName], - array = [1, 2, 3], isReduce = methodName == 'reduce'; - QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments', function(assert) { + QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments when iterating an array', function(assert) { assert.expect(1); - var args; + var args, + array = [1, 2, 3]; func(function() { args || (args = slice.call(arguments)); @@ -585,6 +609,24 @@ assert.deepEqual(args, isReduce ? [0, 1] : [0, 3]); }); + + QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments when iterating an object', function(assert) { + assert.expect(1); + + var args, + object = { 'a': 1, 'b': 2 }, + isFIFO = _.keys(object)[0] == 'a'; + + var expected = isFIFO + ? (isReduce ? [0, 1] : [0, 2]) + : (isReduce ? [0, 2] : [0, 1]); + + func(function() { + args || (args = slice.call(arguments)); + })(0, object); + + assert.deepEqual(args, expected); + }); }); /*--------------------------------------------------------------------------*/ @@ -812,9 +854,8 @@ assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.assignWith'); - args = null; + args = undefined; value = _.clone(object); - actual = fp.extendWith(function(objValue, srcValue) { args || (args = _.map(arguments, _.cloneDeep)); return srcValue; @@ -822,12 +863,11 @@ assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.extendWith'); - args = null; - value = { 'a': [1] }; - var stack = { '__data__': { 'array': [], 'map': null } }, expected = [[1], [2, 3], 'a', { 'a': [ 1 ] }, { 'a': [2, 3] }, stack]; + args = undefined; + value = { 'a': [1] }; actual = fp.mergeWith(function(objValue, srcValue) { args || (args = _.map(arguments, _.cloneDeep)); if (_.isArray(objValue)) {