Skip to the code

A steep timer on a server-rendered tea page

Each tea's page at Leaf and Kettle, app/teas/[slug]/page.tsx, is a Server Component: it loads the tea on the server, and none of its code is sent to the browser. Customers want a steep timer on it, and a timer needs state, an effect, and clicks, which only Client Components have. Leave the page as it is (it's under the statement) and write the timer in its own file, app/teas/[slug]/steep-timer.tsx, whose default export is SteepTimer. The page renders it as <SteepTimer seconds={tea.steepSeconds} teaName={tea.name} />.

Show the time left as minutes and seconds, 3:00, 2:59, and so on down to 0:00, in an element with role="timer".

A Start button starts the countdown, one second at a time. While the timer runs, the same button reads Pause and stops it, keeping the time left, and Start then goes on from there. A Reset button stops the timer and puts the full time back.

At 0:00 the timer stops and shows Your Oolong tea is ready. (with the tea's name) in an element with role="status", and Start stays disabled until Reset.

When the timer leaves the page, nothing of it keeps running.

<SteepTimer seconds={180} teaName="Oolong tea" />
→ 3:00 [Start] [Reset]
Start, then 3 seconds → 2:57 [Pause] [Reset]
3 minutes after Start → 0:00, Your Oolong tea is ready. [Start, disabled] [Reset]

The app's other files

lib/teas.ts, read-only. Import it as @/lib/teas.

export type Tea = { slug: string; name: string; temperature: number; steepSeconds: number };
 
const teas: Tea[] = [
  { slug: "green", name: "Green tea", temperature: 80, steepSeconds: 120 },
  { slug: "oolong", name: "Oolong tea", temperature: 90, steepSeconds: 180 },
  { slug: "mint", name: "Mint tea", temperature: 100, steepSeconds: 300 },
];
 
/** The tea with this slug, or undefined when the shop doesn't sell it. */
export async function getTea(slug: string): Promise<Tea | undefined> {
  return teas.find((tea) => tea.slug === slug);
}

app/teas/[slug]/page.tsx, read-only. Import it as @/app/teas/[slug]/page.

import { notFound } from "next/navigation";
import { getTea } from "@/lib/teas";
import SteepTimer from "./steep-timer";
 
export default async function TeaPage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const tea = await getTea(slug);
  if (!tea) notFound();
 
  return (
    <main>
      <h1>{tea.name}</h1>
      <p>Steep at {tea.temperature}°C.</p>
      <SteepTimer seconds={tea.steepSeconds} teaName={tea.name} />
    </main>
  );
}

What the tests check

  1. is a Client Component, so the server-rendered tea page can show it
    const renderOnServer = async (element, options) => {
      try {
        return await renderServer(element, options);
      } catch (error) {
        if (/reading 'use[A-Za-z]*'/.test(String(error && error.message))) {
          throw new Error('A component that calls hooks ran as a Server Component, where there are none. Does its file start with "use client"? (' + error.message + ")");
        }
        throw error;
      }
    };
    await renderOnServer(<Page params={params({ slug: "oolong" })} />);
    expect(screen.getByRole("heading", { level: 1, name: "Oolong tea" })).toBeInTheDocument();
    expect(screen.getByRole("timer")).toHaveTextContent("3:00");
    expect(screen.getByRole("button", { name: "Start" })).toBeEnabled();
    expect(screen.getByRole("button", { name: "Reset" })).toBeInTheDocument();
    
  2. counts down one second at a time after Start
    const clock = useFakeTimers();
    await renderServer(<Page params={params({ slug: "green" })} />);
    expect(screen.getByRole("timer")).toHaveTextContent("2:00");
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(1000);
    expect(screen.getByRole("timer")).toHaveTextContent("1:59");
    await clock.advance(2000);
    expect(screen.getByRole("timer")).toHaveTextContent("1:57");
    expect(screen.getByRole("button", { name: "Pause" })).toBeInTheDocument();
    await clock.advance(60000);
    expect(screen.getByRole("timer")).toHaveTextContent("0:57");
    
  3. Pause keeps the time left, and Start goes on from there
    const clock = useFakeTimers();
    render(<SteepTimer seconds={120} teaName="Green tea" />);
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(2000);
    await user.click(screen.getByRole("button", { name: "Pause" }));
    await clock.advance(5000);
    expect(screen.getByRole("timer")).toHaveTextContent("1:58");
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(3000);
    expect(screen.getByRole("timer")).toHaveTextContent("1:55");
    
  4. stops at 0:00 and says the tea is ready
    const clock = useFakeTimers();
    render(<SteepTimer seconds={3} teaName="Mint tea" />);
    expect(screen.queryByText("Your Mint tea is ready.")).not.toBeInTheDocument();
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(3000);
    expect(screen.getByRole("timer")).toHaveTextContent("0:00");
    expect(screen.getByRole("status")).toHaveTextContent("Your Mint tea is ready.");
    await clock.advance(5000);
    expect(screen.getByRole("timer")).toHaveTextContent("0:00");
    expect(screen.getByRole("button", { name: "Start" })).toBeDisabled();
    expect(clock.pending()).toBe(0);
    
  5. Reset stops the timer and puts the full time back
    const clock = useFakeTimers();
    render(<SteepTimer seconds={120} teaName="Green tea" />);
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(5000);
    await user.click(screen.getByRole("button", { name: "Reset" }));
    expect(screen.getByRole("timer")).toHaveTextContent("2:00");
    await clock.advance(3000);
    expect(screen.getByRole("timer")).toHaveTextContent("2:00");
    expect(screen.getByRole("button", { name: "Start" })).toBeEnabled();
    cleanup();
    render(<SteepTimer seconds={2} teaName="Green tea" />);
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(2000);
    await user.click(screen.getByRole("button", { name: "Reset" }));
    expect(screen.getByRole("timer")).toHaveTextContent("0:02");
    expect(screen.getByRole("button", { name: "Start" })).toBeEnabled();
    expect(screen.queryByText("Your Green tea is ready.")).not.toBeInTheDocument();
    
  6. leaves no timer running when it's removed
    const clock = useFakeTimers();
    const { unmount } = render(<SteepTimer seconds={120} teaName="Green tea" />);
    await user.click(screen.getByRole("button", { name: "Start" }));
    await clock.advance(1000);
    unmount();
    expect(clock.pending()).toBe(0);
    

Learn more

app/teas/[slug]/steep-timer.tsxSaved in this browser

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).