Skip to content

Commit

Permalink
Merge pull request #3 from jaebradley/return-error
Browse files Browse the repository at this point in the history
fix(error): return error instead of throwing
  • Loading branch information
jaebradley committed Dec 2, 2017
2 parents c4fc25c + 6112b5c commit f7abf37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import isEmail from 'isemail';

const emailPropType = (props, propName, componentName) => {
const value = props[propName];

if (value == null || isEmail.validate(value)) {
return null;
}

throw new TypeError(`Invalid Email Prop Value: ${value} for ${propName} in ${componentName}`);
return new TypeError(`Invalid Email Prop Value: ${value} for ${propName} in ${componentName}`);
};

const requiredEmailPropType = (props, propName, componentName) => {
Expand All @@ -16,7 +17,7 @@ const requiredEmailPropType = (props, propName, componentName) => {
return null;
}

throw new TypeError(`Invalid Email Prop Value: ${value} for ${propName} in ${componentName}`);
return new TypeError(`Invalid Email Prop Value: ${value} for ${propName} in ${componentName}`);
};

emailPropType.isRequired = requiredEmailPropType;
Expand Down
33 changes: 6 additions & 27 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ describe('Email Prop Type Validation', () => {
const props = {};
props[propName] = email;

expect(() => emailPropType(props, propName, componentName))
.toThrow(`Invalid Email Prop Value: ${email} for ${propName} in ${componentName}`);
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);

isEmail.validate.mockClear();

expect(() => emailPropType(props, propName, componentName))
.toThrow(TypeError);
expect(emailPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: ${email} for ${propName} in ${componentName}`));
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);
});
Expand Down Expand Up @@ -55,30 +48,16 @@ describe('Email Prop Type Validation', () => {
const props = {};
props[propName] = email;

expect(() => emailPropType.isRequired(props, propName, componentName))
.toThrow(`Invalid Email Prop Value: ${email} for ${propName} in ${componentName}`);
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);

isEmail.validate.mockClear();

expect(() => emailPropType.isRequired(props, propName, componentName))
.toThrow(TypeError);
expect(emailPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: ${email} for ${propName} in ${componentName}`));
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);
});

it('should throw an error if prop is not defined', () => {
const props = {};
expect(() => emailPropType.isRequired(props, propName, componentName))
.toThrow(`Invalid Email Prop Value: undefined for ${propName} in ${componentName}`);
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(undefined);

isEmail.validate.mockClear();

expect(() => emailPropType.isRequired(props, propName, componentName))
.toThrow(TypeError);
expect(emailPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: undefined for ${propName} in ${componentName}`));
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(undefined);
});
Expand Down

0 comments on commit f7abf37

Please sign in to comment.