From b6e016eb09b7de037013547dd851ddb3f98943a3 Mon Sep 17 00:00:00 2001 From: bekzod Date: Thu, 5 Oct 2017 23:43:32 +0500 Subject: [PATCH] fix `canInvoke` for edge cases --- packages/ember-utils/lib/invoke.js | 2 +- packages/ember-utils/tests/can_invoke_test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/ember-utils/lib/invoke.js b/packages/ember-utils/lib/invoke.js index e708b81d854..688be948b24 100644 --- a/packages/ember-utils/lib/invoke.js +++ b/packages/ember-utils/lib/invoke.js @@ -19,7 +19,7 @@ import applyStr from './apply-str'; @private */ export function canInvoke(obj, methodName) { - return !!(obj && typeof obj[methodName] === 'function'); + return obj !== null && obj !== undefined && typeof obj[methodName] === 'function'; } /** diff --git a/packages/ember-utils/tests/can_invoke_test.js b/packages/ember-utils/tests/can_invoke_test.js index 5dc7ba9be49..a3974a1c99d 100644 --- a/packages/ember-utils/tests/can_invoke_test.js +++ b/packages/ember-utils/tests/can_invoke_test.js @@ -19,6 +19,12 @@ QUnit.test('should return false if the object doesn\'t exist', function() { equal(canInvoke(undefined, 'aMethodThatDoesNotExist'), false); }); +QUnit.test('should return true for falsy values that have methods', function() { + equal(canInvoke(false, 'valueOf'), true); + equal(canInvoke('', 'charAt'), true); + equal(canInvoke(0, 'toFixed'), true); +}); + QUnit.test('should return true if the method exists on the object', function() { equal(canInvoke(obj, 'aMethodThatExists'), true); });