Skip to content

Commit 753da29

Browse files
luizhf42otavio
authored andcommitted
test(ui-react): add unit tests for validateRecoveryEmail
1 parent 23bde6c commit 753da29

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

ui-react/apps/admin/src/pages/Profile.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PageHeader from "../components/common/PageHeader";
44
import Drawer from "../components/common/Drawer";
55
import { AxiosError } from "axios";
66
import { LABEL, INPUT } from "../utils/styles";
7+
import { validateRecoveryEmail } from "./profile/validate";
78
import {
89
UserIcon,
910
PencilSquareIcon,
@@ -41,13 +42,6 @@ function validateEmail(v: string): string | null {
4142
return null;
4243
}
4344

44-
function validateRecoveryEmail(recoveryEmail: string, primaryEmail: string): string | null {
45-
if (!recoveryEmail) return null;
46-
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(recoveryEmail)) return "Invalid email format";
47-
if (recoveryEmail.toLowerCase() === primaryEmail.toLowerCase()) return "Must be different from your email";
48-
return null;
49-
}
50-
5145
function validatePassword(v: string): string | null {
5246
if (v.length < 5) return "Password must be at least 5 characters";
5347
if (v.length > 32) return "Password must be at most 32 characters";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, it, expect } from "vitest";
2+
import { validateRecoveryEmail } from "../validate";
3+
4+
describe("validateRecoveryEmail", () => {
5+
it("returns null when recovery email is empty", () => {
6+
expect(validateRecoveryEmail("", "user@example.com")).toBeNull();
7+
});
8+
9+
it("rejects invalid email format", () => {
10+
expect(validateRecoveryEmail("not-an-email", "user@example.com")).toBe("Invalid email format");
11+
});
12+
13+
it("rejects when emails match exactly", () => {
14+
expect(validateRecoveryEmail("user@example.com", "user@example.com")).toBe("Must be different from your email");
15+
});
16+
17+
it("rejects when emails match case-insensitively", () => {
18+
expect(validateRecoveryEmail("User@Example.COM", "user@example.com")).toBe("Must be different from your email");
19+
});
20+
21+
it("rejects when primary email has mixed case", () => {
22+
expect(validateRecoveryEmail("user@example.com", "User@Example.COM")).toBe("Must be different from your email");
23+
});
24+
25+
it("returns null for a valid different email", () => {
26+
expect(validateRecoveryEmail("other@example.com", "user@example.com")).toBeNull();
27+
});
28+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function validateRecoveryEmail(recoveryEmail: string, primaryEmail: string): string | null {
2+
if (!recoveryEmail) return null;
3+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(recoveryEmail)) return "Invalid email format";
4+
if (recoveryEmail.toLowerCase() === primaryEmail.toLowerCase()) return "Must be different from your email";
5+
return null;
6+
}

0 commit comments

Comments
 (0)