Skip to content

Commit

Permalink
Merge pull request #4 from jaebradley/fix-input-prop-definition
Browse files Browse the repository at this point in the history
fix(prop-structure): do not expect object prop definition
  • Loading branch information
jaebradley authored Dec 16, 2017
2 parents ff75129 + 0a8e0e4 commit 23aac37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 71 deletions.
9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ const phoneNumberUtil = libPhoneNumber.PhoneNumberUtil.getInstance();
const requiredPhoneNumberPropType = (props, propName, componentName) => {
const value = props[propName];

if (!value || !value.phoneNumber || !value.countryCode || typeof value.phoneNumber !== 'string' || typeof value.countryCode !== 'string') {
if (value == null || typeof value !== 'string') {
return new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`);
}

const {
phoneNumber,
countryCode,
} = value;

try {
phoneNumberUtil.parse(phoneNumber, countryCode);
phoneNumberUtil.parse(value);
} catch (e) {
return new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`);
}
Expand Down
75 changes: 11 additions & 64 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,29 @@ import phoneNumberPropType from './index';
describe('Phone Number Prop Type', () => {
const propName = 'baejadley';
const componentName = 'jaebaebae';
const phoneNumber = '555555555';
const phoneNumber = '+1555555555';
const invalidPhoneNumber = 'foobar';
const countryCode = 'US';
const nonStringValue = 10;


describe('phoneNumberPropType', () => {
it('should throw an error if phoneNumber value does not exist', () => {
const props = {};
const value = { countryCode };
props[propName] = value;
expect(phoneNumberPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
});

it('should throw an error if countryCode value does not exist', () => {
const props = {};
const value = { phoneNumber };
props[propName] = value;
expect(phoneNumberPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
});

it('should throw an error if countryCode value exists and is not a string', () => {
const props = {};
const value = { phoneNumber, countryCode: nonStringValue };
props[propName] = value;
expect(phoneNumberPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
});

it('should throw an error if phoneNumber value exists and is not a string', () => {
const props = {};
const value = { phoneNumber: nonStringValue, countryCode };
props[propName] = value;
props[propName] = nonStringValue;
expect(phoneNumberPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${nonStringValue} for ${propName} in ${componentName}`));
});

it('should throw an error if phoneNumber value is an invalid string', () => {
const props = {};
const value = { phoneNumber: invalidPhoneNumber, countryCode };
props[propName] = value;
props[propName] = invalidPhoneNumber;
expect(phoneNumberPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${invalidPhoneNumber} for ${propName} in ${componentName}`));
});

it('should return null if prop value is a valid phone number', () => {
const props = {};
props[propName] = { phoneNumber, countryCode };
props[propName] = phoneNumber;
expect(phoneNumberPropType(props, propName, componentName)).toBeNull();
});

Expand All @@ -63,44 +36,18 @@ describe('Phone Number Prop Type', () => {
});

describe('validateRequiredPhoneNumber', () => {
it('should throw an error if phoneNumber value does not exist', () => {
const props = {};
const value = { countryCode };
props[propName] = value;
expect(phoneNumberPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
});

it('should throw an error if countryCode value does not exist', () => {
const props = {};
const value = { phoneNumber };
props[propName] = value;
expect(phoneNumberPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
});

it('should throw an error if countryCode value exists and is not a string', () => {
const props = {};
const value = { phoneNumber, countryCode: nonStringValue };
props[propName] = value;
expect(phoneNumberPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
});

it('should throw an error if phoneNumber value exists and is not a string', () => {
const props = {};
const value = { phoneNumber: nonStringValue, countryCode };
props[propName] = value;
props[propName] = nonStringValue;
expect(phoneNumberPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${nonStringValue} for ${propName} in ${componentName}`));
});

it('should throw an error if phoneNumber value is an invalid string', () => {
const props = {};
const value = { phoneNumber: invalidPhoneNumber, countryCode };
props[propName] = value;
props[propName] = invalidPhoneNumber;
expect(phoneNumberPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${value} for ${propName} in ${componentName}`));
.toEqual(new TypeError(`Invalid Phone Number Prop Value: ${invalidPhoneNumber} for ${propName} in ${componentName}`));
});

it('should throw an error if prop is not defined', () => {
Expand All @@ -111,7 +58,7 @@ describe('Phone Number Prop Type', () => {

it('should return null if prop value is a valid phone number', () => {
const props = {};
props[propName] = { phoneNumber, countryCode };
props[propName] = phoneNumber;
expect(phoneNumberPropType.isRequired(props, propName, componentName)).toBeNull();
});
});
Expand Down

0 comments on commit 23aac37

Please sign in to comment.