Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: email test cases #276

Closed
wants to merge 9 commits into from
246 changes: 193 additions & 53 deletions library/src/validations/email/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,200 @@ import { describe, expect, test } from 'vitest';
import { email } from './email.ts';

describe('email', () => {
test('should pass only emails', () => {
const validate = email();

const value1 = '[email protected]';
expect(validate._parse(value1).output).toBe(value1);
const value2 = '[email protected]';
expect(validate._parse(value2).output).toBe(value2);
const value3 = '[email protected]';
expect(validate._parse(value3).output).toBe(value3);
const value4 = '[email protected]';
expect(validate._parse(value4).output).toBe(value4);
const value5 = '[email protected]';
expect(validate._parse(value5).output).toBe(value5);
const value6 = '[email protected]';
expect(validate._parse(value6).output).toBe(value6);
const value7 = '[email protected]';
expect(validate._parse(value7).output).toBe(value7);
const value8 = '[email protected]';
expect(validate._parse(value8).output).toBe(value8);
const value9 = '[email protected]';
expect(validate._parse(value9).output).toBe(value9);
const value10 = '[email protected]';
expect(validate._parse(value10).output).toBe(value10);
const value11 = '[email protected]';
expect(validate._parse(value11).output).toBe(value11);
const value12 = '[email protected]';
expect(validate._parse(value12).output).toBe(value12);

expect(validate._parse('plainaddress').issues).toBeTruthy();
expect(validate._parse('#@%^%#$@#$@#.com').issues).toBeTruthy();
expect(validate._parse('@example.com').issues).toBeTruthy();
expect(
validate._parse('Joe Smith <[email protected]>').issues
).toBeTruthy();
expect(validate._parse('email.example.com').issues).toBeTruthy();
expect(validate._parse('email@[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('あいうえお@example.com').issues).toBeTruthy();
expect(
validate._parse('[email protected] (Joe Smith)').issues
).toBeTruthy();
expect(validate._parse('email@example').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
expect(validate._parse('[email protected]').issues).toBeTruthy();
describe('should return action object', () => {
test('should contain the default message', () => {
expect(email()).toMatchObject({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is .toMatchObject() less strict than .toEqual()? Why do you choose it here?

type: 'email',
async: false,
message: 'Invalid email',
requirement: expect.any(RegExp),
_parse: expect.any(Function),
});
});

test('should contain the message argument', () => {
const message = 'Custom message';
expect(email(message)).toMatchObject({
type: 'email',
async: false,
message: message,
requirement: expect.any(RegExp),
_parse: expect.any(Function),
});
});
});

describe('should return action output', () => {
test('should pass standard email', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with country code top level domain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with dot in local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with hyphen in local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with plus in local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with plus in end of local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with plus in begining of local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with hyphen in domain part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with long top level domain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with numerical local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with numerical local part', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with subdomain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with subdomain and hyphen in domain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with subdomain and country code top level domain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with short domain and top level domain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});

test('should pass email with numeric domain and top level domain', () => {
const value = '[email protected]';
expect(email()._parse(value)).toEqual({ output: value });
});
});

test('should return custom error message', () => {
const error = 'Value is not an email!';
const validate = email(error);
expect(validate._parse('test').issues?.[0].message).toBe(error);
describe('should return action issue', () => {
test('should reject email with missing @ symbol', () => {
const value = 'example.com';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with missing domain', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with special chars in top level domain', () => {
const value = '#@%^%#$@#$@#.com';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with no top-level domain', () => {
const value = 'email@example';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with numeric top-level domain', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with only domain', () => {
const value = '@example.com';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with only local part', () => {
const value = 'email@';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with spaces', () => {
const value = ' [email protected] ';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email with multiple @ symbols', () => {
const value = ' email@@example.com ';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where domain consists from numerical values', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where local part consists of non ASCII chars', () => {
const value = 'あいうえお@example.com';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where local part begin with dot', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where local part end with special char', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where domain part end with special char', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where domain part end with special char', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where two dots separate in local part', () => {
const value = '[email protected]';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where name is represented in front of email', () => {
const value = 'Joe Smith <[email protected]>';
expect(email()._parse(value).issues).toBeTruthy();
});

test('should reject email where name is represented after email', () => {
const value = '[email protected] (Joe Smith)';
expect(email()._parse(value).issues).toBeTruthy();
});
});
});
Loading