Skip to content

Commit

Permalink
More phone number test cases (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuykendall committed Jan 29, 2020
1 parent 8728249 commit 0ed5043
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/formatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function formatPhoneNumber (input?: string | null) {
if (!hasStringContent(input)) { return EMPTY_FIELD; }

const phoneNumbers = input.match(/\d/g) || []
, phoneNotNumbers = input.match(/[^0-9\-()]/g) || []
, phoneNotNumbers = input.match(/[^0-9\-(). ]/g) || []
, PHONE_FORMATS: string[] = [
'###-####',
'(###) ###-####',
Expand Down
26 changes: 22 additions & 4 deletions test/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,35 @@ describe('formatting', () => {
});

it('formatPhoneNumber', () => {
// Nonsense should just return
expect(util.formatPhoneNumber('+2111 (555)-867-5309')).toBe('+2111 (555)-867-5309');
expect(util.formatPhoneNumber('-------')).toBe('-------');
expect(util.formatPhoneNumber('5')).toBe('5');
expect(util.formatPhoneNumber('867-5309 ext. 4')).toBe('867-5309 ext. 4');
expect(util.formatPhoneNumber('1867-5309')).toBe('1867-5309');
expect(util.formatPhoneNumber('86753098675309867530')).toBe('86753098675309867530');
expect(util.formatPhoneNumber('Call main office')).toBe('Call main office');

// Digits
expect(util.formatPhoneNumber('5309')).toBe('5309');
expect(util.formatPhoneNumber('8675309')).toBe('867-5309');
expect(util.formatPhoneNumber('5558675309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('15558675309')).toBe('+1 (555) 867-5309');
expect(util.formatPhoneNumber('125558675309')).toBe('+12 (555) 867-5309');

// Other characters
expect(util.formatPhoneNumber(' (555) 867-5309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('(555) 867-5309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('(555)-867-5309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('(555)-8675309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('(555)8675309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('---8675309')).toBe('867-5309');
expect(util.formatPhoneNumber('-8675309')).toBe('867-5309');
expect(util.formatPhoneNumber('86-5309')).toBe('86-5309');
expect(util.formatPhoneNumber('555 867 5309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('555-867-5309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('555.867.5309')).toBe('(555) 867-5309');
expect(util.formatPhoneNumber('867-5309')).toBe('867-5309');
expect(util.formatPhoneNumber('867-5309 ext. 4')).toBe('867-5309 ext. 4');
expect(util.formatPhoneNumber('Call main office')).toBe('Call main office');
expect(util.formatPhoneNumber('+2111 (555)-867-5309')).toBe('+2111 (555)-867-5309');
expect(util.formatPhoneNumber('125558675309')).toBe('+12 (555) 867-5309');
});

it('getNameOrDefault', () => {
Expand Down

0 comments on commit 0ed5043

Please sign in to comment.