From 9b55607fea15e4769aa1a6165f21bdc4eae321d7 Mon Sep 17 00:00:00 2001 From: Eduardo Lundgren Date: Thu, 2 Mar 2017 16:13:52 -0800 Subject: [PATCH] Changes option throwValidationError to a method shouldThrowValidationError() This is required to be on class definition to work on values received on constructor --- packages/metal-state/src/State.js | 29 ++++----------- packages/metal-state/test/State.js | 57 +++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/packages/metal-state/src/State.js b/packages/metal-state/src/State.js index c7c6774e..2ab940e6 100644 --- a/packages/metal-state/src/State.js +++ b/packages/metal-state/src/State.js @@ -55,14 +55,6 @@ class State extends EventEmitter { */ this.scheduledBatchData_ = null; - /** - * Throws exception when validator returns an `Error` instance. - * @type {boolean} - * @default false - * @protected - */ - this.throwValidationError_ = false; - /** * Object that contains information about all this instance's state keys. * @type {!Object} @@ -98,7 +90,7 @@ class State extends EventEmitter { this.initialValues_[name]; if (!isDefAndNotNull(value)) { let errorMessage = `The property called "${name}" is required but didn't receive a value.`; - if (this.getThrowValidationError()) { + if (this.shouldThrowValidationError()) { throw new Error(errorMessage); } else { console.error(errorMessage); @@ -114,7 +106,7 @@ class State extends EventEmitter { */ assertValidatorReturnInstanceOfError_(validatorReturn) { if (validatorReturn instanceof Error) { - if (this.getThrowValidationError()) { + if (this.shouldThrowValidationError()) { throw validatorReturn; } else { console.error(`Warning: ${validatorReturn}`); @@ -391,15 +383,6 @@ class State extends EventEmitter { } } - /** - * Gets the configuration value for whether or not should throw error when - * vaildator functions returns an `Error` instance. - * @return {boolean} - */ - getThrowValidationError() { - return this.throwValidationError_; - } - /** * Merges the STATE static variable for the given constructor function. * @param {!Function} ctor Constructor function. @@ -640,12 +623,12 @@ class State extends EventEmitter { } /** - * Sets the configuration value for whether or not should throw error when + * Returns a boolean that determines whether or not should throw error when * vaildator functions returns an `Error` instance. - * @param {boolean} throwValidationError + * @return {boolean} By default returns false. */ - setThrowValidationError(throwValidationError) { - this.throwValidationError_ = throwValidationError; + shouldThrowValidationError() { + return false; } /** diff --git a/packages/metal-state/test/State.js b/packages/metal-state/test/State.js index d73737a5..f0480cd8 100644 --- a/packages/metal-state/test/State.js +++ b/packages/metal-state/test/State.js @@ -390,9 +390,13 @@ describe('State', function() { console.error = originalConsoleFn; }); - it('should throw error if validator returns an Error and throwValidationError is enabled', function() { - var state = new State(); - state.setThrowValidationError(true); + it('should throw error if validator returns an Error and shouldThrowValidationError is true', function() { + class Test extends State { + shouldThrowValidationError() { + return true; + } + } + var state = new Test(); state.configState( { key1: { @@ -406,9 +410,13 @@ describe('State', function() { assert.throws(() => state.key1 = 1); }); - it('should not throw error if validator returns an Error and throwValidationError is disabled', function() { - var state = new State(); - state.setThrowValidationError(false); + it('should not throw error if validator returns an Error and shouldThrowValidationError is false', function() { + class Test extends State { + shouldThrowValidationError() { + return false; + } + } + var state = new Test(); state.configState( { key1: { @@ -584,11 +592,15 @@ describe('State', function() { assert.strictEqual(2, console.error.callCount); }); - it('should throw error if required property gets no initial value via configState and throwValidationError is enabled', function() { - var state = new State({ + it('should throw error if required property gets no initial value via configState and shouldThrowValidationError is true', function() { + class Test extends State { + shouldThrowValidationError() { + return true; + } + } + var state = new Test({ key2: 'initialValue' }); - state.setThrowValidationError(true); assert.doesNotThrow(() => { state.configState({ @@ -615,11 +627,15 @@ describe('State', function() { }); }); - it('should log error if required property is set to null or undefined and throwValidationError is enabled', function() { - var state = new State({ + it('should throw error if required property is set to null or undefined and shouldThrowValidationError is true', function() { + class Test extends State { + shouldThrowValidationError() { + return true; + } + } + var state = new Test({ key: 'initialValue' }); - state.setThrowValidationError(true); assert.doesNotThrow(() => { state.configState({ @@ -641,6 +657,23 @@ describe('State', function() { state.key = undefined; }); }); + + it('should throw error if required property is set to null or undefined and shouldThrowValidationError is true', function() { + class Test extends State { + shouldThrowValidationError() { + return true; + } + } + Test.STATE = { + key: { + required: true + } + }; + + assert.throws(() => { + new Test(); + }); + }); }); it('should emit event when a state key\'s value changes', function() {