Leaf and Kettle's tea journal lets visitors write tasting notes at /journal/new, and losing a long note to a stray click hurts. Build the form, warning before the visitor leaves with words that aren't saved: app/journal/new/note-form.tsx, whose default export is NoteForm, a Client Component.
Render a <form> with a textarea labelled Tasting note and a Save note submit button, then two next/link links: Cancel to /journal and Brewing guides to /guides. Submitting calls saveNote(text) from @/lib/journal with the text trimmed, then empties the box and shows Saved.
The note is unsaved while the box holds anything but spaces. While it is, a click on either link asks window.confirm("Leave this page? Your note isn't saved.") and stays on the form unless the visitor answers OK. Use Link's onNavigate, which runs only when the link is about to navigate inside the app, so a Ctrl or Cmd click, which opens the link in a new tab and leaves this page alone, never asks. With nothing unsaved, the links never ask.
Links aren't the only way out: reloading or closing the tab skips them. While the note is unsaved, also cancel the window's beforeunload event with event.preventDefault(), so the browser asks its own question, and stop as soon as the note is saved or emptied, or the form is gone.
You type "Malty, with a honey finish." and click Cancel
→ window.confirm("Leave this page? Your note isn't saved.")
Cancel: you stay on /journal/new, the note still there
OK: you go to /journal
lib/journal.ts, read-only. Import it as @/lib/journal.
/** Every note saved so far. A real shop keeps them in its database. */
export const notes: string[] = [];
/** Saves a tasting note to the visitor's journal. */
export async function saveNote(text: string) {
notes.push(text);
}
const note = () => screen.getByLabelText("Tasting note");
window.confirm = spy(() => false);
renderClient(<NoteForm />, { url: "/journal/new" });
await user.click(screen.getByRole("link", { name: "Brewing guides" }));
expect(router.url).toBe("/guides");
await user.type(note(), " ");
await user.click(screen.getByRole("link", { name: "Cancel" }));
expect(router.url).toBe("/journal");
expect(window.confirm).not.toHaveBeenCalled();
const note = () => screen.getByLabelText("Tasting note");
window.confirm = spy(() => false);
renderClient(<NoteForm />, { url: "/journal/new" });
await user.type(note(), "Malty, with a honey finish.");
await user.click(screen.getByRole("link", { name: "Cancel" }));
expect(window.confirm).toHaveBeenCalledWith("Leave this page? Your note isn't saved.");
expect(router.url).toBe("/journal/new");
expect(router.calls).toEqual([]);
expect(note()).toHaveValue("Malty, with a honey finish.");
const note = () => screen.getByLabelText("Tasting note");
window.confirm = spy(() => true);
renderClient(<NoteForm />, { url: "/journal/new" });
await user.type(note(), "Malty.");
await user.click(screen.getByRole("link", { name: "Brewing guides" }));
expect(window.confirm).toHaveBeenCalledTimes(1);
expect(router.url).toBe("/guides");
const note = () => screen.getByLabelText("Tasting note");
window.confirm = spy(() => false);
renderClient(<NoteForm />, { url: "/journal/new" });
await user.type(note(), "Smoky.");
fireEvent.click(screen.getByRole("link", { name: "Brewing guides" }), { ctrlKey: true });
fireEvent.click(screen.getByRole("link", { name: "Brewing guides" }), { metaKey: true });
expect(window.confirm).not.toHaveBeenCalled();
expect(router.calls).toEqual([]);
const note = () => screen.getByLabelText("Tasting note");
window.confirm = spy(() => false);
renderClient(<NoteForm />, { url: "/journal/new" });
await user.type(note(), " Grassy and bright. ");
await user.click(screen.getByRole("button", { name: "Save note" }));
expect(await screen.findByText("Saved.")).toBeInTheDocument();
expect(notes).toEqual(["Grassy and bright."]);
expect(note()).toHaveValue("");
await user.click(screen.getByRole("link", { name: "Cancel" }));
expect(window.confirm).not.toHaveBeenCalled();
expect(router.url).toBe("/journal");
const note = () => screen.getByLabelText("Tasting note");
const unloadIsCancelled = () => {
const event = new Event("beforeunload", { cancelable: true });
window.dispatchEvent(event);
return event.defaultPrevented;
};
const { unmount } = renderClient(<NoteForm />, { url: "/journal/new" });
expect(unloadIsCancelled()).toBe(false);
await user.type(note(), "Grassy.");
expect(unloadIsCancelled()).toBe(true);
await user.clear(note());
expect(unloadIsCancelled()).toBe(false);
await user.type(note(), "Grassy.");
unmount();
expect(unloadIsCancelled()).toBe(false);
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 Warn before leaving an unsaved note. 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.