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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ Claude and ChatGPT are the main models used. The usecases for the AI tools are a
- [x] The lists in the interface: heart, bookmark and «Har prøvd» on the wines.
- [x] Profile page («Min side») with the top 4, the lists and the user's reviews.
- [x] Wishlist stored in the backend, so it follows the user between devices.
- [ ] Calmer landing page: less text, a separate «Om» page, and a burger menu on mobile.
- [ ] Calmer landing page: less text, and a separate «Om» page.
- [ ] CI, an enforced coverage threshold, and eslint-plugin-jsx-a11y.
- [x] Measure sustainability and fill in the budget table.
- [ ] Compress API responses and set cache header in Apache.
Expand Down
4 changes: 3 additions & 1 deletion frontend/e2e/landing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AxeBuilder from '@axe-core/playwright';
import { expect, test } from '@playwright/test';
import { stubGraphQL } from './support/graphql';
import { openMenuIfCollapsed, stubGraphQL } from './support/graphql';

/*
* E2E for forsiden. Kjøres mot `vite preview` (se playwright.config.ts).
Expand Down Expand Up @@ -55,6 +55,7 @@ test.describe('Forsiden', () => {
});

test('viser «Logg inn» i menyen når ingen er logget inn', async ({ page }) => {
await openMenuIfCollapsed(page);
await expect(
page.getByRole('navigation', { name: 'Hovedmeny' }).getByRole('link', { name: 'Logg inn' }),
).toBeVisible();
Expand Down Expand Up @@ -126,6 +127,7 @@ test.describe('Forsiden som innlogget', () => {
});
await page.goto('./');

await openMenuIfCollapsed(page);
await expect(
page.getByRole('navigation', { name: 'Hovedmeny' }).getByRole('link', { name: 'kari' }),
).toHaveAttribute('href', /\/konto$/);
Expand Down
63 changes: 63 additions & 0 deletions frontend/e2e/menu.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test, type Page } from '@playwright/test';
import { expectNoAxeViolations } from './support/axe';
import { fakeApi } from './support/fakeApi';

/*
* E2E for the header menu on a narrow screen. Runs against `vite preview` with the fake API in
* support/fakeApi.ts. The wine page is used because its header also has the search field.
*/
test.describe('Menyen på mobil', () => {
test.beforeEach(async ({ page }) => {
await page.setViewportSize({ width: 375, height: 700 });
await fakeApi(page, { loggedIn: false });
await page.goto('./vin/1');
await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
});

const menuButton = (page: Page) => page.getByRole('button', { name: 'Meny' });
const menu = (page: Page) => page.getByRole('navigation', { name: 'Hovedmeny' });

test('headeren tar én linje, og lenkene er skjult til menyen åpnes', async ({ page }) => {
const header = await page.getByRole('banner').boundingBox();
expect(header?.height).toBeLessThan(80);
await expect(menuButton(page)).toHaveAttribute('aria-expanded', 'false');
await expect(menu(page)).toBeHidden();
await expect(page.getByRole('search', { name: 'Søk i toppmenyen' })).toBeHidden();
});

test('åpnes med tastaturet, med «Utforsk» først, og lukkes med Escape', async ({ page }) => {
await menuButton(page).focus();
await page.keyboard.press('Enter');
await expect(menuButton(page)).toHaveAttribute('aria-expanded', 'true');
await expect(page.getByRole('search', { name: 'Søk i toppmenyen' })).toBeVisible();
await expect(menu(page).getByRole('link')).toHaveText(['Utforsk', 'Min side', 'Logg inn']);
await expectNoAxeViolations(page);

await page.keyboard.press('Escape');
await expect(menu(page)).toBeHidden();
await expect(menuButton(page)).toBeFocused();
});

test('lukkes når brukeren går til en annen side', async ({ page }) => {
await menuButton(page).click();
await menu(page).getByRole('link', { name: 'Utforsk' }).click();
await expect(page).toHaveURL(/\/sok$/);
await expect(menuButton(page)).toHaveAttribute('aria-expanded', 'false');
await expect(menu(page)).toBeHidden();
});

test('har ingen horisontal scroll ved 320 px med menyen åpen (WCAG 1.4.10)', async ({ page }) => {
await page.setViewportSize({ width: 320, height: 640 });
await menuButton(page).click();
const overflow = await page.evaluate(
() => document.documentElement.scrollWidth > window.innerWidth,
);
expect(overflow).toBe(false);
});

test('på bred skjerm vises lenkene uten menyknapp', async ({ page }) => {
await page.setViewportSize({ width: 1024, height: 700 });
await expect(menuButton(page)).toBeHidden();
await expect(menu(page).getByRole('link', { name: 'Utforsk' })).toBeVisible();
});
});
7 changes: 5 additions & 2 deletions frontend/e2e/support/fakeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export async function fakeApi(page: Page, { loggedIn = true } = {}) {
return { top: () => [...top] };
}

