Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
feat(rich-text-editor): tests for rich text editor link extension and…
Browse files Browse the repository at this point in the history
… utils (#1001)
  • Loading branch information
MikaDialpad authored and juliodialpad committed Jun 9, 2023
1 parent 9aeefc6 commit b947270
Show file tree
Hide file tree
Showing 6 changed files with 796 additions and 23 deletions.
24 changes: 14 additions & 10 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,30 +284,34 @@ export const linkRegex = new RegExp(

/**
* Check if a string is a phone number. Validates only exact matches.
* @param {string} string
* @param {string|number} input
* @returns {boolean}
*/
export function isPhoneNumber (string) {
return phoneNumberRegex.exec(string)?.[0] === string;
export function isPhoneNumber (input) {
if (!input || (!['string', 'number'].includes(typeof input))) return false;
input = input.toString();
return phoneNumberRegex.exec(input)?.[0] === input;
}

/**
* Check if a string is an URL. Validates only exact matches.
* @param {string} string
* @param {string} input
* @returns {boolean}
*/
export function isURL (string) {
return urlWithoutProtocolRegex.exec(string)?.[0] === string ||
urlWithProtocolRegex.exec(string)?.[0] === string;
export function isURL (input) {
if (!input || typeof input !== 'string') return false;
return urlWithoutProtocolRegex.exec(input)?.[0] === input ||
urlWithProtocolRegex.exec(input)?.[0] === input;
}

/**
* Check if a string is an email address. Validates only exact matches.
* @param {string} string
* @param {string} input
* @returns {boolean}
*/
export function isEmailAddress (string) {
return emailAddressRegex.exec(string)?.[0] === string;
export function isEmailAddress (input) {
if (!input || typeof input !== 'string') return false;
return emailAddressRegex.exec(input)?.[0] === input;
}

export default {
Expand Down
131 changes: 131 additions & 0 deletions common/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
filterFormattedMessages,
hasFormattedMessageOfType,
getValidationState,
isPhoneNumber,
isURL,
isEmailAddress,
} from './utils';

describe('Util Tests', () => {
Expand Down Expand Up @@ -344,4 +347,132 @@ describe('Util Tests', () => {
});
});
});

describe('isPhoneNumber', () => {
describe('when there is no input', () => {
it.each([
[],
[''],
])('should return false for "%s"', async (input) => {
expect(isPhoneNumber(input)).toBe(false);
});
});

describe('when input is not a string or a number', () => {
it.each([
[[1, 2, 3]],
[{ phone_number: '+17144107035' }],
[undefined],
])('should return false for "%s"', async (input) => {
expect(isPhoneNumber(input)).toBe(false);
});
});

describe('when input is not an exact match', () => {
it('should return false', () => {
expect(isPhoneNumber('oops text (714) 410-7035')).toBe(false);
});
});

describe('when input is an exact match', () => {
it.each([
['(714) 410-7035'],
['714-410-7035'],
['+17144107035'],
['714 410 7035'],
[7144107035],
])('should return true for "%s"', async (input) => {
expect(isPhoneNumber(input)).toBe(true);
});
});
});

describe('isURL', () => {
describe('when there is no input', () => {
it.each([
[],
[''],
])('should return false for "%s"', async (input) => {
expect(isURL(input)).toBe(false);
});
});

describe('when input is not a string', () => {
it.each([
[123],
[[1, 2, 3]],
[{ url: 'dialpad.com' }],
[undefined],
])('should return false for "%s"', async (input) => {
expect(isURL(input)).toBe(false);
});
});

describe('when input is not an exact match', () => {
it('should return false', () => {
expect(isURL('oops text dialpad.com')).toBe(false);
});
});

describe('when input is an exact match', () => {
const validURLs = [
['dialpad.com'],
['dialpad.com/news'],
['help.dialpad.com'],
['dialpad.com?cache=1&dark=true'],
];
const validURLsWithProtocol = validURLs.map(url => [`https://${url[0]}`]);

it.each([
...validURLs,
...validURLsWithProtocol,
])('should return true for "%s"', async (input) => {
expect(isURL(input)).toBe(true);
});
});
});

describe('isEmailAddress', () => {
describe('when there is no input', () => {
it.each([
[],
[''],
])('should return false for "%s"', async (input) => {
expect(isEmailAddress(input)).toBe(false);
});
});

describe('when input is not a string', () => {
it.each([
[123],
[[1, 2, 3]],
[{ email: 'noreply@dialpad.com' }],
[undefined],
])('should return false for "%s"', async (input) => {
expect(isEmailAddress(input)).toBe(false);
});
});

describe('when input is not an exact match', () => {
it('should return false', () => {
expect(isEmailAddress('oops text noreply@dialpad.com')).toBe(false);
});
});

describe('when input is an exact match', () => {
const validEmails = [
['noreply@dialpad.com'],
['help.noreply@dialpad.com'],
['noreply@dialpad.com?subject=Hi&body=Hey%20there!'],
];
const validEmailsWithPrefix = validEmails.map(email => [`mailto:${email[0]}`]);

it.each([
...validEmails,
...validEmailsWithPrefix,
])('should return true for "%s"', async (input) => {
expect(isEmailAddress(input)).toBe(true);
});
});
});
});
Loading

0 comments on commit b947270

Please sign in to comment.