Skip to content

Commit

Permalink
add tests for validators (#1471)
Browse files Browse the repository at this point in the history
Provide tests for `validateEmail`, `validatePhoneNumber`,
`validateForgotUsernameForm`.
  • Loading branch information
zburke authored May 29, 2024
1 parent 7b08224 commit 4312058
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/validators/validateEmail/validateEmail.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* validate an email address
* Email address validation is notoriously difficult. I don't know where this
* particular regex came from but it shares substantially with well-known
* versions such as https://emailregex.com/ and does a good enough job for us.
*
* This was introduced in STCOR-276/PR #496
*/
export default email => {
// eslint-disable-next-line no-useless-escape
const emailRegExp = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
Expand Down
23 changes: 23 additions & 0 deletions src/validators/validateEmail/validateEmail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import validateEmail from './validateEmail';

describe('validateEmail', () => {
it('accepts valid email addresses', () => {
expect(validateEmail('test@example.edu')).toBe(true);
});

it('accepts values with comments in username', () => {
expect(validateEmail('test+comment@example.edu')).toBe(true);
});

it('rejects values without @', () => {
expect(validateEmail('test$example.edu')).toBe(false);
});

it('rejects values without domain suffix', () => {
expect(validateEmail('test@example')).toBe(false);
});

it('rejects values with whitespace', () => {
expect(validateEmail(' test@example.com ')).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import validateForgotUsernameForm from './validateForgotUsernameForm';

describe('validateForgotUsernameForm', () => {
describe('accepts valid email addresses and phone numbers', () => {
it('accepts valid email addresses', () => {
expect(validateForgotUsernameForm('test@example.edu')).toBe(true);
});

it('accepts values with comments in username', () => {
expect(validateForgotUsernameForm('test+comment@example.edu')).toBe(true);
});

it('accepts values with numbers only', () => {
expect(validateForgotUsernameForm('12')).toBe(true);
});

it('accepts values with numbers and dashes', () => {
expect(validateForgotUsernameForm('1-2')).toBe(true);
});

it('accepts values with numbers and dots', () => {
expect(validateForgotUsernameForm('1.2')).toBe(true);
});
});

it('rejects values that are not valid email addresses or phone numbers', () => {
// validateForgotUsernameForm trims whitespace so we don't test for
// whitespace rejection here, even though other validators do.
expect(validateForgotUsernameForm('not an email address')).toBe(false);
expect(validateForgotUsernameForm('not a phone number')).toBe(false);
expect(validateForgotUsernameForm('test$example.edu')).toBe(false);
expect(validateForgotUsernameForm('test@example')).toBe(false);
expect(validateForgotUsernameForm('1.2a')).toBe(false);
expect(validateForgotUsernameForm('1.2!')).toBe(false);
});
});
22 changes: 22 additions & 0 deletions src/validators/validatePhoneNumber/validatePhoneNumber.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import validatePhoneNumber from './validatePhoneNumber';

describe('validatePhoneNumber', () => {
it('accepts values with numbers only', () => {
expect(validatePhoneNumber('12')).toBe(true);
});

it('accepts values with numbers and dashes', () => {
expect(validatePhoneNumber('1-2')).toBe(true);
});

it('accepts values with numbers and dots', () => {
expect(validatePhoneNumber('1.2')).toBe(true);
});

it('rejects values containing characters other than numbers, dashes, and dots', () => {
expect(validatePhoneNumber('1.2 ')).toBe(false);
expect(validatePhoneNumber('1.2a')).toBe(false);
expect(validatePhoneNumber('1.2!')).toBe(false);
});
});

0 comments on commit 4312058

Please sign in to comment.