Skip to content

Commit 491d2a3

Browse files
Merge pull request #21 from akirachix/develop
Develop
2 parents 92991fb + f8d46fb commit 491d2a3

File tree

4 files changed

+45
-90
lines changed

4 files changed

+45
-90
lines changed

biopima/src/app/hooks/useFetchLogin.test.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as fetchLoginModule from "../utils/fetchLogin";
44

55
interface FakeUser {
66
user_type: string;
7+
token?: string;
8+
user_id?: number;
79
}
810

911
describe("useLogin hook", () => {
@@ -45,7 +47,11 @@ describe("useLogin hook", () => {
4547
});
4648

4749
it("returns result when login succeeds", async () => {
48-
const fakeUser: FakeUser = { user_type: "admin" };
50+
const fakeUser: FakeUser = {
51+
user_type: "admin",
52+
token: "token123",
53+
user_id: 1,
54+
};
4955
mockLoginUser.mockResolvedValue(fakeUser);
5056

5157
const { result } = renderHook(() => useLogin());
@@ -90,7 +96,7 @@ describe("useLogin hook", () => {
9096
});
9197

9298
expect(response).toBeNull();
93-
expect(result.current.error).toBe("Cannot read properties of undefined (reading 'role')");
99+
expect(result.current.error).toBe("This account does not have access to this role.");
94100
expect(result.current.loading).toBe(false);
95101
});
96102
});
Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
1-
'use client';
1+
"use client";
22
import { useState } from 'react';
33
import { loginUser } from '../utils/fetchLogin';
44

5-
65
export function useLogin() {
7-
const [loading, setLoading] = useState(false);
8-
const [error, setError] = useState<string | null>(null);
9-
10-
11-
async function handleLogin(email: string, password: string, expectedRole?: string) {
12-
setLoading(true);
13-
setError(null);
14-
try {
15-
const result = await loginUser(email, password);
16-
17-
18-
if (expectedRole && result.user.role !== expectedRole) {
19-
setError("This account does not have access to this role.");
20-
return null;
21-
}
22-
23-
return result;
24-
25-
26-
} catch (error) {
27-
setError((error as Error).message);
28-
return null;
29-
} finally {
30-
setLoading(false);
31-
}
32-
}
33-
34-
35-
return { handleLogin, loading, error };
6+
const [loading, setLoading] = useState(false);
7+
const [error, setError] = useState<string | null>(null);
8+
9+
async function handleLogin(email: string, password: string, expectedRole?: string) {
10+
setLoading(true);
11+
setError(null);
12+
try {
13+
const result = await loginUser(email, password);
14+
15+
if (expectedRole && result.user_type !== expectedRole) {
16+
setError("This account does not have access to this role.");
17+
return null;
18+
}
19+
20+
return result;
21+
22+
} catch (error) {
23+
setError((error as Error).message);
24+
return null;
25+
} finally {
26+
setLoading(false);
27+
}
28+
}
29+
30+
return { handleLogin, loading, error };
3631
}
37-
38-
39-
40-
41-

biopima/src/app/login/login.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export default function SignInForm() {
2525
localStorage.setItem("userId", userId);
2626
localStorage.setItem("token", result.token);
2727
localStorage.setItem("email", email);
28-
localStorage.setItem("role", role || "");
28+
localStorage.setItem("role", result.user_type);
2929
}
3030

31-
if (role?.toLowerCase() === "institutional operator") {
31+
if (result.user_type.toLowerCase() === "institutional operator") {
3232
router.push("/dashboard");
3333
} else {
3434
router.push("/institution");

biopima/src/app/login/page.test.tsx

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,28 @@ describe("SignInForm", () => {
1717
beforeEach(() => {
1818
jest.clearAllMocks();
1919
(useRouter as jest.Mock).mockReturnValue({ push: mockPush });
20-
(useSearchParams as jest.Mock).mockReturnValue({
21-
get: jest.fn().mockReturnValue(null),
22-
});
23-
});
24-
25-
it("renders all input fields", () => {
26-
mockUseLogin.mockReturnValue({ handleLogin: jest.fn(), loading: false, error: null });
27-
render(<SignInForm />);
28-
29-
expect(screen.getByPlaceholderText("Enter your email")).toBeInTheDocument();
30-
expect(screen.getByPlaceholderText("Enter your password")).toBeInTheDocument();
31-
});
32-
33-
it("updates input fields on change", () => {
34-
mockUseLogin.mockReturnValue({ handleLogin: jest.fn(), loading: false, error: null });
35-
render(<SignInForm />);
36-
37-
const emailInput = screen.getByPlaceholderText("Enter your email");
38-
fireEvent.change(emailInput, { target: { value: "amanda123@example.com" } });
39-
expect(emailInput).toHaveValue("amanda123@example.com");
40-
41-
const passwordInput = screen.getByPlaceholderText("Enter your password");
42-
fireEvent.change(passwordInput, { target: { value: "amanda@job" } });
43-
expect(passwordInput).toHaveValue("amanda@job");
20+
(useSearchParams as jest.Mock).mockReturnValue({ get: jest.fn().mockReturnValue(null) });
4421
});
4522

4623
it("calls handleLogin and redirects on successful login", async () => {
47-
const mockHandleLogin = jest.fn().mockResolvedValue({});
24+
const mockHandleLogin = jest.fn().mockResolvedValue({
25+
user_type: "user",
26+
token: "abc",
27+
user_id: 1,
28+
});
4829
mockUseLogin.mockReturnValue({ handleLogin: mockHandleLogin, loading: false, error: null });
4930

5031
render(<SignInForm />);
51-
5232
fireEvent.change(screen.getByPlaceholderText("Enter your email"), {
5333
target: { value: "amanda123@example.com" },
5434
});
5535
fireEvent.change(screen.getByPlaceholderText("Enter your password"), {
5636
target: { value: "amanda@job" },
5737
});
58-
5938
fireEvent.click(screen.getByRole("button", { name: /sign in|signing in/i }));
6039

61-
await screen.findByPlaceholderText("Enter your email");
62-
expect(mockHandleLogin).toHaveBeenCalledWith(
63-
"amanda123@example.com",
64-
"amanda@job",
65-
undefined
66-
);
67-
expect(mockPush).toHaveBeenCalledWith("/institution");
68-
});
69-
70-
it("shows error message if login fails", () => {
71-
mockUseLogin.mockReturnValue({ handleLogin: jest.fn(), loading: false, error: "Login failed" });
72-
render(<SignInForm />);
73-
74-
const errorMessage = screen.getByText(/login failed/i);
75-
expect(errorMessage).toBeInTheDocument();
76-
});
77-
78-
it("disables submit button while loading", () => {
79-
mockUseLogin.mockReturnValue({ handleLogin: jest.fn(), loading: true, error: null });
80-
render(<SignInForm />);
81-
82-
const button = screen.getByRole("button", { name: /sign in|signing in/i });
83-
expect(button).toBeDisabled();
40+
await screen.findByPlaceholderText("Enter your email");
41+
expect(mockHandleLogin).toHaveBeenCalledWith("amanda123@example.com", "amanda@job", undefined);
42+
expect(mockPush).toHaveBeenCalledWith("/institution");
8443
});
8544
});

0 commit comments

Comments
 (0)