Skip to content

Commit e4aa0df

Browse files
committed
feat: add test for the user card component
1 parent d3c774f commit e4aa0df

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

__mocks__/fixtures/user.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export const mockUser: User = {
1212
uid: "uuid-uuido-232-dex0232-2331",
1313
joined: "14 days ago",
1414
disabled: false,
15-
reputation: 0,
16-
username: "",
15+
reputation: 2,
16+
username: "Rouven",
1717
lastName: "",
1818
emailVerified: false,
1919
email: "",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import UserCard from "@/components/cards/User";
2+
import { renderWithRedux } from "../../../__mocks__/renderWithRedux";
3+
import { mockUser } from "../../../__mocks__/fixtures/user";
4+
import { fireEvent, screen } from "@testing-library/react";
5+
import "@testing-library/jest-dom";
6+
import { useRouter } from "next/router";
7+
8+
jest.mock("next/router", () => ({
9+
useRouter: jest.fn(),
10+
}));
11+
const teamMembers = [mockUser];
12+
const handleClick = jest.fn(() => {});
13+
const mockPush = jest.fn();
14+
const mockUseRouter = {
15+
locale: "en",
16+
push: mockPush,
17+
};
18+
(useRouter as jest.Mock).mockReturnValue(mockUseRouter);
19+
20+
describe("User card component", () => {
21+
const timestamp = {
22+
date: new Date(),
23+
text: "",
24+
};
25+
beforeEach(() => {
26+
renderWithRedux(
27+
<UserCard user={mockUser} timestamp={timestamp} teamMembers={teamMembers} onClick={handleClick} link="/testlink">
28+
<div>test link</div>
29+
</UserCard>
30+
);
31+
});
32+
// Mock window.getSelection
33+
window.getSelection = jest.fn().mockReturnValue({
34+
type: "Caret",
35+
});
36+
test("Renders the card component", () => {
37+
const teamMembers = [mockUser];
38+
expect(screen.getByTestId("userId")).toBeInTheDocument();
39+
expect(screen.getAllByTestId("avatar").length).toEqual(teamMembers.length);
40+
const linkElements = screen.getAllByRole("link");
41+
linkElements.forEach((element, index) => {
42+
if (teamMembers[index]) {
43+
const expectedHref = `/profile/${teamMembers[index].username}`;
44+
expect(element).toHaveAttribute("href", expectedHref);
45+
}
46+
});
47+
});
48+
49+
test("Renders the correct tag and currecy when teamMembers is not empty", () => {
50+
expect(screen.getByTestId("currencyId")).toBeInTheDocument();
51+
expect(screen.getByTestId("tag")).toBeInTheDocument();
52+
});
53+
54+
test("Calls onClock prop when clicked", () => {
55+
fireEvent.click(screen.getByTestId("userId"));
56+
expect(handleClick).toHaveBeenCalled();
57+
});
58+
test("Navigates to the correct link when clicked", () => {
59+
const element = screen.getByText("test link");
60+
fireEvent.click(element);
61+
expect(mockPush).toHaveBeenCalledWith("/testlink");
62+
});
63+
});

src/components/cards/User.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface UserProps {
3333
submission?: Submission;
3434
teamMembers?: User[];
3535
onClick?: () => void;
36+
testId?: string
3637
}
3738

3839
/**
@@ -51,7 +52,7 @@ interface UserProps {
5152
}
5253
* @returns {ReactElement}
5354
*/
54-
export default function UserCard({ boxLayout, link, bordered, expanded, user, badge = "", timestamp, children, className, teamMembers, onClick }: UserProps): ReactElement {
55+
export default function UserCard({ boxLayout, link, bordered, expanded, user, badge = "", timestamp, children, className, teamMembers, onClick, testId="userId" }: UserProps): ReactElement {
5556
const { locale, push } = useRouter();
5657
const colors = useSelector((state) => state.ui.colors);
5758

@@ -77,7 +78,7 @@ export default function UserCard({ boxLayout, link, bordered, expanded, user, ba
7778
"cursor-pointer": link,
7879
});
7980
return (
80-
<div className={userCardClassName} onClick={handleClick}>
81+
<div data-testid={testId} className={userCardClassName} onClick={handleClick}>
8182
<div className={`z-10 ${boxLayout ? "relative flex-none" : "absolute top-0 left-0"}`}>
8283
{teamMembers && teamMembers?.length ? (
8384
<div className="w-15 h-15 rounded-full bg-gray-800 overflow-hidden grid grid-cols-2 items-between">

0 commit comments

Comments
 (0)