Skip to content

Commit 0958235

Browse files
committed
Feat: User profile testcases
1 parent 43c0152 commit 0958235

File tree

6 files changed

+320
-21
lines changed

6 files changed

+320
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"test:task4": "(react-scripts test test/task4/App.test.js --watchAll=false --env=jsdom --verbose --colors --testResultsProcessor=jest-junit); (mv junit.xml ./output/task4.xml)",
2525
"test:task5": "(react-scripts test test/task5/App.test.js --watchAll=false --env=jsdom --verbose --colors --testResultsProcessor=jest-junit); (mv junit.xml ./output/task5.xml)",
2626
"test:task6": "(react-scripts test test/task6/App.test.js --watchAll=false --env=jsdom --verbose --colors --testResultsProcessor=jest-junit); (mv junit.xml ./output/task6.xml)",
27+
"test:task7": "(react-scripts test test/task7/App.test.js --watchAll=false --env=jsdom --verbose --colors --testResultsProcessor=jest-junit); (mv junit.xml ./output/task7.xml)",
2728
"eject": "react-scripts eject"
2829
},
2930
"devDependencies": {

src/components/Login.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ const Login = ({ setUser }) => {
1010
const navigate = useNavigate();
1111

1212
const handleLogin = () => {
13+
if (!email && !password) {
14+
setError("Please enter both email and password");
15+
return;
16+
}
17+
18+
if (!email) {
19+
setError("Please enter your email");
20+
return;
21+
}
22+
23+
if (!password) {
24+
setError("Please enter your password");
25+
return;
26+
}
27+
1328
const user = usersData.find(
1429
(u) => u.email === email && u.password === password
1530
);
@@ -22,7 +37,11 @@ const Login = ({ setUser }) => {
2237
navigate("/");
2338
}, 2000);
2439
} else {
25-
setError("Invalid email or password");
40+
if (!usersData.some((u) => u.email === email)) {
41+
setError("Invalid email");
42+
} else {
43+
setError("Invalid password");
44+
}
2645
}
2746
};
2847

