Leaf and Kettle's tea list, app/teas/page.tsx, keeps its filter, its order, and its page in the URL, so /teas?type=green&sort=price&page=2 always shows the same thing. Next.js gives the page searchParams, a promise of a plain object: each value is a string, an array of strings when the URL has the param twice (?sort=price&sort=name), or undefined when it's missing. Load the teas with getTeas() from @/lib/teas.
type keeps only the teas of that type, one of TEA_TYPES: green, black, oolong, or herbal. sort is price (cheapest first) or name (A to Z); without it, the teas keep the shop's order. page picks the page, four teas to a page, starting at 1.
Anyone can type anything into a URL, so never fail on a bad value. An unknown type or sort is ignored. A page that isn't a whole number from 1 up (abc, 0, -1, 1.5) means page 1, and a page past the last one means the last page. A param given twice counts with its first value.
Show each tea as an <li> in a <ul>, with its name and price: Sencha: $6.00. Under the list, show Page 2 of 3, then Previous and Next links (with next/link) to the pages on either side, with no Previous on the first page and no Next on the last.
The links keep the type and sort the page is using, so a bad one isn't passed on. Write their params in the order type, sort, page, and leave out page for page 1: /teas?sort=price, not /teas?sort=price&page=1.
/teas?type=green&sort=price&page=2
→ Gyokuro: $10.00
Page 2 of 2
Previous → /teas?type=green&sort=price
lib/teas.ts, read-only. Import it as @/lib/teas.
export type TeaType = "green" | "black" | "oolong" | "herbal";
export type Tea = { slug: string; name: string; type: TeaType; price: number };
export const TEA_TYPES: TeaType[] = ["green", "black", "oolong", "herbal"];
const teas: Tea[] = [
{ slug: "sencha", name: "Sencha", type: "green", price: 6 },
{ slug: "earl-grey", name: "Earl Grey", type: "black", price: 5 },
{ slug: "mint", name: "Mint", type: "herbal", price: 3 },
{ slug: "jasmine", name: "Jasmine", type: "green", price: 5.5 },
{ slug: "assam", name: "Assam", type: "black", price: 4 },
{ slug: "tie-guan-yin", name: "Tie Guan Yin", type: "oolong", price: 8 },
{ slug: "chamomile", name: "Chamomile", type: "herbal", price: 3.5 },
{ slug: "matcha", name: "Matcha", type: "green", price: 9 },
{ slug: "dragon-well", name: "Dragon Well", type: "green", price: 7.5 },
{ slug: "rooibos", name: "Rooibos", type: "herbal", price: 4.5 },
{ slug: "darjeeling", name: "Darjeeling", type: "black", price: 7 },
{ slug: "gyokuro", name: "Gyokuro", type: "green", price: 10 },
];
/** Every tea, featured first: the shop's own order. */
export async function getTeas(): Promise<Tea[]> {
return teas;
}
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page searchParams={params({})} />);
expect(listed()).toEqual(["Sencha: $6.00", "Earl Grey: $5.00", "Mint: $3.00", "Jasmine: $5.50"]);
expect(screen.getByText("Page 1 of 3")).toBeInTheDocument();
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page searchParams={params({ type: "herbal", sort: "price" })} />);
expect(listed()).toEqual(["Mint: $3.00", "Chamomile: $3.50", "Rooibos: $4.50"]);
expect(screen.getByText("Page 1 of 1")).toBeInTheDocument();
cleanup();
await renderServer(<Page searchParams={params({ sort: "name" })} />);
expect(listed()).toEqual(["Assam: $4.00", "Chamomile: $3.50", "Darjeeling: $7.00", "Dragon Well: $7.50"]);
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page searchParams={params({ page: "2" })} />);
expect(listed()).toEqual(["Assam: $4.00", "Tie Guan Yin: $8.00", "Chamomile: $3.50", "Matcha: $9.00"]);
expect(screen.getByText("Page 2 of 3")).toBeInTheDocument();
cleanup();
await renderServer(<Page searchParams={params({ type: "green", page: "2" })} />);
expect(listed()).toEqual(["Gyokuro: $10.00"]);
expect(screen.getByText("Page 2 of 2")).toBeInTheDocument();
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
for (const query of [{ page: "abc" }, { page: "0" }, { page: "-1" }, { page: "1.5" }, { page: "" }, { type: "coffee" }, { sort: "popular" }]) {
await renderServer(<Page searchParams={params(query)} />);
expect(listed()).toEqual(["Sencha: $6.00", "Earl Grey: $5.00", "Mint: $3.00", "Jasmine: $5.50"]);
expect(screen.getByText("Page 1 of 3")).toBeInTheDocument();
cleanup();
}
await renderServer(<Page searchParams={params({ page: "99" })} />);
expect(listed()).toEqual(["Dragon Well: $7.50", "Rooibos: $4.50", "Darjeeling: $7.00", "Gyokuro: $10.00"]);
expect(screen.getByText("Page 3 of 3")).toBeInTheDocument();
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page searchParams={params({ sort: ["name", "price"], page: ["2", "3"] })} />);
expect(listed()).toEqual(["Earl Grey: $5.00", "Gyokuro: $10.00", "Jasmine: $5.50", "Matcha: $9.00"]);
expect(screen.getByText("Page 2 of 3")).toBeInTheDocument();
await renderServer(<Page searchParams={params({ sort: "price", page: "2" })} />);
expect(screen.getByRole("link", { name: "Previous" })).toHaveAttribute("href", "/teas?sort=price");
expect(screen.getByRole("link", { name: "Next" })).toHaveAttribute("href", "/teas?sort=price&page=3");
cleanup();
await renderServer(<Page searchParams={params({ type: "green", sort: "price" })} />);
expect(screen.queryByRole("link", { name: "Previous" })).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "Next" })).toHaveAttribute("href", "/teas?type=green&sort=price&page=2");
cleanup();
await renderServer(<Page searchParams={params({ type: "coffee", sort: "popular", page: "3" })} />);
expect(screen.getByRole("link", { name: "Previous" })).toHaveAttribute("href", "/teas?page=2");
expect(screen.queryByRole("link", { name: "Next" })).not.toBeInTheDocument();
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 Tea list filtered, sorted, and paged by the URL. 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.