Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Throw an error for non-existing behaviors.

Summary:
This is very defensive. This throws in prod and it can be
caught outside of initBehaviors if one or many behaviors are not
available. If it is an unimportant behavior and if there is no attempt
to catch it on the outside, this can likely be ignored by library users
if initBehaviors is called in its own execution context.

Test Plan: Specs!

Reviewers: tomo, mroch, epriestley, jg

Reviewed By: epriestley

CC: aran, epriestley

Differential Revision: 836
  • Loading branch information...
commit 0cdaf85a58c6c29545b1b7712d33f73911838b8d 1 parent 757fb81
@cpojer authored
Showing with 27 additions and 6 deletions.
  1. +17 −0 src/lib/__tests__/behavior.js
  2. +10 −6 src/lib/behavior.js
View
17 src/lib/__tests__/behavior.js
@@ -75,5 +75,22 @@ describe('Javelin Behaviors', function() {
expect(asserted).toBe(2);
});
+ it('should throw for undefined behaviors', function() {
+ var called;
+ JX.behavior('can-haz', function() {
+ called = true;
+ });
+
+ expect(function() {
+ JX.initBehaviors({
+ 'no-can-haz': [],
+ 'can-haz': [],
+ 'i-fail': []
+ });
+ }).toThrow();
+
+ expect(called).toBe(true);
+ });
+
});
View
16 src/lib/behavior.js
@@ -74,13 +74,11 @@ JX.behavior = function(name, control_function) {
* @group behavior
*/
JX.initBehaviors = function(map) {
+ var missing_behaviors = [];
for (var name in map) {
- if (__DEV__) {
- if (!(name in JX.behavior._behaviors)) {
- throw new Error(
- 'JX.initBehavior({"' + name + '" : ...}): ' +
- 'behavior is not registered.');
- }
+ if (!(name in JX.behavior._behaviors)) {
+ missing_behaviors.push(name);
+ continue;
}
var configs = map[name];
if (!configs.length) {
@@ -94,6 +92,12 @@ JX.initBehaviors = function(map) {
}
JX.behavior._initialized[name] = true;
}
+ if (missing_behaviors.length) {
+ throw new Error(
+ 'JX.initBehavior(map): behavior(s) not registered: ' +
+ missing_behaviors.join(', ')
+ );
+ }
};
JX.behavior._behaviors = {};
Please sign in to comment.
Something went wrong with that request. Please try again.