|
| 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 | +}); |
0 commit comments