Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sonar.projectKey=amrabed.github.io
sonar.projectName=amrabed.github.io
sonar.sources=src
sonar.tests=src
sonar.exclusions=**/*.test.ts,**/*.test.tsx
sonar.test.inclusions=**/*.test.ts,**/*.test.tsx
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.typescript.lcov.reportPaths=coverage/lcov.info
Expand Down
63 changes: 61 additions & 2 deletions src/components/publication.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";

import { render } from "@testing-library/react";
import { render, fireEvent, act } from "@testing-library/react";

import publications from "@/data/publications";

import PublicationCard from "./publication";

describe("PublicationCard", () => {
beforeEach(() => {
// Mock navigator.clipboard.writeText
const mockClipboard = {
writeText: vi.fn().mockResolvedValue(undefined),
};
Object.defineProperty(globalThis, "navigator", {
value: { clipboard: mockClipboard },
writable: true,
});
vi.useFakeTimers();
});

it("renders correctly", () => {
if (publications.length > 0) {
const { getByText } = render(
Expand All @@ -15,4 +27,51 @@ describe("PublicationCard", () => {
expect(getByText(publications[0].title)).toBeInTheDocument();
}
});

it("renders a publication with a DOI link correctly", () => {
const pubWithDoi = publications.find(p => p.links && p.links.doi);
if (pubWithDoi) {
const { getByLabelText } = render(
<PublicationCard publication={pubWithDoi} />,
);
expect(getByLabelText("DOI")).toBeInTheDocument();
}
});

it("triggers and copies BibTeX successfully with visual feedback", async () => {
if (publications.length > 0) {
const { getByLabelText, getByText, queryByText } = render(
<PublicationCard publication={publications[0]} />,
);

// Open Popover
const citeBtn = getByLabelText("Cite");
fireEvent.click(citeBtn);

// Verify Popover content displays and the copy button is present
const copyBtnBefore = getByLabelText("Copy BibTeX to clipboard");
expect(copyBtnBefore).toBeInTheDocument();

// Click the Copy BibTeX button
fireEvent.click(copyBtnBefore);

// Should show the copied checkmark, "Copied!" tooltip/state, and "Copied" text
expect(navigator.clipboard.writeText).toHaveBeenCalled();

const copyBtnAfter = getByLabelText("BibTeX copied");
expect(copyBtnAfter).toBeInTheDocument();

const copiedText = getByText("Copied");
expect(copiedText).toBeInTheDocument();

// Fast-forward timers by 2 seconds
act(() => {
vi.advanceTimersByTime(2000);
});

// The button state should reset back to "Copy BibTeX to clipboard"
expect(getByLabelText("Copy BibTeX to clipboard")).toBeInTheDocument();
expect(queryByText("Copied")).not.toBeInTheDocument();
}
});
});
12 changes: 8 additions & 4 deletions src/components/publication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const CiteButton = ({ publication }: { publication: Publication }) => {
>
<div className="flex flex-row justify-end text-muted-foreground p-2">
{copied && (
<span className="flex flex-row text-sm font-medium p-2 gap-1">
<span className="flex flex-row text-sm font-medium p-2 gap-1 animate-pulse">
<FaCheck className="size-4 text-green-500" /> Copied
</span>
)}
Expand All @@ -63,16 +63,20 @@ const CiteButton = ({ publication }: { publication: Publication }) => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}}
aria-label="Copy BibTeX to clipboard"
aria-label={copied ? "BibTeX copied" : "Copy BibTeX to clipboard"}
isIconOnly
className="text-muted-foreground hover:text-primary transition-colors"
>
<FaCopy className="size-4" />
{copied ? (
<FaCheck className="size-4 text-green-500" />
) : (
<FaCopy className="size-4" />
)}
</Button>
</Tooltip.Trigger>
<Tooltip.Content>
<Tooltip.Arrow />
Copy to clipboard
{copied ? "Copied!" : "Copy to clipboard"}
</Tooltip.Content>
</Tooltip>
</div>
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default defineConfig({
"src/components/featured-section-container.tsx",
"src/components/header.tsx",
"src/components/chat/client.tsx",
"src/components/publication.tsx",
],
},
},
Expand Down
Loading