From 210be3243bb9e8d6c8cfa381ca98f16df892d96b Mon Sep 17 00:00:00 2001 From: Alexandra Date: Fri, 20 Nov 2026 12:30:45 +0100 Subject: [PATCH 1/9] Added walktrough functionality --- e2e/uiAccessibility.spec.ts | 12 ++ e2e/uiWalkthrough.spec.ts | 65 +++++++ frontend/src/App.tsx | 59 ++++++ frontend/src/components/header/Header.css | 36 ++++ frontend/src/components/header/Header.tsx | 17 ++ .../src/components/plantCard/PlantCard.css | 4 + frontend/src/components/sidebar/Sidebar.css | 3 + .../components/walkthrough/Walkthrough.css | 178 ++++++++++++++++++ .../components/walkthrough/Walkthrough.tsx | 161 ++++++++++++++++ .../walkthrough/walkthroughSteps.ts | 35 ++++ frontend/src/i18n/locales/en/translation.json | 41 ++++ frontend/src/i18n/locales/no/translation.json | 41 ++++ frontend/src/tests/App.test.tsx | 49 ++++- frontend/src/tests/Walkthrough.test.tsx | 131 +++++++++++++ frontend/src/tests/setup.ts | 12 ++ playwright.config.ts | 15 +- 16 files changed, 856 insertions(+), 3 deletions(-) create mode 100644 e2e/uiWalkthrough.spec.ts create mode 100644 frontend/src/components/walkthrough/Walkthrough.css create mode 100644 frontend/src/components/walkthrough/Walkthrough.tsx create mode 100644 frontend/src/components/walkthrough/walkthroughSteps.ts create mode 100644 frontend/src/tests/Walkthrough.test.tsx diff --git a/e2e/uiAccessibility.spec.ts b/e2e/uiAccessibility.spec.ts index 774d724e..aa510a68 100644 --- a/e2e/uiAccessibility.spec.ts +++ b/e2e/uiAccessibility.spec.ts @@ -78,6 +78,8 @@ const BOUNDARY_SELECTORS = [ ".add-match-button", ".reload-button", ".sidebar-button", + ".help-button", + ".walkthrough__button", ".plant-preferences button", ".plant-owner-test-option", ".sort-button", @@ -439,6 +441,16 @@ for (const theme of THEMES) { await checkAccessibility(page); }); + test("Walkthrough open", async ({ page }) => { + await page.goto(""); + await expect(page.locator(".plant-card-front")).toBeVisible(); + await setTheme(page, theme); + await page.locator(".help-button").click(); + await expect(page.getByRole("dialog")).toBeVisible(); + // The grayed out page behind is not checked, only the walkthrough + await checkAccessibility(page, ".walkthrough"); + }); + test("Sidebar open, with the language and theme buttons reachable", async ({ page, }) => { diff --git a/e2e/uiWalkthrough.spec.ts b/e2e/uiWalkthrough.spec.ts new file mode 100644 index 00000000..8d1c288e --- /dev/null +++ b/e2e/uiWalkthrough.spec.ts @@ -0,0 +1,65 @@ +import { test, expect } from "@playwright/test"; + +// The other UI tests start as users who have seen the walkthrough +// (playwright.config.ts). These start as new users, with empty storage. +test.use({ storageState: { cookies: [], origins: [] } }); + +test("the walkthrough opens for new users and not after it is closed", async ({ + page, +}) => { + await page.goto(""); + const dialog = page.getByRole("dialog"); + await expect(dialog).toBeVisible(); + await expect(page.locator(".walkthrough__progress")).toHaveText( + /1 (of|av) 7/, + ); + + await page.keyboard.press("Escape"); + await expect(dialog).toBeHidden(); + + await page.reload(); + await expect(page.locator(".plant-card-front")).toBeVisible(); + await expect(dialog).toBeHidden(); +}); + +test("the page behind is grayed out and can't be used while the walkthrough is open", async ({ + page, +}) => { + await page.goto(""); + await expect(page.getByRole("dialog")).toBeVisible(); + + const backdrop = await page + .locator(".walkthrough") + .evaluate((el) => getComputedStyle(el, "::backdrop").backgroundColor); + expect(backdrop).not.toBe("rgba(0, 0, 0, 0)"); + + // Tab never reaches the page behind. The browser may move focus to its own + // address bar after the last button, then the page reports body as focused. + for (let i = 0; i < 8; i++) { + await page.keyboard.press("Tab"); + const focusOnPageBehind = await page.evaluate(() => { + const el = document.activeElement; + return !!el && el !== document.body && !el.closest(".walkthrough"); + }); + expect(focusOnPageBehind).toBe(false); + } +}); + +test("the help button opens the walkthrough at the current page, and Go to changes page", async ({ + page, +}) => { + await page.goto(""); + await page.keyboard.press("Escape"); + + await page.goto("#/explorePlants"); + await page.locator(".help-button").click(); + await expect(page.locator(".walkthrough__progress")).toHaveText( + /3 (of|av) 7/, + ); + + await page.keyboard.press("ArrowRight"); + await page.locator(".walkthrough__go-to").click(); + + await expect(page.getByRole("dialog")).toBeHidden(); + await expect(page).toHaveURL(/#\/myMatches$/); +}); diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 37257b16..38cad29d 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -6,6 +6,29 @@ import Sidebar from "./components/sidebar/Sidebar"; import Header from "./components/header/Header"; import AppRoutes from "./AppRoutes.tsx"; import { ErrorMessageProvider } from "./context/ErrorMessageContext.tsx"; +import Walkthrough from "./components/walkthrough/Walkthrough.tsx"; +import { stepForPath } from "./components/walkthrough/walkthroughSteps.ts"; + +// Remembers that the user has seen the walkthrough, so it only opens by +// itself on the first visit +const WALKTHROUGH_SEEN_KEY = "walkthroughSeen"; + +function hasSeenWalkthrough() { + try { + return localStorage.getItem(WALKTHROUGH_SEEN_KEY) === "true"; + } catch { + // Without storage the walkthrough would open on every page load + return true; + } +} + +function markWalkthroughSeen() { + try { + localStorage.setItem(WALKTHROUGH_SEEN_KEY, "true"); + } catch { + // The walkthrough still closes, it just isn't remembered + } +} function App() { const { t } = useTranslation(); @@ -26,6 +49,33 @@ function App() { pageStartRef.current?.focus({ preventScroll: true }); }, [pathname]); + // The walkthrough opens by itself on the first visit, starting at the + // welcome step. The help button opens it at the step for the current page. + const [walkthrough, setWalkthrough] = useState(() => ({ + isOpen: !hasSeenWalkthrough(), + startStep: 0, + })); + const helpButtonRef = useRef(null); + const returnFocusToHelp = useRef(false); + + const openWalkthrough = () => { + setWalkthrough({ isOpen: true, startStep: stepForPath(pathname) }); + }; + + const closeWalkthrough = (returnFocus: boolean) => { + markWalkthroughSeen(); + returnFocusToHelp.current = returnFocus; + setWalkthrough((current) => ({ ...current, isOpen: false })); + }; + + // Focus goes back to the help button when the walkthrough closes. Done in + // an effect, since the page can't get focus while the dialog is still open. + useEffect(() => { + if (walkthrough.isOpen || !returnFocusToHelp.current) return; + returnFocusToHelp.current = false; + helpButtonRef.current?.focus(); + }, [walkthrough.isOpen]); + const sidebarRef = useRef(null); const menuButtonRef = useRef(null); @@ -82,8 +132,17 @@ function App() { menuButtonRef={menuButtonRef} isSidebarOpen={isSidebarOpen} onOpenSidebar={openSidebar} + onOpenWalkthrough={openWalkthrough} + helpButtonRef={helpButtonRef} /> + {walkthrough.isOpen && ( + + )} + {/* tabIndex -1 lets the skip link focus it, without adding it to the tab order */}
void; menuButtonRef?: Ref; + onOpenWalkthrough?: () => void; + helpButtonRef?: Ref; }; // Maps each route path to the translation key used for its title @@ -23,6 +25,8 @@ export default function Header({ isSidebarOpen, onOpenSidebar, menuButtonRef, + onOpenWalkthrough, + helpButtonRef, }: HeaderProps) { const { t } = useTranslation(); const { pathname } = useLocation(); @@ -45,6 +49,19 @@ export default function Header({

{t(titleKey)}

+ + {onOpenWalkthrough && ( + + )} ); } diff --git a/frontend/src/components/plantCard/PlantCard.css b/frontend/src/components/plantCard/PlantCard.css index 4d73fa45..e7834eb9 100644 --- a/frontend/src/components/plantCard/PlantCard.css +++ b/frontend/src/components/plantCard/PlantCard.css @@ -94,6 +94,10 @@ overflow: hidden; } +[data-theme="dark"] .plant-card-back { + border: 2px solid var(--dark-green); +} + .plant-card-front { background: var(--card-front); } diff --git a/frontend/src/components/sidebar/Sidebar.css b/frontend/src/components/sidebar/Sidebar.css index 603891af..f7848c54 100644 --- a/frontend/src/components/sidebar/Sidebar.css +++ b/frontend/src/components/sidebar/Sidebar.css @@ -80,6 +80,9 @@ .sidebar__heart { color: var(--dark-pink); } +[data-theme="dark"] .sidebar__heart { + color: var(--dark-pink); +} .close-button { display: none; diff --git a/frontend/src/components/walkthrough/Walkthrough.css b/frontend/src/components/walkthrough/Walkthrough.css new file mode 100644 index 00000000..a2d0ad73 --- /dev/null +++ b/frontend/src/components/walkthrough/Walkthrough.css @@ -0,0 +1,178 @@ +/* The pop-up box. padding is on the content, so a click on the dialog + element itself always means a click on the gray area outside the box. */ +.walkthrough { + width: min(460px, calc(100vw - 32px)); + max-height: calc(100vh - 32px); + padding: 0; + border: 1px solid var(--border-soft); + border-radius: 24px; + background-color: var(--light-background); + color: var(--text-dark); + box-shadow: 0 16px 48px rgba(0, 0, 0, 0.25); +} + +/* Grays out the page behind the walkthrough */ +.walkthrough::backdrop { + background-color: rgba(30, 30, 30, 0.6); +} + +.walkthrough__content { + position: relative; + display: flex; + flex-direction: column; + align-items: center; + padding: 36px 32px 24px; + text-align: center; +} + +.walkthrough__close { + position: absolute; + top: 12px; + right: 14px; + width: 44px; + height: 44px; + border: none; + border-radius: 50%; + background: none; + color: var(--text-dark); + font-size: 32px; + line-height: 1; + cursor: pointer; +} + +.walkthrough__close:hover { + background-color: var(--light-green); +} + +/* The avatar pictures are a green circle that is cut off at the edges of the + square image, so they are cropped to a circle, like on the profile page */ +.walkthrough__image { + width: 120px; + height: 120px; + object-fit: cover; + border-radius: 50%; +} + +.walkthrough__progress { + margin: 16px 0 4px; + color: var(--pink-text); + font-size: 16px; + letter-spacing: 0.5px; +} + +.walkthrough__title { + margin: 0 0 12px; + font-size: 30px; + font-weight: 400; +} + +.walkthrough__text { + margin: 0 0 12px; + font-size: 18px; + line-height: 1.5; +} + +.walkthrough__reminder { + font-style: italic; +} + +.walkthrough__go-to { + margin: 4px 0 8px; + padding: 8px 4px; + color: var(--dark-green); + font-size: 18px; + text-underline-offset: 4px; +} + +.walkthrough__dots { + display: flex; + gap: 8px; + margin: 16px 0 20px; + padding: 0; + list-style: none; +} + +.walkthrough__dot { + width: 10px; + height: 10px; + border-radius: 50%; + background-color: var(--border-soft); +} + +.walkthrough__dot--active { + background-color: var(--dark-pink); +} + +.walkthrough__navigation { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + width: 100%; +} + +.walkthrough__step-buttons { + display: flex; + gap: 10px; +} + +.walkthrough__skip { + padding: 10px 4px; + border: none; + background: none; + color: var(--text-dark); + font-size: 16px; + text-decoration: underline; + text-underline-offset: 4px; + cursor: pointer; +} + +.walkthrough__button { + min-width: 90px; + padding: 10px 18px; + border: 2px solid var(--dark-green); + border-radius: 999px; + background-color: var(--dark-green); + color: var(--text-on-green); + font-size: 16px; + cursor: pointer; +} + +.walkthrough__button--secondary { + background-color: transparent; + color: var(--dark-green); +} + +.walkthrough__button:hover { + opacity: 0.85; +} + +.walkthrough button:focus-visible, +.walkthrough__go-to:focus-visible { + outline: 2px solid var(--dark-pink); + outline-offset: 2px; +} + +@media (max-width: 767px) { + .walkthrough__content { + padding: 28px 20px 20px; + } + + .walkthrough__image { + width: 90px; + height: 90px; + } + + .walkthrough__title { + font-size: 26px; + } + + .walkthrough__text { + font-size: 16px; + } + + .walkthrough__button { + min-width: 0; + padding: 10px 14px; + } +} diff --git a/frontend/src/components/walkthrough/Walkthrough.tsx b/frontend/src/components/walkthrough/Walkthrough.tsx new file mode 100644 index 00000000..3f3c7781 --- /dev/null +++ b/frontend/src/components/walkthrough/Walkthrough.tsx @@ -0,0 +1,161 @@ +import { useEffect, useRef, useState, type KeyboardEvent } from "react"; +import { useTranslation } from "react-i18next"; +import { Link, useLocation } from "react-router-dom"; +import { walkthroughSteps } from "./walkthroughSteps"; +import "./Walkthrough.css"; + +type WalkthroughProps = { + startStep?: number; + onClose: (returnFocus: boolean) => void; +}; + +export default function Walkthrough({ + startStep = 0, + onClose, +}: WalkthroughProps) { + const { t } = useTranslation(); + const { pathname } = useLocation(); + const dialogRef = useRef(null); + const nextButtonRef = useRef(null); + const [stepIndex, setStepIndex] = useState(startStep); + + const step = walkthroughSteps[stepIndex]; + const isFirst = stepIndex === 0; + const isLast = stepIndex === walkthroughSteps.length - 1; + + useEffect(() => { + const dialog = dialogRef.current; + if (!dialog) return; + if (!dialog.open) dialog.showModal(); + nextButtonRef.current?.focus(); + return () => { + if (dialog.open) dialog.close(); + }; + }, []); + + const goBack = () => { + if (isFirst) return; + setStepIndex(stepIndex - 1); + if (stepIndex - 1 === 0) nextButtonRef.current?.focus(); + }; + + const goNext = () => { + if (isLast) onClose(true); + else setStepIndex(stepIndex + 1); + }; + + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key === "ArrowRight" && !isLast) setStepIndex(stepIndex + 1); + if (e.key === "ArrowLeft") goBack(); + }; + + return ( + { + e.preventDefault(); + onClose(true); + }} + // A click on the gray area outside the box hits the dialog itself + onClick={(e) => { + if (e.target === e.currentTarget) onClose(true); + }} + > +
+ + + + +

