Skip to content

Commit

Permalink
refactor(shared): breadcrumb component
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao committed Nov 10, 2024
1 parent cd273b3 commit 799d103
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 82 deletions.
25 changes: 24 additions & 1 deletion components/shared/Breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import { Children } from "react";
import Link from "next/link";
import { cn } from "@/lib/utils";
import BreadcrumbItem, { BreadcrumbItemProps } from "./BreadcrumbItem";
import Icon from "../Icon";

export interface BreadcrumbItemProps {
/** The displayed text of the breadcrumb item */
text: string;
/** The href of the breadcrumb item */
href?: string;
/** A CSS class name for styling the breadcrumb item */
className?: string;
}

export const BreadcrumbItem = ({
text,
href,
className,
}: BreadcrumbItemProps) => {
return href ? (
<Link href={href} className={className}>
{text}
</Link>
) : (
<span className={className}>{text}</span>
);
};

export interface BreadcrumbProps {
/** `Breadcrumb.Item` with text and href */
children: React.ReactNode;
Expand Down
22 changes: 0 additions & 22 deletions components/shared/Breadcrumb/BreadcrumbItem.tsx

This file was deleted.

24 changes: 6 additions & 18 deletions components/shared/Breadcrumb/breadcrumb.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { cleanup, render, screen } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import Breadcrumb from "./Breadcrumb";

jest.mock("next/router", () => ({
Expand All @@ -20,18 +20,8 @@ describe("Breadcrumb", () => {
);

expect(screen.getByRole("navigation")).toBeInTheDocument();
});

it("renders separator correctly", () => {
render(
<Breadcrumb>
<Breadcrumb.Item text="Home" href="/" />
<Breadcrumb.Item text="About" href="/about" />
</Breadcrumb>
);

const separator = screen.getAllByText(">");
expect(separator).toHaveLength(1);
expect(screen.getByText("Home")).toBeInTheDocument();
expect(screen.getByText("About")).toBeInTheDocument();
});

it("renders with custom separator correctly", () => {
Expand All @@ -47,17 +37,15 @@ describe("Breadcrumb", () => {
});

it("renders with ReactNode separator correctly", () => {
const separator = <span style={{ color: "red" }}>***</span>;
render(
<Breadcrumb separator={separator}>
<Breadcrumb separator={<span>***</span>}>
<Breadcrumb.Item text="Home" href="/" />
<Breadcrumb.Item text="About" href="/about" />
</Breadcrumb>
);

const separatorElement = screen.getByText("***");
expect(separatorElement).toBeInTheDocument();
expect(separatorElement).toHaveStyle("color: red");
const separator = screen.getByText("***");
expect(separator).toBeInTheDocument();
});

it("renders breadcrumb items correctly", () => {
Expand Down
37 changes: 0 additions & 37 deletions components/shared/Breadcrumb/breadcrumbItem.test.tsx

This file was deleted.

5 changes: 1 addition & 4 deletions components/shared/Breadcrumb/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import Breadcrumb from "./Breadcrumb";

export { default as Breadcrumb } from "./Breadcrumb";
export * from "./Breadcrumb";

export default Breadcrumb;

0 comments on commit 799d103

Please sign in to comment.