/** The header shows the username once the page knows who is logged in. Clicks wait for that. */
/**
* The header shows the username once the page knows who is logged in. Clicks wait for that.
* On a narrow screen the link is in the closed menu, so hidden links count too.
*/
export const loggedIn = (page: Page) =>
expect(page.getByRole('link', { name: 'kari' })).toBeAttached();
expect(page.getByRole('link', { name: 'kari', includeHidden: true })).toBeAttached();
6 changes: 6 additions & 0 deletions frontend/e2e/support/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ export async function stubGraphQL(page: Page, handlers: Record<string, Handler>)

return operations;
}

/** On a narrow screen the header links are behind the «Meny» button. Opens it if it is there. */
export async function openMenuIfCollapsed(page: Page) {
const button = page.getByRole('button', { name: 'Meny' });
if (await button.isVisible()) await button.click();
}
2 changes: 1 addition & 1 deletion frontend/e2e/wine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ test.describe('Detaljsiden', () => {
await page.goto('./vin/100062');
// The header shows the username once the page knows who is logged in. A click before
// that is ignored on purpose, so the test waits for it.
await expect(page.getByRole('link', { name: 'kari' })).toBeAttached();
await expect(page.getByRole('link', { name: 'kari', includeHidden: true })).toBeAttached();
const favourite = page.getByRole('button', { name: 'Favoritt' });

// With the keyboard: focus the button and press Space.
Expand Down
34 changes: 34 additions & 0 deletions frontend/src/components/layout/AppLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ describe('AppLayout', () => {
expect(screen.getByRole('search', { name: 'Søk i toppmenyen' })).toBeInTheDocument();
});

it('åpner og lukker menyen med «Meny»-knappen', async () => {
const user = userEvent.setup();
renderApp('/mine');
const button = screen.getByRole('button', { name: 'Meny' });
expect(button).toHaveAttribute('aria-expanded', 'false');
expect(button).toHaveAttribute('aria-controls', 'header-menu');

await user.click(button);
expect(button).toHaveAttribute('aria-expanded', 'true');
await user.click(button);
expect(button).toHaveAttribute('aria-expanded', 'false');
});

it('lukker menyen med Escape og setter fokus tilbake på knappen', async () => {
const user = userEvent.setup();
renderApp('/mine');
const button = screen.getByRole('button', { name: 'Meny' });
await user.click(button);
await user.tab();
expect(button).not.toHaveFocus();

await user.keyboard('{Escape}');
expect(button).toHaveAttribute('aria-expanded', 'false');
expect(button).toHaveFocus();
});

it('lukker menyen når brukeren går til en annen side', async () => {
const user = userEvent.setup();
renderApp('/mine');
await user.click(screen.getByRole('button', { name: 'Meny' }));
await user.click(screen.getByRole('link', { name: 'Utforsk' }));
expect(screen.getByRole('button', { name: 'Meny' })).toHaveAttribute('aria-expanded', 'false');
});

it('venter på h1 som dukker opp senere (lazy-lastede sider)', async () => {
const main = document.createElement('main');
document.body.append(main);
Expand Down
54 changes: 42 additions & 12 deletions frontend/src/components/layout/Header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,39 @@
text-decoration: none;
}

.search {
order: 3;
/* Narrow screens: the brand and the menu button share one line. The button opens the search
and the links below it. */
.menuButton {
display: inline-flex;
align-items: center;
gap: var(--space-2);
min-height: var(--tap-target);
margin-left: auto;
padding: 0 var(--space-4);
border: 1px solid currentcolor;
border-radius: var(--radius-pill);
background: transparent;
color: inherit;
font: inherit;
font-weight: 600;
cursor: pointer;
}

.menu {
display: none;
flex-basis: 100%;
flex-direction: column;
gap: var(--space-3);
padding-bottom: var(--space-2);
}

.nav {
margin-left: auto;
.menu[data-open='true'] {
display: flex;
}

.nav ul {
display: flex;
align-items: center;
flex-direction: column;
gap: var(--space-2);
margin: 0;
padding: 0;
Expand Down Expand Up @@ -75,18 +96,27 @@
background: var(--color-accent-strong);
}

/* Wide screens: no menu button. The search and the links sit on the line with the brand. */
@media (min-width: 48rem) {
.menuButton {
display: none;
}

.menu {
display: contents;
}

.search {
order: 0;
flex: 1;
flex-basis: auto;
max-width: 30rem;
}
}

/* Svært smale skjermer (320 px / 400 % zoom): skjul «Utforsk», søket dekker behovet. */
@media (max-width: 22rem) {
.nav li:first-child {
display: none;
.nav {
margin-left: auto;
}

.nav ul {
flex-direction: row;
align-items: center;
}
}
89 changes: 64 additions & 25 deletions frontend/src/components/layout/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Link, NavLink } from 'react-router';
import { useEffect, useRef, useState } from 'react';
import { Link, NavLink, useLocation } from 'react-router';
import { useAuth } from '../../features/auth/useAuth';
import { SearchForm } from '../ui/SearchForm';
import { LogoMark } from './Logo';
Expand All @@ -11,6 +12,22 @@ interface HeaderProps {

export function Header({ showSearch = true }: HeaderProps) {
const { user, loading } = useAuth();
const { key } = useLocation();
const menuButton = useRef<HTMLButtonElement>(null);
// The menu is open only on the page where it was opened, so every navigation closes it.
const [openedOn, setOpenedOn] = useState<string | null>(null);
const open = openedOn === key;

useEffect(() => {
if (!open) return;
const closeOnEscape = (event: KeyboardEvent) => {
if (event.key !== 'Escape') return;
setOpenedOn(null);
menuButton.current?.focus();
};
document.addEventListener('keydown', closeOnEscape);
return () => document.removeEventListener('keydown', closeOnEscape);
}, [open]);

return (
<header className={styles.header}>
Expand All @@ -20,34 +37,56 @@ export function Header({ showSearch = true }: HeaderProps) {
<span>Kork &amp; Krone</span>
</Link>

{showSearch && (
<div className={styles.search}>
<SearchForm label="Søk etter vin" hideLabel landmarkLabel="Søk i toppmenyen" />
</div>
)}
{/* Only shown on narrow screens. On wide screens the search and the links are always visible. */}
<button
ref={menuButton}
type="button"
className={styles.menuButton}
aria-expanded={open}
aria-controls="header-menu"
onClick={() => setOpenedOn(open ? null : key)}
>
<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" focusable="false">
<path
d={open ? 'M6 6l12 12M18 6L6 18' : 'M4 7h16M4 12h16M4 17h16'}
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
/>
</svg>
Meny
</button>

<div id="header-menu" className={styles.menu} data-open={open}>
{showSearch && (
<div className={styles.search}>
<SearchForm label="Søk etter vin" hideLabel landmarkLabel="Søk i toppmenyen" />
</div>
)}

<nav aria-label="Hovedmeny" className={styles.nav}>
<ul>
<li>
<NavLink to="/sok" className={styles.navLink}>
Utforsk
</NavLink>
</li>
<li>
<NavLink to="/mine" className={styles.cta}>
Min side
</NavLink>
</li>
{/* Nothing until we know who is logged in, so the menu does not flicker. */}
{!loading && (
<nav aria-label="Hovedmeny" className={styles.nav}>
<ul>
<li>
<NavLink to="/sok" className={styles.navLink}>
Utforsk
</NavLink>
</li>
<li>
<NavLink to="/konto" className={styles.navLink}>
{user ? user.username : 'Logg inn'}
<NavLink to="/mine" className={styles.cta}>
Min side
</NavLink>
</li>
)}
</ul>
</nav>
{/* Nothing until we know who is logged in, so the menu does not flicker. */}
{!loading && (
<li>
<NavLink to="/konto" className={styles.navLink}>
{user ? user.username : 'Logg inn'}
</NavLink>
</li>
)}
</ul>
</nav>
</div>
</div>
</header>
);
Expand Down