+ {t("walkthrough.stepOf", { + step: stepIndex + 1, + total: walkthroughSteps.length, + })} +

+

+ {t(`walkthrough.steps.${step.id}.title`)} +

+

+ {t(`walkthrough.steps.${step.id}.text`)} +

+ {isLast && ( +

+ {t("walkthrough.reminder")} +

+ )} + + {step.route && ( + onClose(step.route === pathname)} + > + {t("walkthrough.goTo", { + page: t(`walkthrough.steps.${step.id}.title`), + })}{" "} + + + )} + + + +
+ {isLast ? ( + + ) : ( + + )} +
+ {!isFirst && ( + + )} + +
+
+
+
+ ); +} diff --git a/frontend/src/components/walkthrough/walkthroughSteps.ts b/frontend/src/components/walkthrough/walkthroughSteps.ts new file mode 100644 index 00000000..c7f1466e --- /dev/null +++ b/frontend/src/components/walkthrough/walkthroughSteps.ts @@ -0,0 +1,35 @@ +// The steps of the walkthrough that shows new users around the app. +// The text for each step is in translation.json under walkthrough.steps.. + +import { ROUTES } from "../../AppRoutes"; +import monstera from "../../assets/avatars/monstera.png"; +import pothos from "../../assets/avatars/pothos.png"; +import fern from "../../assets/avatars/fern.png"; +import anthurium from "../../assets/avatars/anthurium.png"; +import orchid from "../../assets/avatars/orchid.png"; +import succulent from "../../assets/avatars/succulent.png"; +import snakePlant from "../../assets/avatars/snakePlant.png"; + +export type WalkthroughStep = { + id: string; + image: string; + // The page the step is about. Steps without a route have no "Go to" link. + route?: string; +}; + +export const walkthroughSteps: WalkthroughStep[] = [ + { id: "welcome", image: monstera }, + { id: "plantMatch", image: pothos, route: ROUTES.plantMatch }, + { id: "explore", image: fern, route: ROUTES.explore }, + { id: "myMatches", image: anthurium, route: ROUTES.myMatches }, + { id: "profile", image: orchid, route: ROUTES.profile }, + { id: "plantOwnerTest", image: succulent, route: ROUTES.plantOwnerTest }, + { id: "settings", image: snakePlant }, +]; + +// The step to start on when the walkthrough is opened from a page, so the +// help button explains the page the user is on. Other pages start at the beginning. +export function stepForPath(pathname: string) { + const index = walkthroughSteps.findIndex((step) => step.route === pathname); + return Math.max(index, 0); +} diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index eab30686..5817f1aa 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -196,5 +196,46 @@ "backToPlantMatch": "Go to Plant Match", "orChooseInSidebar": "Or choose another page in the sidebar.", "orChooseInMenu": "Or choose another page in the menu." + }, + "walkthrough": { + "open": "Open guide", + "close": "Close guide", + "next": "Next", + "back": "Back", + "finish": "Finish", + "skip": "Skip tour", + "stepOf": "Step {{step}} of {{total}}", + "goTo": "Go to {{page}}", + "reminder": "You can open this guide again at any time with the ? button in the top right corner.", + "steps": { + "welcome": { + "title": "Welcome!", + "text": "I <3 Plants helps you find houseplants that suit your home and everyday life. Here is a short tour of what you can do on each page." + }, + "plantMatch": { + "title": "Plant Match", + "text": "You are shown one plant at a time. Press ✿ if you like the plant and ✖ if you don't. Press the corner of the card to see how much water, sun and care the plant needs." + }, + "explore": { + "title": "Explore Plants", + "text": "Search for plants by name, filter on watering, sun and care level, and sort the results. Press ✿ next to a plant to add it to My Matches." + }, + "myMatches": { + "title": "My Matches", + "text": "All the plants you have liked are saved here. Sort them by date or name, and remove the ones you no longer want." + }, + "profile": { + "title": "Profile", + "text": "Add your name, a short description and an avatar. Choose your plant preferences, and the plants that match them are marked on the plant cards." + }, + "plantOwnerTest": { + "title": "Plant Owner Test", + "text": "Answer four questions to find out what kind of plant owner you are. You can use the result as your profile picture." + }, + "settings": { + "title": "Language and theme", + "text": "At the bottom of the menu you can switch between Norwegian and English, and between light and dark mode. On small screens, open the menu with ☰." + } + } } } diff --git a/frontend/src/i18n/locales/no/translation.json b/frontend/src/i18n/locales/no/translation.json index fb265e30..be91018a 100644 --- a/frontend/src/i18n/locales/no/translation.json +++ b/frontend/src/i18n/locales/no/translation.json @@ -196,5 +196,46 @@ "backToPlantMatch": "Gå til Plante-Matching", "orChooseInSidebar": "Eller velg en annen side i sidemenyen.", "orChooseInMenu": "Eller velg en annen side i menyen." + }, + "walkthrough": { + "open": "Åpne guide", + "close": "Lukk guiden", + "next": "Neste", + "back": "Tilbake", + "finish": "Ferdig", + "skip": "Hopp over", + "stepOf": "Steg {{step}} av {{total}}", + "goTo": "Gå til {{page}}", + "reminder": "Du kan åpne guiden igjen når som helst med ?-knappen øverst til høyre.", + "steps": { + "welcome": { + "title": "Velkommen!", + "text": "I <3 Plants hjelper deg å finne planter som passer hjemmet og hverdagen din. Her er en kort omvisning i hva du kan gjøre på hver side." + }, + "plantMatch": { + "title": "Plante-Matching", + "text": "Du får se én plante om gangen. Trykk ✿ hvis du liker planten og ✖ hvis ikke. Trykk på hjørnet av kortet for å se hvor mye vann, sol og stell planten trenger." + }, + "explore": { + "title": "Utforsk Planter", + "text": "Søk etter planter på navn, filtrer på vanning, sol og stellbehov, og sorter resultatene. Trykk ✿ ved en plante for å legge den til i Mine Matches." + }, + "myMatches": { + "title": "Mine Matches", + "text": "Alle plantene du har likt blir lagret her. Sorter dem etter dato eller navn, og fjern de du ikke vil ha lenger." + }, + "profile": { + "title": "Profil", + "text": "Legg inn navnet ditt, en kort beskrivelse og en avatar. Velg plantepreferansene dine, så blir plantene som passer markert på plantekortene." + }, + "plantOwnerTest": { + "title": "Planteeiertest", + "text": "Svar på fire spørsmål og finn ut hva slags planteeier du er. Du kan bruke resultatet som profilbilde." + }, + "settings": { + "title": "Språk og tema", + "text": "Nederst i menyen kan du bytte mellom norsk og engelsk, og mellom lys og mørk modus. På små skjermer åpner du menyen med ☰." + } + } } } diff --git a/frontend/src/tests/App.test.tsx b/frontend/src/tests/App.test.tsx index 6294d3c2..f8e7418c 100644 --- a/frontend/src/tests/App.test.tsx +++ b/frontend/src/tests/App.test.tsx @@ -1,7 +1,7 @@ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { MemoryRouter } from "react-router-dom"; -import { describe, it, expect, vi } from "vitest"; +import { beforeEach, describe, it, expect, vi } from "vitest"; import App from "../App"; import { ROUTES } from "../AppRoutes"; @@ -36,6 +36,13 @@ function renderApp(route: string) { } describe("App", () => { + // The walkthrough opens on the first visit. These tests are about the + // rest of the app, so it is marked as seen. It is tested below. + beforeEach(() => { + localStorage.clear(); + localStorage.setItem("walkthroughSeen", "true"); + }); + it.each([ [ROUTES.plantMatch, "plant match page"], [ROUTES.explore, "explore page"], @@ -93,4 +100,44 @@ describe("App", () => { screen.getByRole("link", { name: "app.skipToContent" }), ).toHaveFocus(); }); + + describe("walkthrough", () => { + beforeEach(() => { + localStorage.clear(); + }); + + it("opens on the first visit and is not shown again after closing", async () => { + const user = userEvent.setup(); + renderApp(ROUTES.plantMatch); + + expect(screen.getByRole("dialog")).toHaveAccessibleName( + "walkthrough.steps.welcome.title", + ); + await user.click( + screen.getByRole("button", { name: "walkthrough.skip" }), + ); + + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); + expect(localStorage.getItem("walkthroughSeen")).toBe("true"); + // Focus goes back to the help button + expect( + screen.getByRole("button", { name: "walkthrough.open" }), + ).toHaveFocus(); + }); + + it("opens from the help button at the step for the current page", async () => { + localStorage.setItem("walkthroughSeen", "true"); + const user = userEvent.setup(); + renderApp(ROUTES.explore); + + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); + await user.click( + screen.getByRole("button", { name: "walkthrough.open" }), + ); + + expect(screen.getByRole("dialog")).toHaveAccessibleName( + "walkthrough.steps.explore.title", + ); + }); + }); }); diff --git a/frontend/src/tests/Walkthrough.test.tsx b/frontend/src/tests/Walkthrough.test.tsx new file mode 100644 index 00000000..ea3c48f3 --- /dev/null +++ b/frontend/src/tests/Walkthrough.test.tsx @@ -0,0 +1,131 @@ +import { render, screen } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { MemoryRouter, Route, Routes } from "react-router-dom"; +import { describe, it, expect, vi } from "vitest"; +import Walkthrough from "../components/walkthrough/Walkthrough"; +import { + stepForPath, + walkthroughSteps, +} from "../components/walkthrough/walkthroughSteps"; +import { ROUTES } from "../AppRoutes"; + +vi.mock("react-i18next", () => ({ + useTranslation: () => ({ t: (key: string) => key }), +})); + +function renderWalkthrough(startStep = 0, route = ROUTES.plantMatch) { + const onClose = vi.fn(); + render( + + + + explore page

} /> + +
+
, + ); + return onClose; +} + +const LAST_STEP = walkthroughSteps.length - 1; + +describe("Walkthrough", () => { + it("opens as a modal dialog on the given step", () => { + renderWalkthrough(2); + + const dialog = screen.getByRole("dialog"); + expect(dialog).toHaveAttribute("open"); + expect(dialog).toHaveAccessibleName("walkthrough.steps.explore.title"); + }); + + it("goes forward and back through the steps", async () => { + const user = userEvent.setup(); + renderWalkthrough(); + + expect( + screen.queryByRole("button", { name: "walkthrough.back" }), + ).not.toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: "walkthrough.next" })); + expect( + screen.getByRole("heading", { + name: "walkthrough.steps.plantMatch.title", + }), + ).toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: "walkthrough.back" })); + expect( + screen.getByRole("heading", { name: "walkthrough.steps.welcome.title" }), + ).toBeInTheDocument(); + }); + + it("moves between steps with the arrow keys", async () => { + const user = userEvent.setup(); + renderWalkthrough(); + + await user.keyboard("{ArrowRight}{ArrowRight}"); + expect( + screen.getByRole("heading", { name: "walkthrough.steps.explore.title" }), + ).toBeInTheDocument(); + + await user.keyboard("{ArrowLeft}"); + expect( + screen.getByRole("heading", { + name: "walkthrough.steps.plantMatch.title", + }), + ).toBeInTheDocument(); + }); + + it("closes with Finish on the last step", async () => { + const user = userEvent.setup(); + const onClose = renderWalkthrough(LAST_STEP); + + expect(screen.getByText("walkthrough.reminder")).toBeInTheDocument(); + await user.click( + screen.getByRole("button", { name: "walkthrough.finish" }), + ); + + expect(onClose).toHaveBeenCalledWith(true); + }); + + it.each(["walkthrough.skip", "walkthrough.close"])( + "closes with the %s button", + async (name) => { + const user = userEvent.setup(); + const onClose = renderWalkthrough(); + + await user.click(screen.getByRole("button", { name })); + + expect(onClose).toHaveBeenCalledWith(true); + }, + ); + + it("goes to the page of the step and closes", async () => { + const user = userEvent.setup(); + const onClose = renderWalkthrough(2); + + await user.click(screen.getByRole("link", { name: /walkthrough.goTo/ })); + + expect(screen.getByText("explore page")).toBeInTheDocument(); + // The app moves focus to the new page, so it should not go back to the help button + expect(onClose).toHaveBeenCalledWith(false); + }); + + it("has no link on steps that are not about a page", () => { + renderWalkthrough(0); + + expect(screen.queryByRole("link")).not.toBeInTheDocument(); + }); +}); + +describe("stepForPath", () => { + it("starts on the step for the current page", () => { + expect(walkthroughSteps[stepForPath(ROUTES.myMatches)].id).toBe( + "myMatches", + ); + }); + + it("starts at the beginning on pages without a step", () => { + expect(stepForPath("/unknown-page")).toBe(0); + }); +}); diff --git a/frontend/src/tests/setup.ts b/frontend/src/tests/setup.ts index 2ba240b8..505180fa 100644 --- a/frontend/src/tests/setup.ts +++ b/frontend/src/tests/setup.ts @@ -5,3 +5,15 @@ import { cleanup } from "@testing-library/react"; afterEach(() => { cleanup(); }); + +// jsdom has but not showModal() and close(), so they are added here +if (!HTMLDialogElement.prototype.showModal) { + HTMLDialogElement.prototype.showModal = function () { + this.open = true; + }; +} +if (!HTMLDialogElement.prototype.close) { + HTMLDialogElement.prototype.close = function () { + this.open = false; + }; +} diff --git a/playwright.config.ts b/playwright.config.ts index 93a712cf..1fc103b0 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -4,6 +4,17 @@ import { E2E_DB_NAME, E2E_DB_PORT, E2E_MONGODB_URI } from "./e2e/testDb"; // The test backend runs on 3002, so it does not collide with the dev backend on 3001 const E2E_BACKEND_PORT = 3002; +// The walkthrough opens on the first visit and covers the page. +const WALKTHROUGH_SEEN = { + cookies: [], + origins: [ + { + origin: "http://localhost:4173", + localStorage: [{ name: "walkthroughSeen", value: "true" }], + }, + ], +}; + export default defineConfig({ testDir: "./e2e", // Runs after the web servers are up: empties the test database and fills it with fixtures @@ -34,12 +45,12 @@ export default defineConfig({ { name: "desktop", testMatch: "**/ui*.spec.ts", - use: { ...devices["Desktop Chrome"] }, + use: { ...devices["Desktop Chrome"], storageState: WALKTHROUGH_SEEN }, }, { name: "mobile", testMatch: "**/ui*.spec.ts", - use: { ...devices["Pixel 7"] }, + use: { ...devices["Pixel 7"], storageState: WALKTHROUGH_SEEN }, }, ], // Started in order: the database must be up before the backend, which connects on startup From 1204221c5f3ebd5edbb5ea4f8f21b7a744b0ac8e Mon Sep 17 00:00:00 2001 From: Alexandra Date: Fri, 20 Nov 2026 12:41:34 +0100 Subject: [PATCH 2/9] cleaned up css code as there was unused and duplicate code --- .../components/errorMessage/ErrorMessage.css | 1 - .../components/filterSelect/FilterSelect.css | 1 - frontend/src/components/header/Header.css | 14 ++++++++++++- frontend/src/components/header/Header.tsx | 2 +- .../components/matchButton/MatchButton.css | 8 ++++---- .../src/components/plantCard/PlantCard.css | 14 +++++++------ .../PlantPreferenceChips.css | 10 ---------- frontend/src/components/sidebar/Sidebar.css | 3 --- frontend/src/index.css | 13 ++++++++++++ .../pages/explorePlants/ExplorePlantsPage.css | 13 +----------- .../src/pages/plantMatch/PlantMatchPage.css | 20 +------------------ .../plantOwnerTest/PlantOwnerTestPage.css | 1 - frontend/src/pages/profile/ProfilePage.css | 13 ------------ frontend/src/pages/profile/ProfilePage.tsx | 2 +- frontend/src/tests/ProfilePage.test.tsx | 2 +- .../__snapshots__/ProfilePage.test.tsx.snap | 2 +- 16 files changed, 44 insertions(+), 75 deletions(-) diff --git a/frontend/src/components/errorMessage/ErrorMessage.css b/frontend/src/components/errorMessage/ErrorMessage.css index aa8d1197..b4d1cdf6 100644 --- a/frontend/src/components/errorMessage/ErrorMessage.css +++ b/frontend/src/components/errorMessage/ErrorMessage.css @@ -16,7 +16,6 @@ border-radius: 8px; background-color: var(--dark-pink); color: var(--text-light); - font-family: var(--font-ui); font-size: 1rem; animation: error-message-in 0.2s ease-out; } diff --git a/frontend/src/components/filterSelect/FilterSelect.css b/frontend/src/components/filterSelect/FilterSelect.css index 53919975..c75004fc 100644 --- a/frontend/src/components/filterSelect/FilterSelect.css +++ b/frontend/src/components/filterSelect/FilterSelect.css @@ -17,6 +17,5 @@ .filter select:focus { outline: none; - border-color: var(--dark-pink); box-shadow: 0 0 0 2px var(--dark-pink); } diff --git a/frontend/src/components/header/Header.css b/frontend/src/components/header/Header.css index 1cb5ba4c..cf48ea42 100644 --- a/frontend/src/components/header/Header.css +++ b/frontend/src/components/header/Header.css @@ -41,8 +41,13 @@ border-radius: 50%; background-color: var(--dark-green); color: var(--text-on-green); + /* Georgia draws "?" curved and leaning, so the button uses a plain + sans-serif font to show it straight and centred */ + font-family: Arial, Helvetica, sans-serif; font-size: 26px; font-weight: 700; + font-style: normal; + line-height: 1; cursor: pointer; transition: transform 0.2s ease; } @@ -67,8 +72,15 @@ display: none; } + /* Smaller on narrow screens, and kept clear of the menu button on the left + and the help button on the right. A very narrow screen wraps the title + instead of letting it go under the buttons. */ .header h1 { - font-size: 37px; + padding: 0 64px; + font-size: clamp(24px, 7vw, 32px); + line-height: 1.1; + text-align: center; + overflow-wrap: anywhere; } .menu-button { diff --git a/frontend/src/components/header/Header.tsx b/frontend/src/components/header/Header.tsx index 49d5e2d1..f1378271 100644 --- a/frontend/src/components/header/Header.tsx +++ b/frontend/src/components/header/Header.tsx @@ -59,7 +59,7 @@ export default function Header({ aria-label={t("walkthrough.open")} aria-haspopup="dialog" > - ? + )} diff --git a/frontend/src/components/matchButton/MatchButton.css b/frontend/src/components/matchButton/MatchButton.css index bb68063e..8c58b552 100644 --- a/frontend/src/components/matchButton/MatchButton.css +++ b/frontend/src/components/matchButton/MatchButton.css @@ -56,10 +56,6 @@ border-color: var(--text-light); } -[data-theme="dark"] .add-match-button:focus-visible { - box-shadow: 0 0 0 3px var(--text-light); -} - .add-match-button:hover { transform: scale(1.1); } @@ -69,6 +65,10 @@ box-shadow: 0 0 0 3px var(--dark-pink); } +[data-theme="dark"] .add-match-button:focus-visible { + box-shadow: 0 0 0 3px var(--text-light); +} + @media (max-width: 767px) { .add-match-button { width: 35px; diff --git a/frontend/src/components/plantCard/PlantCard.css b/frontend/src/components/plantCard/PlantCard.css index e7834eb9..551a85ce 100644 --- a/frontend/src/components/plantCard/PlantCard.css +++ b/frontend/src/components/plantCard/PlantCard.css @@ -239,13 +239,8 @@ margin: 0; padding: 20px 18px 4px; - display: flex; - flex-wrap: wrap; - justify-content: flex-start; align-items: center; align-content: flex-start; - - gap: 8px; } .plant-card-hint { @@ -255,7 +250,6 @@ width: 100%; border: none; background: none; - font-family: inherit; cursor: pointer; margin: auto 0 0; @@ -429,11 +423,19 @@ overflow-wrap: anywhere; } + /* The chips are shown two by two, with the same width */ .plant-card-front .plant-card-preferences { + display: grid; + grid-template-columns: 1fr 1fr; padding: 18px 12px 4px; gap: 6px; } + .plant-card-flipper .plant-preference-chip { + width: 100%; + text-align: center; + } + .plant-card-hint { padding: 16px var(--card-hint-space) 26px 14px; font-size: clamp(0.8rem, 4.8cqw, 0.9rem); diff --git a/frontend/src/components/plantPreferenceChips/PlantPreferenceChips.css b/frontend/src/components/plantPreferenceChips/PlantPreferenceChips.css index 5791f5a5..0c9209fe 100644 --- a/frontend/src/components/plantPreferenceChips/PlantPreferenceChips.css +++ b/frontend/src/components/plantPreferenceChips/PlantPreferenceChips.css @@ -13,15 +13,6 @@ display: flex; } -.plant-card-preferences .visually-hidden { - position: absolute; - width: 1px; - height: 1px; - overflow: hidden; - clip: rect(0 0 0 0); - white-space: nowrap; -} - .plant-preference-chip { flex-shrink: 0; @@ -42,6 +33,5 @@ .plant-preference-chip.is-match { background: var(--dark-pink); color: var(--text-light); - font-weight: 700; transform: scale(1.05); } diff --git a/frontend/src/components/sidebar/Sidebar.css b/frontend/src/components/sidebar/Sidebar.css index f7848c54..603891af 100644 --- a/frontend/src/components/sidebar/Sidebar.css +++ b/frontend/src/components/sidebar/Sidebar.css @@ -80,9 +80,6 @@ .sidebar__heart { color: var(--dark-pink); } -[data-theme="dark"] .sidebar__heart { - color: var(--dark-pink); -} .close-button { display: none; diff --git a/frontend/src/index.css b/frontend/src/index.css index bf61b462..938a3c38 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -63,6 +63,19 @@ textarea { --card-hint: var(--cream); } +/* Hidden on screen, but still read by screen readers. Used on several pages */ +.visually-hidden { + position: absolute; + width: 1px; + height: 1px; + padding: 0; + margin: -1px; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; + border: 0; +} + /* The round "i" button that shows a help text, used on Plant Match and Profile */ .info-button { display: flex; diff --git a/frontend/src/pages/explorePlants/ExplorePlantsPage.css b/frontend/src/pages/explorePlants/ExplorePlantsPage.css index 3bcc0020..d3a07349 100644 --- a/frontend/src/pages/explorePlants/ExplorePlantsPage.css +++ b/frontend/src/pages/explorePlants/ExplorePlantsPage.css @@ -11,15 +11,6 @@ gap: 30px; } -.visually-hidden { - position: absolute; - width: 1px; - height: 1px; - overflow: hidden; - clip: rect(0 0 0 0); - white-space: nowrap; -} - .explore-form, .plant-grid { width: 100%; @@ -53,7 +44,6 @@ .explore-search input:focus { outline: none; - border-color: var(--dark-pink); box-shadow: 0 0 0 2px var(--dark-pink); } @@ -87,9 +77,8 @@ color: var(--text-dark); } -.reset-button:focus-within { +.reset-button:focus-visible { outline: none; - border-color: var(--dark-pink); box-shadow: 0 0 0 2px var(--dark-pink); } diff --git a/frontend/src/pages/plantMatch/PlantMatchPage.css b/frontend/src/pages/plantMatch/PlantMatchPage.css index f85f2cd6..23f71099 100644 --- a/frontend/src/pages/plantMatch/PlantMatchPage.css +++ b/frontend/src/pages/plantMatch/PlantMatchPage.css @@ -27,9 +27,7 @@ display: flex; justify-content: center; align-items: center; -} - -.plant-card-container { + /* A stronger shadow than the cards on the other pages */ --card-shadow: drop-shadow(0 8px 18px rgba(30, 40, 30, 0.3)); --card-shadow-hover: drop-shadow(0 12px 26px rgba(30, 40, 30, 0.36)); } @@ -222,7 +220,6 @@ margin-left: 0; width: 100%; padding: 16px 8px 24px; - gap: 14px; overflow-x: hidden; } @@ -272,7 +269,6 @@ } .match-info-box p { - margin: 0; font-size: 14px; line-height: 1.45; } @@ -283,18 +279,4 @@ flex: 0 0 80px; font-size: 36px; } - - .plant-card-front .plant-card-preferences { - padding: 18px 14px 20px; - display: grid; - grid-template-columns: 1fr 1fr; - gap: 8px; - } - - .plant-preference-chip { - width: 100%; - box-sizing: border-box; - text-align: center; - justify-content: center; - } } diff --git a/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css b/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css index db59f5e1..08325af0 100644 --- a/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css +++ b/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css @@ -55,7 +55,6 @@ border-radius: 16px; background: var(--dark-green); color: var(--text-on-green); - font-family: var(--font-text); font-size: 1.45rem; font-weight: 500; line-height: 1.5; diff --git a/frontend/src/pages/profile/ProfilePage.css b/frontend/src/pages/profile/ProfilePage.css index 18e0222f..1ad54204 100644 --- a/frontend/src/pages/profile/ProfilePage.css +++ b/frontend/src/pages/profile/ProfilePage.css @@ -4,7 +4,6 @@ box-sizing: border-box; } - .profile > * { zoom: 1.1; } @@ -27,18 +26,6 @@ padding-top: 20px; } -.profile-description-label { - position: absolute; - width: 1px; - height: 1px; - padding: 0; - margin: -1px; - overflow: hidden; - clip-path: inset(50%); - white-space: nowrap; - border: 0; -} - .profile-preferences { margin-top: 50px; } diff --git a/frontend/src/pages/profile/ProfilePage.tsx b/frontend/src/pages/profile/ProfilePage.tsx index 71e96f1b..753d6bd5 100644 --- a/frontend/src/pages/profile/ProfilePage.tsx +++ b/frontend/src/pages/profile/ProfilePage.tsx @@ -83,7 +83,7 @@ export default function ProfilePage() {

{shownName}

- + {t("profile.descriptionLabel")}:{" "} {shownDescription} diff --git a/frontend/src/tests/ProfilePage.test.tsx b/frontend/src/tests/ProfilePage.test.tsx index f2442457..f279383f 100644 --- a/frontend/src/tests/ProfilePage.test.tsx +++ b/frontend/src/tests/ProfilePage.test.tsx @@ -62,7 +62,7 @@ describe("Profile page", () => { render(); const label = screen.getByText("profile.descriptionLabel:"); - expect(label).toHaveClass("profile-description-label"); + expect(label).toHaveClass("visually-hidden"); expect(label.closest("p")).toHaveTextContent( "profile.descriptionLabel: profile.description", ); diff --git a/frontend/src/tests/__snapshots__/ProfilePage.test.tsx.snap b/frontend/src/tests/__snapshots__/ProfilePage.test.tsx.snap index ba75b73e..8455aefd 100644 --- a/frontend/src/tests/__snapshots__/ProfilePage.test.tsx.snap +++ b/frontend/src/tests/__snapshots__/ProfilePage.test.tsx.snap @@ -21,7 +21,7 @@ exports[`Profile page > matches snapshot 1`] = `

profile.descriptionLabel : From cf9c1ce206adac8f77ccb1e6782eca3308b91ae8 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Fri, 20 Nov 2026 12:56:28 +0100 Subject: [PATCH 3/9] fixed minor styling issues that accured and fixed a bug with the plant owner test --- frontend/src/context/UserContext.tsx | 17 +++++++++++++- frontend/src/hooks/useProfile.ts | 5 ++-- frontend/src/i18n/locales/en/translation.json | 2 +- frontend/src/i18n/locales/no/translation.json | 2 +- frontend/src/index.css | 5 ++-- .../plantOwnerTest/PlantOwnerTestPage.css | 3 ++- .../plantOwnerTest/PlantOwnerTestPage.tsx | 8 ++++++- .../src/tests/PlantOwnerTestPage.test.tsx | 23 +++++++++++++++++-- 8 files changed, 54 insertions(+), 11 deletions(-) diff --git a/frontend/src/context/UserContext.tsx b/frontend/src/context/UserContext.tsx index 72152d25..3204f713 100644 --- a/frontend/src/context/UserContext.tsx +++ b/frontend/src/context/UserContext.tsx @@ -14,6 +14,9 @@ import type { User } from "../types/User"; // The only user data kept in the browser. Everything else is in the database. const STORAGE_KEY = "userId"; +// How often to try loading the user again after an error +const RETRY_MS = 5000; + function readUserId(): string | null { try { return localStorage.getItem(STORAGE_KEY); @@ -67,13 +70,25 @@ export function UserProvider({ children }: { children: ReactNode }) { useEffect(removeLegacyKeys, []); - const { data, loading, error } = useQuery(GET_USER, { + const { data, loading, error, refetch } = useQuery(GET_USER, { variables: { userId: userId ?? "" }, // No id yet, so there is nothing to ask for skip: !userId, }); const [createUser] = useMutation(CREATE_USER); + // If the backend or database was down when the page loaded, keep trying, + // so the user shows up (and changes are shown) once it is back + useEffect(() => { + if (!error || !userId) return; + const retry = setInterval(() => { + refetch().catch(() => { + // Still down. Try again on the next tick. + }); + }, RETRY_MS); + return () => clearInterval(retry); + }, [error, userId, refetch]); + // Stops StrictMode (which runs effects twice) from creating two users const creating = useRef(false); diff --git a/frontend/src/hooks/useProfile.ts b/frontend/src/hooks/useProfile.ts index 9771fde9..fb1c80a3 100644 --- a/frontend/src/hooks/useProfile.ts +++ b/frontend/src/hooks/useProfile.ts @@ -71,7 +71,7 @@ export function useProfile() { const setProfilePicture = async (profilePicture: string) => { if (!userId) return false; try { - await updateProfile({ + const result = await updateProfile({ variables: { userId, profilePicture }, // Shows the new picture right away instead of waiting for the server optimisticResponse: { @@ -84,7 +84,8 @@ export function useProfile() { }, }, }); - return true; + // null means the user was not found in the database + return Boolean(result.data?.updateUserProfile); } catch { // Apollo rolls the picture back if the server fails return false; diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index 5817f1aa..a1485045 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -112,7 +112,7 @@ "fern": "Fern", "orchid": "Orchid", "placeholderName": "Enter your name", - "placeholderAbout": "Something about you..." + "placeholderAbout": "Something about you" }, "plantCard": { "preferences": "Plant preferences", diff --git a/frontend/src/i18n/locales/no/translation.json b/frontend/src/i18n/locales/no/translation.json index be91018a..a12c54c2 100644 --- a/frontend/src/i18n/locales/no/translation.json +++ b/frontend/src/i18n/locales/no/translation.json @@ -112,7 +112,7 @@ "fern": "Bregne", "orchid": "Orkidé", "placeholderName": "Skriv inn navnet ditt", - "placeholderAbout": "Noe om deg..." + "placeholderAbout": "Noe om deg" }, "plantCard": { "preferences": "Plantepreferanser", diff --git a/frontend/src/index.css b/frontend/src/index.css index 938a3c38..5eca1916 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -132,7 +132,8 @@ textarea { --card-shadow: none; --card-shadow-hover: none; --card-box-shadow: none; - /* Front of the plant card: light with dark green text in dark mode */ - --card-front: var(--cream); + /* Front of the plant card: light green with dark green text in dark mode, + the same color as the name at the top */ + --card-front: var(--soft-green); --card-hint: var(--deep-green); } diff --git a/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css b/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css index 08325af0..b6efdb98 100644 --- a/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css +++ b/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.css @@ -151,13 +151,14 @@ .plant-owner-test-eyebrow { margin: 0 0 1rem; - color: var(--dark-pink); + color: var(--text-dark); font-size: 2rem; font-weight: 600; } .plant-owner-test-result h2 { margin: 0 0 1.25rem; + color: var(--dark-pink); font-size: 2.25rem; line-height: 1.3; } diff --git a/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.tsx b/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.tsx index d1171d05..a477bea6 100644 --- a/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.tsx +++ b/frontend/src/pages/plantOwnerTest/PlantOwnerTestPage.tsx @@ -1,5 +1,7 @@ import { useState } from "react"; import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom"; +import { ROUTES } from "../../AppRoutes"; import { useProfile } from "../../hooks/useProfile.ts"; import { plantOwnerTest, @@ -12,6 +14,7 @@ import "./PlantOwnerTestPage.css"; export default function PlantOwnerTestPage() { const { t } = useTranslation(); const { profilePicture, setProfilePicture } = useProfile(); + const navigate = useNavigate(); const [currentQuestion, setCurrentQuestion] = useState(0); // Question id -> chosen option id. Only kept while the test is open, so the @@ -53,7 +56,10 @@ export default function PlantOwnerTestPage() { } async function saveAsProfilePicture(type: PlantOwnerType) { - setSaveFailed(!(await setProfilePicture(type))); + const saved = await setProfilePicture(type); + setSaveFailed(!saved); + // Show the new picture on the profile page + if (saved) navigate(ROUTES.profile); } if (result) { diff --git a/frontend/src/tests/PlantOwnerTestPage.test.tsx b/frontend/src/tests/PlantOwnerTestPage.test.tsx index 2dac5d84..9adaabe4 100644 --- a/frontend/src/tests/PlantOwnerTestPage.test.tsx +++ b/frontend/src/tests/PlantOwnerTestPage.test.tsx @@ -4,10 +4,12 @@ import { beforeEach, describe, it, expect, vi } from "vitest"; import PlantOwnerTestPage from "../pages/plantOwnerTest/PlantOwnerTestPage.tsx"; import { getPlantOwnerResult } from "../pages/plantOwnerTest/plantOwnerTest.ts"; -const { updateProfile, useUser } = vi.hoisted(() => ({ +const { updateProfile, useUser, navigate } = vi.hoisted(() => ({ updateProfile: vi.fn(), useUser: vi.fn(), + navigate: vi.fn(), })); +vi.mock("react-router-dom", () => ({ useNavigate: () => navigate })); vi.mock("@apollo/client/react", () => ({ useMutation: () => [updateProfile], })); @@ -81,7 +83,10 @@ describe("getPlantOwnerResult", () => { describe("PlantOwnerTestPage", () => { beforeEach(() => { updateProfile.mockReset(); - updateProfile.mockResolvedValue({}); + navigate.mockReset(); + updateProfile.mockResolvedValue({ + data: { updateUserProfile: { id: "user-1" } }, + }); mockUser(); }); @@ -150,6 +155,7 @@ describe("PlantOwnerTestPage", () => { }), ); expect(screen.queryByRole("alert")).not.toBeInTheDocument(); + expect(navigate).toHaveBeenCalledWith("/profile"); }); it("shows an error when the profile picture could not be saved", async () => { @@ -161,6 +167,19 @@ describe("PlantOwnerTestPage", () => { expect(await screen.findByRole("alert")).toHaveTextContent( "plantQuiz.profilePicture.failed", ); + expect(navigate).not.toHaveBeenCalled(); + }); + + it("shows an error when the user was not found in the database", async () => { + updateProfile.mockResolvedValue({ data: { updateUserProfile: null } }); + const { pick } = await takeTest("city", "minimum"); + + await pick("plantQuiz.profilePicture.use"); + + expect(await screen.findByRole("alert")).toHaveTextContent( + "plantQuiz.profilePicture.failed", + ); + expect(navigate).not.toHaveBeenCalled(); }); it("says so when the result already is the profile picture", async () => { From 906e6caf2a161f92f44fffecb051696807537a1f Mon Sep 17 00:00:00 2001 From: Alexandra Date: Sun, 22 Nov 2026 17:35:21 +0100 Subject: [PATCH 4/9] Style changes to walktrough and added translation button here, and updated tests to test the new walktrough correctly --- e2e/uiWalkthrough.spec.ts | 18 ++++++++++++ .../sidebarButtons/LanguageSwitcher.tsx | 1 + .../components/walkthrough/Walkthrough.css | 21 ++++---------- .../components/walkthrough/Walkthrough.tsx | 13 ++------- frontend/src/i18n/locales/en/translation.json | 3 +- frontend/src/i18n/locales/no/translation.json | 3 +- frontend/src/index.css | 2 +- frontend/src/tests/App.test.tsx | 2 +- frontend/src/tests/Walkthrough.test.tsx | 28 ++++++++++++------- 9 files changed, 49 insertions(+), 42 deletions(-) diff --git a/e2e/uiWalkthrough.spec.ts b/e2e/uiWalkthrough.spec.ts index 8d1c288e..a57d034a 100644 --- a/e2e/uiWalkthrough.spec.ts +++ b/e2e/uiWalkthrough.spec.ts @@ -63,3 +63,21 @@ test("the help button opens the walkthrough at the current page, and Go to chang await expect(page.getByRole("dialog")).toBeHidden(); await expect(page).toHaveURL(/#\/myMatches$/); }); + +test("the language button in the walkthrough changes the language and keeps the walkthrough open", async ({ + page, +}) => { + await page.goto(""); + const dialog = page.getByRole("dialog"); + await page.keyboard.press("ArrowRight"); + await expect(page.locator(".walkthrough__progress")).toHaveText(/2 av 7/); + + await dialog.getByRole("button", { name: "EN - Switch to English" }).click(); + + await expect(dialog).toBeVisible(); + await expect(page.locator(".walkthrough__progress")).toHaveText(/2 of 7/); + await expect(page.locator("html")).toHaveAttribute("lang", "en"); + + await dialog.getByRole("button", { name: "NO - Bytt til norsk" }).click(); + await expect(page.locator(".walkthrough__progress")).toHaveText(/2 av 7/); +}); diff --git a/frontend/src/components/sidebarButtons/LanguageSwitcher.tsx b/frontend/src/components/sidebarButtons/LanguageSwitcher.tsx index 0615a112..a7ca27c7 100644 --- a/frontend/src/components/sidebarButtons/LanguageSwitcher.tsx +++ b/frontend/src/components/sidebarButtons/LanguageSwitcher.tsx @@ -1,4 +1,5 @@ import { useTranslation } from "react-i18next"; +import "./SidebarButton.css"; // The chosen language is saved in localStorage by i18n/index.ts when it // changes, so the switcher only reads and changes the language in i18next. diff --git a/frontend/src/components/walkthrough/Walkthrough.css b/frontend/src/components/walkthrough/Walkthrough.css index a2d0ad73..ed314ac3 100644 --- a/frontend/src/components/walkthrough/Walkthrough.css +++ b/frontend/src/components/walkthrough/Walkthrough.css @@ -116,31 +116,22 @@ gap: 10px; } -.walkthrough__skip { - padding: 10px 4px; - border: none; - background: none; - color: var(--text-dark); - font-size: 16px; - text-decoration: underline; - text-underline-offset: 4px; - cursor: pointer; -} - .walkthrough__button { min-width: 90px; padding: 10px 18px; - border: 2px solid var(--dark-green); + border: 2px solid var(--dark-pink); border-radius: 999px; - background-color: var(--dark-green); - color: var(--text-on-green); + background-color: var(--dark-pink); + /* White has enough contrast on the pink in both light and dark mode */ + color: var(--text-light); font-size: 16px; cursor: pointer; } .walkthrough__button--secondary { background-color: transparent; - color: var(--dark-green); + /* --pink-text instead of --dark-pink, which is too light on the dark background */ + color: var(--pink-text); } .walkthrough__button:hover { diff --git a/frontend/src/components/walkthrough/Walkthrough.tsx b/frontend/src/components/walkthrough/Walkthrough.tsx index 3f3c7781..474147c0 100644 --- a/frontend/src/components/walkthrough/Walkthrough.tsx +++ b/frontend/src/components/walkthrough/Walkthrough.tsx @@ -1,6 +1,7 @@ import { useEffect, useRef, useState, type KeyboardEvent } from "react"; import { useTranslation } from "react-i18next"; import { Link, useLocation } from "react-router-dom"; +import LanguageSwitcher from "../sidebarButtons/LanguageSwitcher"; import { walkthroughSteps } from "./walkthroughSteps"; import "./Walkthrough.css"; @@ -124,17 +125,7 @@ export default function Walkthrough({

- {isLast ? ( - - ) : ( - - )} +
{!isFirst && ( From 7bd741d061b120126f4f6652215b2b4e1474c312 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Sun, 22 Nov 2026 18:29:05 +0100 Subject: [PATCH 9/9] updated so all tests can be run with one commando and documentation decribes this corretly --- README.md | 4 +++- docs/quality.md | 2 +- docs/tests.md | 6 ++++++ package.json | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4bbf04cc..f3fbda33 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ cd T03-Project-2 npm run install:all ``` -This installs the dependencies for the repo root, `backend` and `frontend`, which each have their own `package.json`. +This installs the dependencies for the repo root, `backend` and `frontend`, which each have their own `package.json`. To install exactly the versions in the lock files instead, as GitHub Actions does, run `npm ci && npm ci --prefix backend && npm ci --prefix frontend`. `npm ci` only installs the dependencies and does not run any tests (see [Testing](#testing)). ### 2. Create the backend `.env` file (in the VS Code terminal) @@ -128,6 +128,8 @@ npm run test:backend npm run test:e2e ``` +`npm run test:all` runs all three after each other, and `npm run ci:all` first does a clean install (`npm ci`) in the root, backend and frontend, and then runs all the tests. Only run one e2e test run at a time, since all the e2e tests share the same test database and servers. + The first time you run the e2e tests, install the browser with `npx playwright install chromium`. ### Test coverage diff --git a/docs/quality.md b/docs/quality.md index 3391e4ec..0e9cc443 100644 --- a/docs/quality.md +++ b/docs/quality.md @@ -12,7 +12,7 @@ This document describes how we have worked to ensure the quality of the applicat ### Continuous integration -GitHub Actions ([.github/workflows/ci.yml](../.github/workflows/ci.yml)) runs on every pull request and on every push to `main`. It checks the formatting with Prettier, runs lint, tests and a build for both the backend and the frontend, and runs the end-to-end tests. This means a pull request that breaks the formatting, lint, a test or the build is caught before it is merged, instead of being found later on `main`. +GitHub Actions ([.github/workflows/ci.yml](../.github/workflows/ci.yml)) runs on every pull request and on every push to `main`. It checks the formatting with Prettier, runs lint, tests and a build for both the backend and the frontend, and runs the end-to-end tests. This means a pull request that breaks the formatting, lint, a test or the build is caught before it is merged, instead of being found later on `main`. Each job first installs the dependencies with `npm ci`, which installs exactly the versions in `package-lock.json`, and then runs the checks as separate steps, so it is clear which step failed. `npm run ci:all` does the same install and runs all the tests locally (see [Run the tests](tests.md#run-the-tests)), but not the formatting check, lint and build, which are run with `npm run format:check`, `npm run lint` and `npm run build`. ## Work practices diff --git a/docs/tests.md b/docs/tests.md index cc89573b..4f340b14 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -47,12 +47,18 @@ Run from the root folder: npm run test:frontend # component tests npm run test:backend # backend tests npm run test:e2e # end-to-end and API tests +npm run test:all # backend, frontend and e2e tests, one after the other +npm run ci:all # clean install (npm ci) in root, backend and frontend, then test:all npm run test:e2e:ui # e2e tests in Playwright's UI mode, step by step npm run test:e2e:headed # desktop e2e tests in a visible browser npx playwright test --project=mobile --headed # the same on mobile (Pixel 7) npx playwright show-report # HTML report with traces of failed e2e tests ``` +`npm ci` on its own only installs the dependencies from `package-lock.json` and does not run any tests. Each folder has its own lock file, so `npm ci` in the root does not install `backend` or `frontend`. `npm run ci:all` does the install and the tests in one command. Stop `npm run dev` first, since `npm ci` deletes `node_modules`. + +Only run one e2e test run at a time. All e2e tests share one test database and the servers on ports 4173 and 3002, and Playwright reuses servers that are already running locally. Two runs at the same time therefore empty each other's database, and when one run ends it stops the servers the other run uses, so the tests fail with `ERR_CONNECTION_REFUSED`. + ### If the tests do not start If the tests stop with `Cannot find native binding` or `Cannot find module '@rolldown/binding-...'`, diff --git a/package.json b/package.json index b52494de..65400b33 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,8 @@ "test:e2e:headed": "playwright test --project=desktop --headed", "test:backend": "npm test --prefix backend -- --run", "test:frontend": "npm test --prefix frontend -- --run", + "test:all": "npm run test:backend && npm run test:frontend && npm run test:e2e", + "ci:all": "npm ci && npm ci --prefix backend && npm ci --prefix frontend && npm run test:all", "test:coverage": "npm run test:backend -- --coverage && npm run test:frontend -- --coverage", "format": "prettier --write .", "format:check": "prettier --check ."