@@ -32,6 +51,7 @@ const Login = ({ setUser }) => {
3251
<h2 className="text-2xl font-bold mb-6 text-center">Login</h2>
3352
<label className="block text-gray-700 mb-2" htmlFor="email">Email</label>
3453
<input
54+
data-testid="email-input"
3555
id="email"
3656
type="email"
3757
placeholder="Email"
@@ -41,15 +61,17 @@ const Login = ({ setUser }) => {
4161
/>
4262
<label className="block text-gray-700 mb-2" htmlFor="password">Password</label>
4363
<input
64+
data-testid="password-input"
4465
id="password"
4566
type="password"
4667
placeholder="Password"
4768
value={password}
4869
onChange={(e) => setPassword(e.target.value)}
4970
className="w-full mb-6 p-2 border rounded"
5071
/>
51-
{error && <p className="text-red-500 my-8">{error}</p>}
72+
{error && <p data-testid="error" className="text-red-500 my-8">{error}</p>}
5273
<button
74+
data-testid="login-button"
5375
onClick={handleLogin}
5476
className="w-full bg-green-700 text-white p-2 mt-16 rounded"
5577
>

src/components/Modals/UserModal.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { useNavigate } from "react-router-dom";
44
const UserModal = ({ onClose, user, setUser }) => {
55
const navigate = useNavigate();
66

7-
console.log("UserModal -1 user:", user);
8-
97
const handleLogout = () => {
108
setUser(null);
119
onClose();
@@ -18,6 +16,7 @@ const UserModal = ({ onClose, user, setUser }) => {
1816
{user ? (
1917
<>
2018
<button
19+
data-testid="user-profile-btn"
2120
className="w-full text-left py-1 px-2 rounded hover:bg-gray-100 transition"
2221
onClick={() => {
2322
navigate("/user-profile");
@@ -27,6 +26,7 @@ const UserModal = ({ onClose, user, setUser }) => {
2726
User Profile
2827
</button>
2928
<button
29+
data-testid="logout-btn"
3030
className="w-full text-left py-1 px-2 rounded hover:bg-gray-100 transition"
3131
onClick={handleLogout}
3232
>
@@ -35,6 +35,7 @@ const UserModal = ({ onClose, user, setUser }) => {
3535
</>
3636
) : (
3737
<button
38+
data-testid="login-btn"
3839
className="w-full text-left py-1 px-2 rounded hover:bg-gray-100 transition"
3940
onClick={() => {
4041
navigate("/login");

src/components/UserProfile.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import React from "react";
2+
import { ArrowLeft } from "lucide-react";
23
import placeholder from "../assests/user-placeholder.png";
4+
import { useNavigate } from "react-router-dom";
35

46
const UserProfile = ({ user }) => {
7+
const navigate = useNavigate();
8+
59
return (
6-
<div className="min-h-screen flex flex-col">
7-
<div className="flex-grow flex items-center justify-center">
10+
<div className="min-h-screen flex items-center justify-center">
11+
<div className="flex flex-col items-start">
12+
<button
13+
data-testid="home-btn"
14+
onClick={() => navigate("/")}
15+
className="flex border border-gray-300 rounded-md p-2 mb-4"
16+
>
17+
<ArrowLeft className="h-12 w-16 mr-1" />
18+
Back to Home
19+
</button>
820
<div className="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden">
921
<div className="flex flex-col items-center p-8">
1022
<img
@@ -13,10 +25,13 @@ const UserProfile = ({ user }) => {
1325
alt={user.name}
1426
/>
1527
<div className="text-center">
16-
<div className="text-2xl font-bold text-green-700 mb-14">
28+
<div
29+
data-testid="username"
30+
className="text-2xl font-bold text-green-700 mb-14"
31+
>
1732
{user.name}
1833
</div>
19-
<div className="text-left">
34+
<div data-testid="user-details" className="text-left">
2035
<p className="mt-2 text-gray-500">Email: {user.email}</p>
2136
<p className="mt-2 text-gray-500">DOB: {user.dob}</p>
2237
<p className="mt-2 text-gray-500">Address: {user.address}</p>
@@ -29,4 +44,4 @@ const UserProfile = ({ user }) => {
2944
);
3045
};
3146

32-
export default UserProfile;
47+
export default UserProfile;

src/data/users.json

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11
[
22
{
3-
"id": 1,
4-
"name": "John Doe",
5-
"email": "john@example.com",
6-
"password": "password123",
7-
"dob": "1990-01-01",
8-
"address": "123 Main St, Anytown, USA"
3+
"id": 1,
4+
"name": "Alex Taylor",
5+
"email": "alex.taylor@example.com",
6+
"password": "securePass123",
7+
"dob": "1992-03-10",
8+
"address": "101 Maple St, Springfield, USA"
99
},
1010
{
11-
"id": 2,
12-
"name": "Jane Smith",
13-
"email": "jane@example.com",
14-
"password": "password456",
15-
"dob": "1985-05-15",
16-
"address": "456 Elm St, Othertown, USA"
11+
"id": 2,
12+
"name": "Casey Morgan",
13+
"email": "casey.morgan@example.com",
14+
"password": "myPassword456",
15+
"dob": "1988-07-22",
16+
"address": "202 Oak Ave, Rivertown, USA"
17+
},
18+
{
19+
"id": 3,
20+
"name": "Jordan Lee",
21+
"email": "jordan.lee@example.com",
22+
"password": "pass@789",
23+
"dob": "1995-12-05",
24+
"address": "303 Pine Ln, Brooksville, USA"
25+
},
26+
{
27+
"id": 4,
28+
"name": "Taylor Quinn",
29+
"email": "taylor.quinn@example.com",
30+
"password": "Taylor#1234",
31+
"dob": "1990-11-18",
32+
"address": "404 Birch Rd, Forestville, USA"
33+
},
34+
{
35+
"id": 5,
36+
"name": "Riley Blake",
37+
"email": "riley.blake@example.com",
38+
"password": "Riley$5678",
39+
"dob": "1987-08-30",
40+
"address": "505 Cedar Ct, Lakeside, USA"
1741
}
1842
]

0 commit comments

Comments
 (0)