Leaf and Kettle's header, in the root layout, links to the shop's sections and shows the visitor where they are. Build its navigation: app/components/site-nav.tsx, whose default export is SiteNav. Render a <nav aria-label="Main"> with a next/link Link for each section, in this order: Home to /, Teas to /teas, Teaware to /teaware, and Journal to /journal.
A Server Component can't read the current URL, so SiteNav is a Client Component that gets the path from usePathname(), and the layout renders it. Mark each link by where the visitor is:
On the link's own page, give it aria-current="page" and the class active.
On a page inside its section, give it aria-current="true" and the class active: Teas on /teas/green. Only whole segments count, so /teaspoons isn't inside /teas, and Home is only marked on / itself.
Anywhere else, leave out aria-current and the active class.
/teas/green
→ <nav aria-label="Main">
<a href="/">Home</a>
<a href="/teas" aria-current="true" class="active">Teas</a>
<a href="/teaware">Teaware</a>
<a href="/journal">Journal</a>
</nav>
const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
renderClient(<SiteNav />, { url: "/teas" });
expect(screen.getByRole("navigation", { name: "Main" })).toBeInTheDocument();
expect(screen.getAllByRole("link").map((item) => item.getAttribute("href"))).toEqual(["/", "/teas", "/teaware", "/journal"]);
expect(marks()).toEqual([
["Home", null, false],
["Teas", "page", true],
["Teaware", null, false],
["Journal", null, false],
]);
const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
renderClient(<SiteNav />, { url: "/teas/green" });
expect(marks()).toEqual([
["Home", null, false],
["Teas", "true", true],
["Teaware", null, false],
["Journal", null, false],
]);
cleanup();
renderClient(<SiteNav />, { url: "/journal/first-flush/notes" });
expect(marks()[3]).toEqual(["Journal", "true", true]);
const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
renderClient(<SiteNav />, { url: "/teaspoons" });
expect(marks().filter(([, current, active]) => current !== null || active)).toEqual([]);
cleanup();
renderClient(<SiteNav />, { url: "/" });
expect(marks()).toEqual([
["Home", "page", true],
["Teas", null, false],
["Teaware", null, false],
["Journal", null, false],
]);
const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
renderClient(<SiteNav />, { url: "/teaware?sort=price" });
expect(marks()[2]).toEqual(["Teaware", "page", true]);
const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
renderClient(<SiteNav />, { url: "/" });
await user.click(screen.getByRole("link", { name: "Journal" }));
expect(router.url).toBe("/journal");
expect(marks()).toEqual([
["Home", null, false],
["Teas", null, false],
["Teaware", null, false],
["Journal", "page", true],
]);
const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
function RootLayout({ children }) {
return (
<div>
<header>
<SiteNav />
</header>
<main>{children}</main>
</div>
);
}
await renderServer(<RootLayout><h1>Kettles</h1></RootLayout>, { url: "/teaware/kettles" });
expect(screen.getByRole("heading", { name: "Kettles" })).toBeInTheDocument();
expect(marks()[2]).toEqual(["Teaware", "true", true]);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Nav links that show where you are. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.