Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ http://it2810-03.idi.ntnu.no/project2/
- **Plant Match:** Plants matching the most preferences come first, otherwise the order is random. The deck is kept while the user visits other pages, so they can continue where they left off.
- **My Matches:** Collects liked plants from both Plant Match and Explore Plants. The user only stores plant ids, and the list is fetched one page at a time, so responses do not grow with the number of matches.
- **Explore Plants:** Search, filtering, sorting and pagination run in the backend, so the frontend only receives one page at a time, whatever the size of the collection. The state is kept in the URL, so a view survives a refresh and can be shared as a link. Each filter value shows how many plants it would give, and values that would give none can't be chosen, so filtering never ends in an empty list.
- **Plant Owner Test:** Four questions give one of six plant owner types, whose picture can be used as profile picture. The result also suggests one preference each for watering, light and care level, based on the answers and the description of the type. The user chooses which of them to add, and they are added to the preferences they already have, so PlantMatch puts plants that suit them first right away.
- **Walkthrough:** A short guide opens on the first visit and explains what the user can do on each page. It can be reopened at any time with the ? button, starts at the current page (or at the welcome step on the home page), links to each page and lets the user change language without closing it.

## Tech stack
Expand Down
25 changes: 16 additions & 9 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,27 @@ The frontend is built with React and TypeScript (Vite).

- **Routing:** Routes are defined in `AppRoutes.tsx` with React Router, and the app is served under the base path `/project2/`. Vite defines this base in `frontend/vite.config.ts`; `HashRouter` provides the client-side routes, including Explore query parameters after the hash. All pages except the start page are lazy-loaded. Any path that does not match a route shows the 404 page, and the header shows the "Page not found" title for it.

| Route | Page | Purpose |
| ----------------- | -------------------- | ------------------------------------------------------------------------ |
| `/` | `PlantMatchPage` | Swipe through preference-ranked plants (`getPlants`) and match with them |
| `/explorePlants` | `ExplorePlantsPage` | Search, filter, sort and paginate all plants (`explorePlants`) |
| `/myMatches` | `MyMatchesPage` | List, sort, paginate and remove the user's matches (`myMatches`) |
| `/profile` | `ProfilePage` | Edit name, description, profile picture and preferences |
| `/plantOwnerTest` | `PlantOwnerTestPage` | A quiz whose result can be set as the profile picture |
| `*` | `NotFoundPage` | 404 page for unknown paths, with a link back to Plant Match |
| Route | Page | Purpose |
| ----------------- | -------------------- | ------------------------------------------------------------------------------------ |
| `/` | `PlantMatchPage` | Swipe through preference-ranked plants (`getPlants`) and match with them |
| `/explorePlants` | `ExplorePlantsPage` | Search, filter, sort and paginate all plants (`explorePlants`) |
| `/myMatches` | `MyMatchesPage` | List, sort, paginate and remove the user's matches (`myMatches`) |
| `/profile` | `ProfilePage` | Edit name, description, profile picture and preferences |
| `/plantOwnerTest` | `PlantOwnerTestPage` | A quiz whose result can be set as the profile picture, and that suggests preferences |
| `*` | `NotFoundPage` | 404 page for unknown paths, with a link back to Plant Match |

