Skip to content

Commit a545a38

Browse files
committed
test: improving tests and covering 100%
1 parent 0698ade commit a545a38

13 files changed

+180
-8
lines changed

.yarn/install-state.gz

0 Bytes
Binary file not shown.

src/validatePassword.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ValidateFunctions } from "./types";
22

33
const defaultErrorMsg: string[] = [
44
"This password is too long",
5-
"password too short",
5+
"Password too short",
66
"Password requires at least one capital letter",
77
"Password requires at least one special character",
88
"Password requires at least one number",
@@ -60,7 +60,7 @@ const defaultOptionsParams: OptionsParams = {
6060
* Default:
6161
* [
6262
'This password is too long',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
63-
'password too short',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
63+
'Password too short',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
6464
'Requires at least one capital letter',
6565
'Requires at least one special character',
6666
'Requires at least one number',
@@ -202,7 +202,7 @@ function getErrorMessage(
202202
: defaultErrorMsg[index];
203203
if (
204204
errorMessage === "This password is too long" ||
205-
errorMessage === "password too short"
205+
errorMessage === "Password too short"
206206
) {
207207
if (maxLenthPassword === Infinity) {
208208
return `Password must be greater than ${minLenthPassword.toString()} characters`;

src/validateUsername.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const regexOnlyNumbers: RegExp = /^\d+$/;
55
const regexStartsWithNumber: RegExp = /^\d/;
66
const defaultErrorMsg: string[] = [
77
"Username cannot be empty",
8-
"username too short",
8+
"Username too short",
99
"This username is too long",
1010
"Username cannot contain spaces",
1111
"Cannot start with a number",
@@ -166,9 +166,8 @@ function validateLengthParams(
166166
maxLenthUsername: number,
167167
): void {
168168
if (
169-
(typeof minLenthUsername !== "number" ||
170-
typeof maxLenthUsername !== "number") &&
171-
(!Number.isNaN(minLenthUsername) || !Number.isNaN(maxLenthUsername))
169+
typeof minLenthUsername !== "number" ||
170+
typeof maxLenthUsername !== "number"
172171
) {
173172
throw new Error("maxLength or minLength must be a number");
174173
}
@@ -190,7 +189,7 @@ function getErrorMessage(
190189
? errorMsg[index]
191190
: defaultErrorMsg[index];
192191
if (
193-
errorMessage === "username too short" ||
192+
errorMessage === "Username too short" ||
194193
errorMessage === "This username is too long"
195194
) {
196195
return `Username must be between ${minLenthUsername.toString()} and ${maxLenthUsername.toString()} characters`;

tests/src/cnpjValidator.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,22 @@ describe("cnpjIsValid function", () => {
7979
expect(result.isValid).toBe(false);
8080
expect(result.errorMsg).toBe("CNPJ is not valid");
8181
});
82+
83+
test("should return default error messages when errorMsg['etc', null] is passed", () => {
84+
const result = cnpjIsValid("12.345.678/0001-91", ["etc", null]);
85+
expect(result.errorMsg).toBe("CNPJ is not valid");
86+
});
87+
88+
test("should return default error messages when errorMsg is null", () => {
89+
const result = cnpjIsValid("12.345.678/0001-91", null);
90+
expect(result.errorMsg).toBe("CNPJ is not valid");
91+
});
92+
93+
test("should return true for a valid CNPJ where the first verifier is 0", () => {
94+
const cnpj = "69.228.768.0159-00";
95+
const result = cnpjIsValid(cnpj);
96+
expect(result.isValid).toBe(true);
97+
expect(result.errorMsg).toBeNull();
98+
});
99+
82100
});

tests/src/getOnlyEmail.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,28 @@ describe("getOnlyEmail", () => {
108108
);
109109
expect(result).toBe("[email protected]");
110110
});
111+
112+
it("should return the first email when repeatEmail is true and multiple is false", () => {
113+
const result = getOnlyEmail(
114+
"Entre em contato com a equipe: [email protected], [email protected]",
115+
{ multiple: false, cleanDomain: false, repeatEmail: true }
116+
);
117+
expect(result).toBe("[email protected]");
118+
});
119+
120+
it("should return the first cleaned email when repeatEmail is true and multiple is false", () => {
121+
const result = getOnlyEmail(
122+
"Entre em contato com a equipe: [email protected], [email protected]",
123+
{ multiple: false, cleanDomain: true, repeatEmail: true }
124+
);
125+
expect(result).toBe("[email protected]");
126+
});
127+
128+
it("should return all cleaned emails when repeatEmail is true and multiple is true", () => {
129+
const result = getOnlyEmail(
130+
"Entre em contato com a equipe: [email protected], [email protected]",
131+
{ multiple: true, cleanDomain: true, repeatEmail: true }
132+
);
133+
expect(result).toEqual(["[email protected]", "[email protected]"]);
134+
});
111135
});

tests/src/isDate.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,20 @@ describe("isDate", () => {
5151
const result = isDate("2022-02-31");
5252
expect(result).toBe(false);
5353
});
54+
55+
it("should return false for a date in February of a non-leap year divisible by 100 but not by 400", () => {
56+
const result = isDate("1900-02-29");
57+
expect(result).toBe(false);
58+
});
59+
60+
it("should return true for February 29 in a leap year divisible by 4 but not by 100", () => {
61+
const result = isDate("2024-02-29");
62+
expect(result).toBe(true);
63+
});
64+
65+
it("should return true for February 29 in a leap year divisible by 400", () => {
66+
const result = isDate("2000-02-29");
67+
expect(result).toBe(true);
68+
});
69+
5470
});

tests/src/validateBRPhoneNumber.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@ describe("validateBRPhoneNumber", () => {
4242
errorMsg: null,
4343
});
4444
});
45+
46+
it("should return default error messages when errorMsg['etc', null] is passed", () => {
47+
const result = validateBRPhoneNumber("12345678", ["etc", null]);
48+
expect(result).toEqual({
49+
isValid: false,
50+
errorMsg: "Invalid phone number",
51+
});
52+
});
53+
54+
it("should return default error messages when errorMsg is null", () => {
55+
const result = validateBRPhoneNumber("12345678", null);
56+
expect(result).toEqual({
57+
isValid: false,
58+
errorMsg: "Invalid phone number",
59+
});
60+
});
4561
});

