From 670abd924f73b39cb26071545fe966e8690c6646 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Tue, 17 Sep 2013 12:25:29 -0400 Subject: [PATCH] Reqiure callbacks to be functions for event binding --- src/event/CallbackRegistry.js | 8 +++- src/event/__tests__/CallbackRegistry-test.js | 40 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/event/__tests__/CallbackRegistry-test.js diff --git a/src/event/CallbackRegistry.js b/src/event/CallbackRegistry.js index 04e83ec1f30..32a5d4c8f64 100644 --- a/src/event/CallbackRegistry.js +++ b/src/event/CallbackRegistry.js @@ -20,6 +20,7 @@ "use strict"; var listenerBank = {}; +var invariant = require('invariant'); /** * Stores "listeners" by `registrationName`/`id`. There should be at most one @@ -40,6 +41,11 @@ var CallbackRegistry = { * @param {?function} listener The callback to store. */ putListener: function(id, registrationName, listener) { + invariant( + typeof listener === 'function', + 'Trying to bind an event handler to a non-function for %s', + registrationName + ); var bankForRegistrationName = listenerBank[registrationName] || (listenerBank[registrationName] = {}); bankForRegistrationName[id] = listener; @@ -88,4 +94,4 @@ var CallbackRegistry = { }; -module.exports = CallbackRegistry; +module.exports = CallbackRegistry; \ No newline at end of file diff --git a/src/event/__tests__/CallbackRegistry-test.js b/src/event/__tests__/CallbackRegistry-test.js new file mode 100644 index 00000000000..68b06ab503b --- /dev/null +++ b/src/event/__tests__/CallbackRegistry-test.js @@ -0,0 +1,40 @@ +/** + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @emails react-core + */ + +"use strict"; + +describe('CallbackRegistry', function() { + var CallbackRegistry; + + beforeEach(function() { + CallbackRegistry = require('CallbackRegistry'); + }); + + it('should bind to a function', function(){ + var noop = function(){}; + expect(function(){ + CallbackRegistry.putListener('test', 'onClick', noop); + }).not.toThrow(); + }); + + it('should throw when binding to a non-function', function(){ + expect(function(){ + CallbackRegistry.putListener('test', 'onClick', {}); + }).toThrow(); + }); +}); \ No newline at end of file