- **User state:** `UserProvider` (`context/UserContext.tsx`) loads the current user from the backend and makes it available through `useUser()`. The hooks `useProfile` and `useMatchedPlants` wrap the user mutations, so pages do not call GraphQL directly for user data. The user queries and mutations themselves live in `graphql/user/` (see [GraphQL operations](#graphql-operations-in-the-frontend)).
- **Error messages:** `ErrorMessageProvider` (`context/ErrorMessageContext.tsx`) wraps the app and makes `showError()` available through `useErrorMessage()`. `useMatchedPlants` uses it to show an error message when adding or removing a match fails.
- **Reusable logic in hooks:** Logic without UI is kept in hooks. `useDebounce` delays updates while the user is typing (used for search and for saving the profile text). `useExploreParams` keeps the Explore search, filters, sort order and page in the URL, and sends the user back to page 1 when the search, a filter or the sort order changes. It uses `useUrlParams`, which reads the page number and updates the URL (with React Router's `useSearchParams`). `useSessionState` stores state in `sessionStorage`, for example the sort order on My Matches.
- `usePlantDeck` keeps the remaining Plant Match cards in `sessionStorage` while the user visits another page. It sends the saved preferences with each batch, and changing the preferences clears the current deck and fetches a new first batch so the order reflects the new preferences. Stale responses from the previous deck are ignored.
- **Reusable components:** Shared UI is kept in `components/`. `Pagination` shows previous/next and numbered pages, `FilterSelect` is a labelled dropdown for one filter, and `SortMenu` is a sort button with a menu that can be used with the keyboard. `MatchButton` adds a plant to My Matches (✿) or removes it (✖), `ErrorMessage` shows a short error message, and `LanguageSwitcher` is used both in the sidebar and in the walkthrough. The components take already translated texts, so they can be used on other pages.
- **Plant Owner Test:** The questions, results and suggestions are data in `pages/plantOwnerTest/plantOwnerTest.ts`, so the page only shows them. `getPlantOwnerResult()` gives the result from the location and watering answers. `getSuggestedPreferences()` gives three preference ids from the answers and the result:
- **Watering** from the result: the minimalists get `minimumWater`, the average gardeners `averageWater`, and the dreaming city gardener and the garden expert `frequentWater`.
- **Light** from the light answer. There is no preference for partial sun, so it suggests `partShade`, since those plants are listed as "part sun/part shade" in the data and match the part shade preference.
- **Care level** from the result where its description says something about care: `easyCare` for the minimalists ("hardy plants") and `hardCare` for the dreaming city gardener and the garden expert ("a lot of attention"). The average gardeners get it from the time answer (10 minutes: `easyCare`, 30 minutes or 1 hour: `mediumCare`, 1 day: `hardCare`).

`SuggestedPreferences.tsx` shows the suggestions that the user does not already have as toggle buttons, all chosen from the start. Nothing is saved until the user clicks "Add to my preferences", which runs `updateUserPreferences` with the saved preferences plus the chosen suggestions, so no preference is removed. It uses an optimistic response like the buttons on the profile page, so the profile and Plant Match are updated at once. The suggestions are shown above "Use as profile picture", since that button takes the user to the profile page.

- **Walkthrough:** The guide for new users is in `components/walkthrough/`, and is made up of three parts:
- **The steps are data:** `walkthroughSteps.ts` lists the steps in order. Each step has an `id`, a picture (one of the avatars) and, if it is about a page, the `route` of that page. The texts are in the translation files under `walkthrough.steps.<id>`. A step can also have a `mobileHint`, a sentence that is only shown on small screens, such as how to open the menu with ☰, since the sidebar is always visible on large screens. It is hidden with CSS at the same breakpoint as the sidebar. A new step therefore only needs one line in the list and a title and text in each language. `stepForPath()` finds the step for the current page.
- **`App.tsx` decides when it is open:** The walkthrough opens by itself when `walkthroughSeen` is not in `localStorage`, and the key is set when it is closed, so it is only shown once. The ? button in the `Header` opens it again at the step for the current page (`stepForPath`). When it closes, focus goes back to the ? button, except when the "Go to" link has moved the user to another page, where focus moves to the start of the new page like for any other navigation.
Expand Down Expand Up @@ -312,7 +319,7 @@ T03-Project-2/
│ │ │ ├── myMatches/ # MyMatchesPage
│ │ │ ├── notFound/ # NotFoundPage
│ │ │ ├── plantMatch/ # PlantMatchPage
│ │ │ ├── plantOwnerTest/ # PlantOwnerTestPage, and plantOwnerTest.ts with the questions and results
│ │ │ ├── plantOwnerTest/ # PlantOwnerTestPage, SuggestedPreferences, and plantOwnerTest.ts with the questions, results and suggestions
│ │ │ └── profile/ # ProfilePage
│ │ ├── tests/ # Unit and component tests, including test setup (see tests.md)
│ │ ├── types/ # Shared TypeScript types (Plant, User, Preference)
Expand Down
34 changes: 23 additions & 11 deletions docs/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ The tests are in `frontend/src/tests`. Snapshot tests are only used for small co
- Preferences and edge cases: the user's saved preferences are sent with the request so the best matches come first, changing preferences starts a new deck with the updated ordering, also when they were changed on the profile page while PlantMatch was closed (the saved deck remembers which preferences it was sorted by), nothing is fetched before the user has loaded, and the end card with disabled buttons is shown when there are no plants at all.
- `PlantOwnerTestPage.test.tsx`
- The result for all six combinations of answers, and that the time and light answers do not change it.
- The suggested preferences for every result: watering and, for the minimalists and the plant lovers, care level from the result, care level from the time answer for the average gardeners, and partial sun suggesting part shade.
- Going on is only possible with an answer, an earlier answer is shown when going back, the result is shown with its picture, the result can be saved as profile picture, an error is shown when saving fails, the page says so when the result already is the profile picture, and the test can be taken again with no answers.
- Suggested preferences: all are chosen from the start, those already saved are not suggested again, the chosen ones are added to the saved preferences, nothing is saved before the user clicks the add button (which is disabled when nothing is chosen), the page says so when all suggestions already are preferences, an error is shown when saving fails or the user is not found, and saving can be tried again.
- `PlantPreferences.test.tsx`
- Saved preferences are marked, also with `aria-pressed` for screen readers, preferences can be added and removed, the page does not crash when saving fails, and nothing is saved before the user exists.
- `ProfilePage.test.tsx`
Expand Down Expand Up @@ -230,19 +232,20 @@ Other choices in the config:

#### Scenarios

Each file covers one scenario. Most features have both an API test and a UI test. The **API tests** check the backend logic over HTTP, including atypical input that is hard to reach through the UI. The **UI tests** check that the frontend is correctly connected to the backend and that the user journeys work from start to finish. Some UI tests have no API test of their own: accessibility and pictures are only about the frontend, and saving preferences is already covered by `apiUserProfile`, while the order of the plants is tested in the backend tests.
Each file covers one scenario. Most features have both an API test and a UI test. The **API tests** check the backend logic over HTTP, including atypical input that is hard to reach through the UI. The **UI tests** check that the frontend is correctly connected to the backend and that the user journeys work from start to finish. Some UI tests have no API test of their own: accessibility and pictures are only about the frontend, and saving preferences, also from the plant owner test, is already covered by `apiUserProfile`, while the order of the plants is tested in the backend tests.

The UI tests find elements the way a user or a screen reader would: by role, label and accessible name (`getByRole`, `getByLabel`), not by CSS classes or ids. A test then only breaks when something changes that the user notices, not when the styling is refactored, and an element without an accessible name cannot be found, so the tests also check that the controls are labelled. The app can run in Norwegian and English, so the names are written once in both languages in `e2e/helpers/names.ts`. Elements without a natural role, such as the plant cards in a list, the avatar pictures (decorative, `alt=""`) and the placeholder picture (a CSS background), have a `data-testid`.

| Feature | API test | UI test |
| ------------------------------------------ | ------------------------ | ----------------------- |
| Search, filter, sort, pagination, details | `apiExplorePlants` | `uiExplorePlants` |
| Profile and avatar | `apiUserProfile` | `uiUserProfile` |
| Matching, removing, sorting and paging | `apiMatchAndRemovePlant` | `uiMatchAndRemovePlant` |
| Accessibility on all pages | – | `uiAccessibility` |
| Pictures load | – | `uiImages` |
| Preferences decide the order in PlantMatch | – | `uiPreferences` |
| Walkthrough | – | `uiWalkthrough` |
| Feature | API test | UI test |
| --------------------------------------------- | ------------------------ | ----------------------- |
| Search, filter, sort, pagination, details | `apiExplorePlants` | `uiExplorePlants` |
| Profile and avatar | `apiUserProfile` | `uiUserProfile` |
| Matching, removing, sorting and paging | `apiMatchAndRemovePlant` | `uiMatchAndRemovePlant` |
| Accessibility on all pages | – | `uiAccessibility` |
| Pictures load | – | `uiImages` |
| Preferences decide the order in PlantMatch | – | `uiPreferences` |
| Walkthrough | – | `uiWalkthrough` |
| Preferences suggested by the plant owner test | – | `uiPlantOwnerTest` |

**ExplorePlants (`uiExplorePlants`):** one continuous user journey that tests pagination, filtering, search, the detail view, sorting and reset, and that changing a filter on page 2 takes the user back to page 1.

Expand All @@ -256,6 +259,15 @@ The test user has no preferences, so PlantMatch shows the plants in random order

**Preferences (`uiPreferences`):** the core feature of the app, from start to finish. A new user lands on PlantMatch, the start page, where the order is random. The user then chooses three preferences on the profile page, and back on PlantMatch the deck starts over with a plant that matches all three, which the card also says to screen readers on each matching chip. The order and the marked chips are the same after a reload, so the preferences are stored in the database and the deck in `sessionStorage`. When the user changes the preferences, PlantMatch starts over with the plant that matches the new ones. The test data has only one or two plants that match each set of preferences, so the expected plants are known. The other UI tests use a new user without preferences, where PlantMatch is random.

**Plant owner test (`uiPlantOwnerTest`):** the user takes the test from the sidebar with answers that give "Average country gardener", which suggests average watering, part shade and medium care, all chosen from the start.

- The user leaves out part shade and adds the others. The test waits for the request that saves exactly `averageWater` and `mediumCare` and checks that `partShade` is not in it, and the page says that the preferences were added.
- On the profile page, average watering and medium care are chosen and part shade is not, also after a reload, so the preferences are stored in the database.
- Taking the test again only suggests part shade, since the others are saved. Adding it keeps the saved preferences, and when all three are saved, the next result says so instead of showing the add button.
- A second test takes the test without adding anything, and checks that no preferences are sent to the backend and none are chosen on the profile page.

Saving preferences is already covered by `apiUserProfile`, and how preferences decide the order in PlantMatch by `uiPreferences`, so the test does not have an API test of its own.

**Profile (`uiUserProfile`):** changing name, description and avatar is shown at once and is still there after a reload, so the changes are stored in the database.

Saving the profile is debounced by 500 ms, and the text is also saved at once when a field loses focus. The test waits until the save request has reached the backend before reloading, instead of waiting a fixed time, so it does not depend on the length of the debounce.
Expand Down Expand Up @@ -284,7 +296,7 @@ Saving the profile is debounced by 500 ms, and the text is also saved at once wh
`uiAccessibility.spec.ts` runs **axe** through `@axe-core/playwright` against WCAG 2.1 level A and AA, the level required for the public sector in Norway. It checks:

- all six pages (PlantMatch, ExplorePlants, MyMatches, Profile, PlantOwnerTest and the 404 page), in both light and dark mode, since colour contrast has to meet the WCAG 2.1 requirements in both themes
- content that only appears after an interaction: a flipped plant card, the edit tab on the profile page, MyMatches with a matched plant, the result of the plant owner test, and the open walkthrough
- content that only appears after an interaction: a flipped plant card, the edit tab on the profile page, MyMatches with a matched plant, the result of the plant owner test with the suggested preferences (whose buttons are also checked for non-text contrast), and the open walkthrough

The tests run on both desktop and mobile, since the layout, and the elements that are visible, differ between them. A failing test lists each element that breaks a rule, with axe's explanation (for contrast: the colours and the ratio).

Expand Down
Loading
Loading