Skip to content

Commit

Permalink
fix wouter link to prevent crash without href/to
Browse files Browse the repository at this point in the history
  • Loading branch information
dswbx authored and molefrog committed Aug 28, 2024
1 parent ab46b00 commit 4162953
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/wouter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ export const Link = forwardRef((props, ref) => {

// handle nested routers and absolute paths
const href = router.hrefs(
targetPath[0] === "~" ? targetPath.slice(1) : router.base + targetPath,
(targetPath ?? "")[0] === "~"
? targetPath.slice(1)
: router.base + targetPath,
router // pass router as a second argument for convinience
);

Expand Down
13 changes: 13 additions & 0 deletions packages/wouter/test/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,17 @@ describe("<Link /> with `asChild` prop", () => {
expect(link).toHaveAttribute("href", "/about");
expect(link).toHaveTextContent("Click Me");
});

it("missing href or to won't crash", () => {
const { getByText } = render(
/* @ts-expect-error */
<Link>Click Me</Link>
);

const link = getByText("Click Me");

expect(link.tagName).toBe("A");
expect(link).toHaveAttribute("href", undefined);
expect(link).toHaveTextContent("Click Me");
});
});

0 comments on commit 4162953

Please sign in to comment.