tests/src/validateEmail.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,13 @@ describe("validateEmail", () => {
116116
expect(result.isValid).toBe(true);
117117
expect(result.errorMsg).toBe(null);
118118
});
119+
120+
it("should validate email and return the custom error message and defaultMsgErrors too", () => {
121+
const result = validateEmail("[email protected]", {
122+
errorMsg: ["Custom error message", null, "Custom error message 2"],
123+
});
124+
125+
expect(result.isValid).toBe(false);
126+
expect(result.errorMsg).toBe("This e-mail is not valid");
127+
});
119128
});

tests/src/validatePassword.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,23 @@ describe("validatePassword", () => {
185185
}),
186186
).toThrow("No size can be smaller than 1");
187187
});
188+
189+
it("should return default error messages when errorMsg is undefined", () => {
190+
const result = validatePassword("Passw", {
191+
minLength: 8,
192+
maxLength: 20,
193+
errorMsg: undefined,
194+
});
195+
196+
expect(result.errorMsg).toBe("Password must be between 8 and 20 characters");
197+
});
198+
199+
it("should return default error messages when errorMsg['etc', null] is passed", () => {
200+
const result = validatePassword("Passw", {
201+
minLength: 8,
202+
maxLength: 20,
203+
errorMsg: ["etc", null],
204+
});
205+
expect(result.errorMsg).toBe("Password too short");
206+
});
188207
});

tests/src/validatePhoneNumber.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,20 @@ describe("validatePhoneNumber", () => {
4444
"The input should be a string.",
4545
);
4646
});
47+
48+
it("should return default error messages when errorMsg['etc', null] is passed", () => {
49+
const result = validatePhoneNumber("12345", ["etc", null]);
50+
expect(result).toEqual({
51+
isValid: false,
52+
errorMsg: "Invalid phone number",
53+
});
54+
});
55+
56+
it("should return default error messages when errorMsg is null", () => {
57+
const result = validatePhoneNumber("12345", null);
58+
expect(result).toEqual({
59+
isValid: false,
60+
errorMsg: "Invalid phone number",
61+
});
62+
});
4763
});

tests/src/validateTextarea.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,22 @@ describe("validateTextarea", () => {
8181
}),
8282
).toThrow("All values within the array must be strings or null/undefined.");
8383
});
84+
85+
it("should return default error messages when errorMsg[null, 'etc'] is passed", () => {
86+
const result = validateTextarea("This is a valid textarea.", {
87+
isRequired: true,
88+
maxLength: 15,
89+
errorMsg: [null, "etc"],
90+
});
91+
expect(result.errorMsg).toBe("This textarea is too big");
92+
});
93+
94+
it("should return default error messages when errorMsg is null", () => {
95+
const result = validateTextarea("This is a valid textarea.", {
96+
isRequired: true,
97+
maxLength: 15,
98+
errorMsg: null as any,
99+
});
100+
expect(result.errorMsg).toBe("Textarea cannot exceed 15 characters");
101+
});
84102
});

tests/src/validateUSPhoneNumber.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ describe("validateUSPhoneNumber", () => {
4444
"The input should be a string.",
4545
);
4646
});
47+
48+
it("should return default error messages when errorMsg['etc', null] is passed", () => {
49+
const result = validateUSPhoneNumber("1234567", ["etc", null]);
50+
expect(result.errorMsg).toBe("Invalid phone number");
51+
});
52+
53+
it("should return default error messages when errorMsg is null", () => {
54+
const result = validateUSPhoneNumber("1234567", null);
55+
expect(result.errorMsg).toBe("Invalid phone number");
56+
});
4757
});

tests/src/validateUsername.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,31 @@ describe("validateUsername", () => {
156156
expect(result2.isValid).toBe(false);
157157
expect(result3.isValid).toBe(false);
158158
});
159+
160+
it("should return default error messages when errorMsg['etc', null] is passed", () => {
161+
const result = validateUsername("Us", {
162+
minLength: 3,
163+
maxLength: 25,
164+
errorMsg: ["etc", null],
165+
});
166+
expect(result.errorMsg).toBe("Username too short");
167+
});
168+
169+
it("should return default error messages when errorMsg is null", () => {
170+
const result = validateUsername("Us", {
171+
minLength: 3,
172+
maxLength: 25,
173+
errorMsg: null as any,
174+
});
175+
expect(result.errorMsg).toBe("Username must be between 3 and 25 characters");
176+
});
177+
178+
it("should throw an error if minLength is number but maxLength is not", () => {
179+
expect(() =>
180+
validateUsername("User123", {
181+
minLength: 3,
182+
maxLength: "aw" as any,
183+
}),
184+
).toThrow("maxLength or minLength must be a number");
185+
});
159186
});

0 commit comments

Comments
 (0)