Skip to content

Commit

Permalink
test: improving tests and covering 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jan 8, 2025
1 parent 0698ade commit a545a38
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 8 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions src/validatePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ValidateFunctions } from "./types";

const defaultErrorMsg: string[] = [
"This password is too long",
"password too short",
"Password too short",
"Password requires at least one capital letter",
"Password requires at least one special character",
"Password requires at least one number",
Expand Down Expand Up @@ -60,7 +60,7 @@ const defaultOptionsParams: OptionsParams = {
* Default:
* [
'This password is too long',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
'password too short',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
'Password too short',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
'Requires at least one capital letter',
'Requires at least one special character',
'Requires at least one number',
Expand Down Expand Up @@ -202,7 +202,7 @@ function getErrorMessage(
: defaultErrorMsg[index];
if (
errorMessage === "This password is too long" ||
errorMessage === "password too short"
errorMessage === "Password too short"
) {
if (maxLenthPassword === Infinity) {
return `Password must be greater than ${minLenthPassword.toString()} characters`;
Expand Down
9 changes: 4 additions & 5 deletions src/validateUsername.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const regexOnlyNumbers: RegExp = /^\d+$/;
const regexStartsWithNumber: RegExp = /^\d/;
const defaultErrorMsg: string[] = [
"Username cannot be empty",
"username too short",
"Username too short",
"This username is too long",
"Username cannot contain spaces",
"Cannot start with a number",
Expand Down Expand Up @@ -166,9 +166,8 @@ function validateLengthParams(
maxLenthUsername: number,
): void {
if (
(typeof minLenthUsername !== "number" ||
typeof maxLenthUsername !== "number") &&
(!Number.isNaN(minLenthUsername) || !Number.isNaN(maxLenthUsername))
typeof minLenthUsername !== "number" ||
typeof maxLenthUsername !== "number"
) {
throw new Error("maxLength or minLength must be a number");
}
Expand All @@ -190,7 +189,7 @@ function getErrorMessage(
? errorMsg[index]
: defaultErrorMsg[index];
if (
errorMessage === "username too short" ||
errorMessage === "Username too short" ||
errorMessage === "This username is too long"
) {
return `Username must be between ${minLenthUsername.toString()} and ${maxLenthUsername.toString()} characters`;
Expand Down
18 changes: 18 additions & 0 deletions tests/src/cnpjValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,22 @@ describe("cnpjIsValid function", () => {
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = cnpjIsValid("12.345.678/0001-91", ["etc", null]);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return default error messages when errorMsg is null", () => {
const result = cnpjIsValid("12.345.678/0001-91", null);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return true for a valid CNPJ where the first verifier is 0", () => {
const cnpj = "69.228.768.0159-00";
const result = cnpjIsValid(cnpj);
expect(result.isValid).toBe(true);
expect(result.errorMsg).toBeNull();
});

});
24 changes: 24 additions & 0 deletions tests/src/getOnlyEmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,28 @@ describe("getOnlyEmail", () => {
);
expect(result).toBe("[email protected]");
});

it("should return the first email when repeatEmail is true and multiple is false", () => {
const result = getOnlyEmail(
"Entre em contato com a equipe: [email protected], [email protected]",
{ multiple: false, cleanDomain: false, repeatEmail: true }
);
expect(result).toBe("[email protected]");
});

it("should return the first cleaned email when repeatEmail is true and multiple is false", () => {
const result = getOnlyEmail(
"Entre em contato com a equipe: [email protected], [email protected]",
{ multiple: false, cleanDomain: true, repeatEmail: true }
);
expect(result).toBe("[email protected]");
});

it("should return all cleaned emails when repeatEmail is true and multiple is true", () => {
const result = getOnlyEmail(
"Entre em contato com a equipe: [email protected], [email protected]",
{ multiple: true, cleanDomain: true, repeatEmail: true }
);
expect(result).toEqual(["[email protected]", "[email protected]"]);
});
});
16 changes: 16 additions & 0 deletions tests/src/isDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ describe("isDate", () => {
const result = isDate("2022-02-31");
expect(result).toBe(false);
});

it("should return false for a date in February of a non-leap year divisible by 100 but not by 400", () => {
const result = isDate("1900-02-29");
expect(result).toBe(false);
});

it("should return true for February 29 in a leap year divisible by 4 but not by 100", () => {
const result = isDate("2024-02-29");
expect(result).toBe(true);
});

it("should return true for February 29 in a leap year divisible by 400", () => {
const result = isDate("2000-02-29");
expect(result).toBe(true);
});

});
16 changes: 16 additions & 0 deletions tests/src/validateBRPhoneNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ describe("validateBRPhoneNumber", () => {
errorMsg: null,
});
});

it("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = validateBRPhoneNumber("12345678", ["etc", null]);
expect(result).toEqual({
isValid: false,
errorMsg: "Invalid phone number",
});
});

