小编Fab*_*bio的帖子

在 Next.js 中模拟 next/与 Jest 链接

如何在 Next JS 中使用 jest 模拟“next/link”中的 Link 组件,并在单击链接元素后检查重定向路径?

这是带有测试的示例组件:

import Link from "next/link";

export default function Nav() {
  return (
    <div>
      <Link href="/login">
        <span>Click here</span>
      </Link>
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)
describe("Nav", () => {
  it("redirects correctly", async () => {
  render(<Nav />)

  fireEvent.click(screen.getByText("Click here"));

  // check if redirect to href prop works as expected

  });
});
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs next.js react-testing-library

5
推荐指数
1
解决办法
6271
查看次数

标签 统计

jestjs ×1

next.js ×1

react-testing-library ×1

reactjs ×1