From 841890c178a3f5e7bebbfd5586b7eada3c5b17a3 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 15 Apr 2014 23:19:39 -0700 Subject: [PATCH] Add tests for `_.pluck`, `_.findWhere`, and `_.where` for their `key` or `source` arguments. --- test/test.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/test.js b/test/test.js index cee3d5bae..e8518d88f 100644 --- a/test/test.js +++ b/test/test.js @@ -2681,6 +2681,13 @@ strictEqual(_.findWhere(objects, { 'a': 1, 'b': 2 }), objects[2]); }); + test('should work with a function for `source`', 1, function() { + function source() {} + source.a = 2; + + strictEqual(_.findWhere(objects, source), objects[3]); + }); + test('should match all elements when provided an empty `source`', 1, function() { var expected = _.map(empties, _.constant(true)); @@ -7005,6 +7012,20 @@ var objects = [{ 'a': 1 }, null, undefined, { 'a': 4 }]; deepEqual(_.pluck(objects, 'a'), [1, undefined, undefined, 4]); }); + + test('should coerce `key` to a string', 1, function() { + function fn() {} + fn.toString = _.constant('fn'); + + var objects = [{ 'null': 1 }, { 'undefined': 2 }, { 'fn': 3 }, { '[object Object]': 4 }], + values = [null, undefined, fn, {}] + + var actual = _.map(objects, function(object, index) { + return _.pluck([object], values[index]); + }); + + deepEqual(actual, [[1], [2], [3], [4]]); + }); }()); /*--------------------------------------------------------------------------*/ @@ -9583,6 +9604,7 @@ var source = new Foo; source.b = 2; + deepEqual(_.where(objects, source), [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }]); }); @@ -9601,6 +9623,13 @@ deepEqual(_.where(collection, { 'a': 1 }), [{ 'a': 1 }, { 'a': 1, 'b': 2 }]); }); + test('should work with a function for `source`', 1, function() { + function source() {} + source.a = 2; + + deepEqual(_.where(objects, source), [{ 'a': 2, 'b': 2 }]); + }); + test('should match all elements when provided an empty `source`', 1, function() { var expected = _.map(empties, _.constant(objects));