it("should return default error messages when errorMsg is null", () => {
const result = validateBRPhoneNumber("12345678", null);
expect(result).toEqual({
isValid: false,
errorMsg: "Invalid phone number",
});
});
});
9 changes: 9 additions & 0 deletions tests/src/validateEmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,13 @@ describe("validateEmail", () => {
expect(result.isValid).toBe(true);
expect(result.errorMsg).toBe(null);
});

it("should validate email and return the custom error message and defaultMsgErrors too", () => {
const result = validateEmail("[email protected]", {
errorMsg: ["Custom error message", null, "Custom error message 2"],
});

expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("This e-mail is not valid");
});
});
19 changes: 19 additions & 0 deletions tests/src/validatePassword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,23 @@ describe("validatePassword", () => {
}),
).toThrow("No size can be smaller than 1");
});

it("should return default error messages when errorMsg is undefined", () => {
const result = validatePassword("Passw", {
minLength: 8,
maxLength: 20,
errorMsg: undefined,
});

expect(result.errorMsg).toBe("Password must be between 8 and 20 characters");
});

it("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = validatePassword("Passw", {
minLength: 8,
maxLength: 20,
errorMsg: ["etc", null],
});
expect(result.errorMsg).toBe("Password too short");
});
});
16 changes: 16 additions & 0 deletions tests/src/validatePhoneNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ describe("validatePhoneNumber", () => {
"The input should be a string.",
);
});

it("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = validatePhoneNumber("12345", ["etc", null]);
expect(result).toEqual({
isValid: false,
errorMsg: "Invalid phone number",
});
});

it("should return default error messages when errorMsg is null", () => {
const result = validatePhoneNumber("12345", null);
expect(result).toEqual({
isValid: false,
errorMsg: "Invalid phone number",
});
});
});
18 changes: 18 additions & 0 deletions tests/src/validateTextarea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,22 @@ describe("validateTextarea", () => {
}),
).toThrow("All values within the array must be strings or null/undefined.");
});

it("should return default error messages when errorMsg[null, 'etc'] is passed", () => {
const result = validateTextarea("This is a valid textarea.", {
isRequired: true,
maxLength: 15,
errorMsg: [null, "etc"],
});
expect(result.errorMsg).toBe("This textarea is too big");
});

it("should return default error messages when errorMsg is null", () => {
const result = validateTextarea("This is a valid textarea.", {
isRequired: true,
maxLength: 15,
errorMsg: null as any,
});
expect(result.errorMsg).toBe("Textarea cannot exceed 15 characters");
});
});
10 changes: 10 additions & 0 deletions tests/src/validateUSPhoneNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ describe("validateUSPhoneNumber", () => {
"The input should be a string.",
);
});

it("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = validateUSPhoneNumber("1234567", ["etc", null]);
expect(result.errorMsg).toBe("Invalid phone number");
});

it("should return default error messages when errorMsg is null", () => {
const result = validateUSPhoneNumber("1234567", null);
expect(result.errorMsg).toBe("Invalid phone number");
});
});
27 changes: 27 additions & 0 deletions tests/src/validateUsername.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,31 @@ describe("validateUsername", () => {
expect(result2.isValid).toBe(false);
expect(result3.isValid).toBe(false);
});

it("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = validateUsername("Us", {
minLength: 3,
maxLength: 25,
errorMsg: ["etc", null],
});
expect(result.errorMsg).toBe("Username too short");
});

it("should return default error messages when errorMsg is null", () => {
const result = validateUsername("Us", {
minLength: 3,
maxLength: 25,
errorMsg: null as any,
});
expect(result.errorMsg).toBe("Username must be between 3 and 25 characters");
});

it("should throw an error if minLength is number but maxLength is not", () => {
expect(() =>
validateUsername("User123", {
minLength: 3,
maxLength: "aw" as any,
}),
).toThrow("maxLength or minLength must be a number");
});
});

0 comments on commit a545a38

Please sign in to comment.