|
| 1 | +import React from "react" |
| 2 | +import { render, screen } from "@testing-library/react" |
| 3 | +import { MemoryRouter } from "react-router-dom" |
| 4 | +import { MockedProvider } from "@apollo/client/testing" |
| 5 | +import { vi } from "vitest" |
| 6 | +import { SingleContributorDisplay } from "../contributor" |
| 7 | + |
| 8 | +// --- Mock Dependencies --- |
| 9 | +vi.mock("../../queries/user", () => ({ |
| 10 | + useUser: vi.fn(() => ({ |
| 11 | + user: null, |
| 12 | + loading: false, |
| 13 | + error: undefined, |
| 14 | + })), |
| 15 | +})) |
| 16 | + |
| 17 | +import { useUser } from "../../queries/user" |
| 18 | + |
| 19 | +vi.mock("../../assets/ORCIDiD_iconvector.svg", () => ({ |
| 20 | + default: "mock-orcid-logo.svg", |
| 21 | +})) |
| 22 | + |
| 23 | +describe("SingleContributorDisplay - Basic Loading", () => { |
| 24 | + const renderComponent = (props: { |
| 25 | + contributor: { name?: string; orcid?: string | null } |
| 26 | + isLast?: boolean |
| 27 | + separator?: React.ReactNode |
| 28 | + }) => { |
| 29 | + return render( |
| 30 | + <MemoryRouter> |
| 31 | + <MockedProvider> |
| 32 | + <SingleContributorDisplay |
| 33 | + contributor={props.contributor} |
| 34 | + isLast={props.isLast ?? true} |
| 35 | + separator={props.separator ?? null} |
| 36 | + /> |
| 37 | + </MockedProvider> |
| 38 | + </MemoryRouter>, |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + vi.clearAllMocks() |
| 44 | + vi.mocked(useUser).mockImplementation((userId) => { |
| 45 | + if (userId) { |
| 46 | + return { |
| 47 | + user: { id: userId, name: `Mock User ${userId}` }, |
| 48 | + loading: false, |
| 49 | + error: undefined, |
| 50 | + } |
| 51 | + } |
| 52 | + return { user: null, loading: false, error: undefined } |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it("renders the component and displays the contributor name", async () => { |
| 57 | + const contributor = { name: "Jane Doe", orcid: null } |
| 58 | + renderComponent({ contributor }) |
| 59 | + expect(screen.getByText("Jane Doe")).toBeInTheDocument() |
| 60 | + }) |
| 61 | + |
| 62 | + it("renders 'Unknown Contributor' if the contributor name is missing", async () => { |
| 63 | + const contributor = { name: undefined, orcid: null } |
| 64 | + renderComponent({ contributor }) |
| 65 | + expect(screen.getByText("Unknown Contributor")).toBeInTheDocument() |
| 66 | + }) |
| 67 | + |
| 68 | + it("renders the component and displays the ORCID link if an ID is provided", async () => { |
| 69 | + const testOrcid = "0000-0000-0000-0000" |
| 70 | + const contributor = { name: "Author With ORCID", orcid: testOrcid } |
| 71 | + renderComponent({ contributor }) |
| 72 | + const orcidLink = screen.getByLabelText( |
| 73 | + `ORCID profile for ${contributor.name}`, |
| 74 | + ) |
| 75 | + expect(orcidLink).toBeInTheDocument() |
| 76 | + expect(orcidLink).toHaveAttribute( |
| 77 | + "href", |
| 78 | + expect.stringContaining(testOrcid), |
| 79 | + ) |
| 80 | + }) |
| 81 | +}) |
0 commit comments