ํ ์คํธ ๋์ ๊ณ ๊ตฐ๋ถํฌ ์ค..
ํ ์คํธ ์ปดํฌ๋ํธ๋ ์ ๋์๊ฐ๋๋ฐ ํ ์คํธ๋ฅผ ๊ฐ์ธ์ฃผ๋ ๋งํฌ ์ปดํฌ๋ํธ๋ ํ ์คํธ ์์ฒด๊ฐ ์๋์๋ค.
๊ทธ๋ฌ๋ค UnstyledLink ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ณด๋ next/link ์ฌ์ฉ๋๋ฌธ์ธ๊ฐ ํด์ ๊ฒ์์ ํ๋ mock์ ์ถ๊ฐํด์ค์ผ ํ๋ค.
๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์ถ๊ฐํ๋๋ ์ ๋์๊ฐ๋ค.
import "@testing-library/jest-dom";
import "@testing-library/jest-dom/jest-globals";
import { render, screen } from "@testing-library/react";
import UnstyledLink, { LinkProps } from "../_link";
////////////////////////////////////// ๐ ์ถ๊ฐ //////////////////////////////////////
jest.mock("next/link", () => {
const { cloneElement } = jest.requireActual("react");
return ({ children, ...rest }: LinkProps) =>
cloneElement(children, { ...rest });
});
////////////////////////////////////// ๐ ์ถ๊ฐ //////////////////////////////////////
it("link has '๋ก๋ณถ์ด'", () => {
render(<UnstyledLink href="#">๋ก๋ณถ์ด</UnstyledLink>);
screen.debug();
expect(screen.getByText("๋ก๋ณถ์ด")).toBeInTheDocument();
});
๐ References
https://stackoverflow.com/questions/71254835/jest-testing-link-next-link