Skip to content

Commit

Permalink
Changes option throwValidationError to a method shouldThrowValidation…
Browse files Browse the repository at this point in the history
…Error()

This is required to be on class definition to work on values received on constructor
  • Loading branch information
eduardolundgren committed Mar 3, 2017
1 parent 4e5bebe commit 9b55607
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
29 changes: 6 additions & 23 deletions packages/metal-state/src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, !Object>}
Expand Down Expand Up @@ -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);
Expand All @@ -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}`);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand Down
57 changes: 45 additions & 12 deletions packages/metal-state/test/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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() {
Expand Down

0 comments on commit 9b55607

Please sign in to comment.