Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import AchievementLinkField from "@/components/sections/profile/achievements/LinkField";
import "@testing-library/jest-dom";
import { fireEvent, render, screen } from "@testing-library/react";

describe("AchievementLinkField", () => {
const testLink = "https://dacade.org/some/path";
it("should render the achievement link", () => {
render(<AchievementLinkField link={testLink}/>)
expect(screen.getByTestId("achievementLinkId")).toBeInTheDocument()
const linkElement = screen.getByText(testLink);
expect(linkElement).toBeInTheDocument();
expect(linkElement.textContent).toBe("https://dacade.org/some/path")
})
it("should copy the link to the clipboard when clicked", () => {
render(<AchievementLinkField link={testLink} />);
if(navigator.clipboard){
const mockCopy = async () => await navigator.clipboard.writeText(testLink)
const linkElement = screen.getByText(testLink);
fireEvent.click(linkElement);
expect(mockCopy()).toHaveBeenCalled();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correctly mock the clipboard API to ensure the test is reliable.

- if(navigator.clipboard){
-         const mockCopy = async () => await navigator.clipboard.writeText(testLink)
-         const linkElement = screen.getByText(testLink);
-         fireEvent.click(linkElement);
-         expect(mockCopy()).toHaveBeenCalled();
- }
+ jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(() => Promise.resolve());
+ const linkElement = screen.getByText(testLink);
+ fireEvent.click(linkElement);
+ expect(navigator.clipboard.writeText).toHaveBeenCalledWith(testLink);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it("should copy the link to the clipboard when clicked", () => {
render(<AchievementLinkField link={testLink} />);
if(navigator.clipboard){
const mockCopy = async () => await navigator.clipboard.writeText(testLink)
const linkElement = screen.getByText(testLink);
fireEvent.click(linkElement);
expect(mockCopy()).toHaveBeenCalled();
}
it("should copy the link to the clipboard when clicked", () => {
render(<AchievementLinkField link={testLink} />);
jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(() => Promise.resolve());
const linkElement = screen.getByText(testLink);
fireEvent.click(linkElement);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(testLink);

});
it("should have the link button", () => {
render(<AchievementLinkField link={testLink} />);
const mockPathname = testLink.split("https://dacade.org/")[1];
expect(screen.getByRole("link")).toBeInTheDocument()
expect(screen.getByRole("button")).toBeInTheDocument()
expect(screen.getByRole("link").hasAttribute("href")).toBeTruthy()
if(typeof window !== "undefined"){
expect(screen.getByRole("link").getAttribute("href")).toBe(`${window.location.origin}/${mockPathname}`)
}
})
})
5 changes: 3 additions & 2 deletions src/components/sections/profile/achievements/LinkField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ReactElement, useEffect, useState } from "react";
*/
interface AchievementLinkFieldProps {
link: string | null;
testId?: string
}

/**
Expand All @@ -23,7 +24,7 @@ interface AchievementLinkFieldProps {
}
* @returns {*}
*/
export default function AchievementLinkField({ link }: AchievementLinkFieldProps): ReactElement {
export default function AchievementLinkField({ link, testId = "achievementLinkId" }: AchievementLinkFieldProps): ReactElement {
const { t } = useTranslation();
const [fullLink, setFullink] = useState("");

Expand All @@ -37,7 +38,7 @@ export default function AchievementLinkField({ link }: AchievementLinkFieldProps
const copy = () => navigator.clipboard.writeText(link as string);

return (
<div className="border relative p-2 rounded">
<div data-testid={testId} className="border relative p-2 rounded">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add keyboard accessibility to the clickable text element.

- <p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy}>
+ <p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy} onKeyUp={copy} tabIndex="0">

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
<div data-testid={testId} className="border relative p-2 rounded">
<div data-testid={testId} className="border relative p-2 rounded">
<p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy} onKeyUp={copy} tabIndex="0">

<p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy}>
{link}
</p>
Expand Down