From 1a6897cbbd27c0fe8b2c2b0ef3025e5e8384501d Mon Sep 17 00:00:00 2001 From: Oscar Bakkeng Date: Fri, 6 Nov 2026 18:32:27 +0100 Subject: [PATCH 01/14] chore(frontend): add Apollo Client and fix dev proxy (#27) The frontend calls /project2/graphql because BASE_URL is part of the path, so the dev proxy now matches that path instead of /graphql. Co-Authored-By: Claude Opus 5.5 --- frontend/package-lock.json | 148 +++++++++++++++++++++++++++++++++++++ frontend/package.json | 2 + frontend/src/lib/apollo.ts | 7 ++ frontend/src/main.tsx | 6 +- frontend/vite.config.ts | 6 +- 5 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 frontend/src/lib/apollo.ts diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 1fc2a06..f5cf5ad 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -8,6 +8,8 @@ "name": "t18-project-2", "version": "0.0.0", "dependencies": { + "@apollo/client": "^4.3.7", + "graphql": "^16.15.0", "react": "^19.2.8", "react-dom": "^19.2.8", "react-router": "^8.5.1" @@ -49,6 +51,43 @@ "dev": true, "license": "MIT" }, + "node_modules/@apollo/client": { + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/@apollo/client/-/client-4.3.7.tgz", + "integrity": "sha512-s+aetmVMhR/UkpsnyKqJb9uQBSv0c42Nb2S517H4Zbqc7GTdKNH/Su+Zv6euR+OHL1YosOhUm3DhvuQUi8Mxtg==", + "license": "MIT", + "dependencies": { + "@graphql-typed-document-node/core": "^3.1.1", + "@wry/caches": "^1.0.0", + "@wry/equality": "^0.5.6", + "@wry/trie": "^0.5.0", + "graphql-tag": "^2.12.6", + "optimism": "^0.18.0", + "tslib": "^2.3.0" + }, + "peerDependencies": { + "graphql": "^16.0.0 || ^17.0.0", + "graphql-ws": "^5.5.5 || ^6.0.3", + "react": "^17.0.0 || ^18.0.0 || >=19.0.0-rc", + "react-dom": "^17.0.0 || ^18.0.0 || >=19.0.0-rc", + "rxjs": "^7.3.0", + "subscriptions-transport-ws": "^0.9.0 || ^0.11.0" + }, + "peerDependenciesMeta": { + "graphql-ws": { + "optional": true + }, + "react": { + "optional": true + }, + "react-dom": { + "optional": true + }, + "subscriptions-transport-ws": { + "optional": true + } + } + }, "node_modules/@asamuzakjp/css-color": { "version": "7.1.9", "resolved": "https://registry.npmjs.org/@asamuzakjp/css-color/-/css-color-7.1.9.tgz", @@ -701,6 +740,15 @@ } } }, + "node_modules/@graphql-typed-document-node/core": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@graphql-typed-document-node/core/-/core-3.2.0.tgz", + "integrity": "sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==", + "license": "MIT", + "peerDependencies": { + "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0" + } + }, "node_modules/@humanfs/core": { "version": "0.19.2", "resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.2.tgz", @@ -1766,6 +1814,54 @@ "url": "https://opencollective.com/vitest" } }, + "node_modules/@wry/caches": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@wry/caches/-/caches-1.0.1.tgz", + "integrity": "sha512-bXuaUNLVVkD20wcGBWRyo7j9N3TxePEWFZj2Y+r9OoUzfqmavM84+mFykRicNsBqatba5JLay1t48wxaXaWnlA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@wry/context": { + "version": "0.7.4", + "resolved": "https://registry.npmjs.org/@wry/context/-/context-0.7.4.tgz", + "integrity": "sha512-jmT7Sb4ZQWI5iyu3lobQxICu2nC/vbUhP0vIdd6tHC9PTfenmRmuIFqktc6GH9cgi+ZHnsLWPvfSvc4DrYmKiQ==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@wry/equality": { + "version": "0.5.7", + "resolved": "https://registry.npmjs.org/@wry/equality/-/equality-0.5.7.tgz", + "integrity": "sha512-BRFORjsTuQv5gxcXsuDXx6oGRhuVsEGwZy6LOzRRfgu+eSfxbhUQ9L9YtSEIuIjY/o7g3iWFjrc5eSY1GXP2Dw==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@wry/trie": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/@wry/trie/-/trie-0.5.0.tgz", + "integrity": "sha512-FNoYzHawTMk/6KMQoEG5O4PuioX19UbwdQKF44yw0nLfOypfQdjtfZzo/UIJWAJ23sNIFbD1Ug9lbaDGMwbqQA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/acorn": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.18.0.tgz", @@ -2560,6 +2656,30 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/graphql": { + "version": "16.15.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-16.15.0.tgz", + "integrity": "sha512-32kz9FecohDiC/tgcViwnRXM7HFBVTPiW7pHO+DikKOI5KjhU/jNULOReXsrFqt2WAYbE9xFXsTffJw6iTO8qw==", + "license": "MIT", + "engines": { + "node": "^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0" + } + }, + "node_modules/graphql-tag": { + "version": "2.12.7", + "resolved": "https://registry.npmjs.org/graphql-tag/-/graphql-tag-2.12.7.tgz", + "integrity": "sha512-xnE/NFzy+0eIesvAsREJZ284zTl/wYuBAvpsFSDhRGRdRHdnE90M21Q3xAWyYInb0J756c6x0pIQ62+vtvOs1Q==", + "license": "MIT", + "dependencies": { + "tslib": "^2.1.0" + }, + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0" + } + }, "node_modules/hashery": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/hashery/-/hashery-1.5.1.tgz", @@ -3215,6 +3335,18 @@ "node": ">=12.20.0" } }, + "node_modules/optimism": { + "version": "0.18.1", + "resolved": "https://registry.npmjs.org/optimism/-/optimism-0.18.1.tgz", + "integrity": "sha512-mLXNwWPa9dgFyDqkNi54sjDyNJ9/fTI6WGBLgnXku1vdKY/jovHfZT5r+aiVeFFLOz+foPNOm5YJ4mqgld2GBQ==", + "license": "MIT", + "dependencies": { + "@wry/caches": "^1.0.0", + "@wry/context": "^0.7.0", + "@wry/trie": "^0.5.0", + "tslib": "^2.3.0" + } + }, "node_modules/optionator": { "version": "0.9.4", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz", @@ -3564,6 +3696,16 @@ "@rolldown/binding-win32-x64-msvc": "1.2.16" } }, + "node_modules/rxjs": { + "version": "7.8.2", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.2.tgz", + "integrity": "sha512-dhKf903U/PQZY6boNNtAGdWbG85WAbjT/1xYoZIC7FAY0yWapOBQVsVrDl58W86//e1VpMNBtRV4MaXfdMySFA==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "tslib": "^2.1.0" + } + }, "node_modules/saxes": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/saxes/-/saxes-6.0.0.tgz", @@ -3751,6 +3893,12 @@ "typescript": ">=4.8.4" } }, + "node_modules/tslib": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.9.1.tgz", + "integrity": "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA==", + "license": "0BSD" + }, "node_modules/type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", diff --git a/frontend/package.json b/frontend/package.json index aa3b70c..f96215a 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -18,6 +18,8 @@ "e2e": "playwright test" }, "dependencies": { + "@apollo/client": "^4.3.7", + "graphql": "^16.15.0", "react": "^19.2.8", "react-dom": "^19.2.8", "react-router": "^8.5.1" diff --git a/frontend/src/lib/apollo.ts b/frontend/src/lib/apollo.ts new file mode 100644 index 0000000..3e92b94 --- /dev/null +++ b/frontend/src/lib/apollo.ts @@ -0,0 +1,7 @@ +import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'; + +/** BASE_URL makes the endpoint /project2/graphql both on the VM and in development. */ +export const apolloClient = new ApolloClient({ + link: new HttpLink({ uri: `${import.meta.env.BASE_URL}graphql` }), + cache: new InMemoryCache(), +}); diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index a717243..0c9d8f3 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -1,10 +1,14 @@ +import { ApolloProvider } from '@apollo/client/react'; import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; import App from './app/App'; +import { apolloClient } from './lib/apollo'; import './styles/global.css'; createRoot(document.getElementById('root')!).render( - + + + , ); diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index aa9b398..34e8383 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -11,7 +11,11 @@ export default defineConfig({ target: 'http://localhost:3001', rewrite: () => '/', }, - '/graphql': 'http://localhost:3001', + // BASE_URL is part of the path, so the frontend calls /project2/graphql. + '/project2/graphql': { + target: 'http://localhost:3001', + rewrite: () => '/graphql', + }, }, }, }); From 3497a526f727ccf1f59e8b64a3986c1e7bb7bf5b Mon Sep 17 00:00:00 2001 From: Oscar Bakkeng Date: Fri, 6 Nov 2026 18:32:27 +0100 Subject: [PATCH 02/14] feat(landing): load landing page from the GraphQL API (#27) Replace the mock data with one query built on wines and filterOptions. A mapper converts API data to the frontend types. Content the API cannot provide yet (recent reviews, student ratings) is hidden, and the favorites section is renamed because its ratings come from X-Wines. The mock data is kept as a test fixture, and e2e tests stub the GraphQL response. Co-Authored-By: Claude Opus 5.5 --- frontend/e2e/landing.spec.ts | 45 +++++++++- frontend/src/components/ui/WineCard.test.tsx | 6 ++ frontend/src/components/ui/WineCard.tsx | 14 ++-- .../src/features/landing/LandingView.test.tsx | 33 +++++--- frontend/src/features/landing/LandingView.tsx | 29 ++++--- .../src/features/landing/api/landingQuery.ts | 66 ++++++--------- .../landing/api/toLandingData.test.ts | 50 +++++++++++ .../src/features/landing/api/toLandingData.ts | 32 +++++++ .../landing/api/useLandingData.test.ts | 41 --------- .../landing/api/useLandingData.test.tsx | 84 +++++++++++++++++++ .../features/landing/api/useLandingData.ts | 71 +++------------- .../features/landing/components/StatsBar.tsx | 5 +- frontend/src/lib/mapWine.test.ts | 55 ++++++++++++ frontend/src/lib/mapWine.ts | 47 +++++++++++ frontend/src/lib/types.ts | 9 +- .../mockLanding.ts => test/landingFixture.ts} | 14 +--- 16 files changed, 407 insertions(+), 194 deletions(-) create mode 100644 frontend/src/features/landing/api/toLandingData.test.ts create mode 100644 frontend/src/features/landing/api/toLandingData.ts delete mode 100644 frontend/src/features/landing/api/useLandingData.test.ts create mode 100644 frontend/src/features/landing/api/useLandingData.test.tsx create mode 100644 frontend/src/lib/mapWine.test.ts create mode 100644 frontend/src/lib/mapWine.ts rename frontend/src/{features/landing/api/mockLanding.ts => test/landingFixture.ts} (88%) diff --git a/frontend/e2e/landing.spec.ts b/frontend/e2e/landing.spec.ts index 7485388..19ddd6d 100644 --- a/frontend/e2e/landing.spec.ts +++ b/frontend/e2e/landing.spec.ts @@ -3,13 +3,54 @@ import { expect, test } from '@playwright/test'; /* * E2E for forsiden. Kjøres mot `vite preview` (se playwright.config.ts). - * Når backend er koblet på, kjør mot test-databasen med X-Wines Test. + * GraphQL-svaret er stubbet, så testene ikke trenger en kjørende backend. */ +const wines = Array.from({ length: 6 }, (_, i) => ({ + __typename: 'Wine', + id: String(i + 1), + name: `Testvin ${i + 1}`, + type: 'Red', + grapes: ['Pinot Noir'], + country: 'France', + wineryName: 'Testprodusent', + communityRating: 4.5, + communityRatingCount: 10, +})); + +const landingResponse = { + data: { + topRated: { + __typename: 'WineConnection', + totalCount: 100, + edges: wines.map((node) => ({ __typename: 'WineEdge', node })), + }, + filterOptions: { + __typename: 'FilterOptions', + types: [{ __typename: 'FilterOption', value: 'Red', count: 100 }], + countries: [{ __typename: 'FilterOption', value: 'FR' }], + }, + }, +}; + test.describe('Forsiden', () => { + let graphqlRequests = 0; + test.beforeEach(async ({ page }) => { + graphqlRequests = 0; + await page.route('**/project2/graphql', (route) => { + graphqlRequests += 1; + return route.fulfill({ json: landingResponse }); + }); await page.goto('./'); }); + test('henter hele forsiden i én GraphQL-forespørsel', async ({ page }) => { + await expect( + page.getByRole('region', { name: 'Best vurdert' }).getByRole('article'), + ).toHaveCount(6); + expect(graphqlRequests).toBe(1); + }); + test('forklarer hva siden er og har riktig tittel', async ({ page }) => { await expect(page).toHaveTitle(/Kork & Krone/); await expect(page.getByRole('heading', { level: 1 })).toHaveText( @@ -32,7 +73,7 @@ test.describe('Forsiden', () => { test('har ingen axe-brudd (WCAG 2.1 AA)', async ({ page }) => { await expect( - page.getByRole('region', { name: 'Medstudentenes favoritter' }).getByRole('article'), + page.getByRole('region', { name: 'Best vurdert' }).getByRole('article'), ).toHaveCount(6); const results = await new AxeBuilder({ page }) .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']) diff --git a/frontend/src/components/ui/WineCard.test.tsx b/frontend/src/components/ui/WineCard.test.tsx index c178c5e..e473529 100644 --- a/frontend/src/components/ui/WineCard.test.tsx +++ b/frontend/src/components/ui/WineCard.test.tsx @@ -39,6 +39,12 @@ describe('WineCard', () => { expect(screen.getByText('Ingen dommer ennå')).toBeInTheDocument(); }); + it('skjuler studentraden når vinen ikke har studentdata', () => { + renderWithRouter(); + expect(screen.queryByText('Studenter')).not.toBeInTheDocument(); + expect(screen.getByText('Fellesskapet')).toBeInTheDocument(); + }); + it('«Vil prøve» er en toggle-knapp med vinnavnet i navnet', async () => { const { user } = renderWithRouter(); const button = screen.getByRole('button', { name: 'Vil prøve Mosel Riesling Kabinett' }); diff --git a/frontend/src/components/ui/WineCard.tsx b/frontend/src/components/ui/WineCard.tsx index bb4711a..6b9fa09 100644 --- a/frontend/src/components/ui/WineCard.tsx +++ b/frontend/src/components/ui/WineCard.tsx @@ -42,12 +42,14 @@ export function WineCard({ wine, headingLevel: Heading = 'h3' }: WineCardProps)

{wine.winery}

-
-
Studenter
-
- {formatRatingSummary(wine.students, { singular: 'dom', plural: 'dommer' })} -
-
+ {wine.students && ( +
+
Studenter
+
+ {formatRatingSummary(wine.students, { singular: 'dom', plural: 'dommer' })} +
+
+ )}
Fellesskapet
{formatRatingSummary(wine.community)}
diff --git a/frontend/src/features/landing/LandingView.test.tsx b/frontend/src/features/landing/LandingView.test.tsx index 1610823..e6592fa 100644 --- a/frontend/src/features/landing/LandingView.test.tsx +++ b/frontend/src/features/landing/LandingView.test.tsx @@ -1,13 +1,13 @@ import { describe, expect, it, vi } from 'vitest'; import { screen, within } from '@testing-library/react'; import { axe } from 'vitest-axe'; +import { landingFixture } from '../../test/landingFixture'; import { renderWithRouter } from '../../test/render'; -import { mockLandingData } from './api/mockLanding'; import { LandingView } from './LandingView'; const now = new Date('2026-10-30T12:00:00Z'); -function renderLoaded(data = mockLandingData) { +function renderLoaded(data = landingFixture) { return renderWithRouter(); } @@ -29,7 +29,7 @@ describe('LandingView – innhold', () => { it('har ett h1 og seksjoner med h2', () => { renderLoaded(); expect(screen.getAllByRole('heading', { level: 1 })).toHaveLength(1); - for (const name of ['Utforsk etter type', 'Medstudentenes favoritter', 'Nylig dømt']) { + for (const name of ['Utforsk etter type', 'Best vurdert', 'Nylig dømt']) { expect(screen.getByRole('region', { name })).toBeInTheDocument(); } }); @@ -66,7 +66,7 @@ describe('LandingView – innhold', () => { it('skjuler kategorier uten viner', () => { renderLoaded({ - ...mockLandingData, + ...landingFixture, categories: [ { type: 'RED', count: 10 }, { type: 'FORTIFIED', count: 0 }, @@ -78,7 +78,7 @@ describe('LandingView – innhold', () => { it('viser seks favoritter som vinkort', () => { renderLoaded(); - const region = screen.getByRole('region', { name: 'Medstudentenes favoritter' }); + const region = screen.getByRole('region', { name: 'Best vurdert' }); expect(within(region).getAllByRole('article')).toHaveLength(6); expect(within(region).getByRole('link', { name: 'Se hele topplisten' })).toHaveAttribute( 'href', @@ -103,14 +103,14 @@ describe('LandingView – innhold', () => { it('viser nøkkeltall', () => { renderLoaded(); expect(screen.getByText('viner å utforske')).toBeInTheDocument(); - expect(screen.getByText('dommer fra studenter')).toBeInTheDocument(); + expect(screen.getByText('land representert')).toBeInTheDocument(); }); }); describe('LandingView – tilstander', () => { it('viser lastetilstand i en live-region, men hero og søk med én gang', () => { renderWithRouter(); - expect(screen.getByRole('status')).toHaveTextContent('Laster favoritter og nye dommer'); + expect(screen.getByRole('status')).toHaveTextContent('Laster viner'); expect( screen.getByRole('searchbox', { name: 'Hva har du lyst til å smake?' }), ).toBeInTheDocument(); @@ -127,15 +127,24 @@ describe('LandingView – tilstander', () => { refetch={refetch} />, ); - expect(screen.getByRole('alert')).toHaveTextContent('Vi fikk ikke hentet favorittene'); + expect(screen.getByRole('alert')).toHaveTextContent('Vi fikk ikke hentet vinene'); await user.click(screen.getByRole('button', { name: 'Prøv igjen' })); expect(refetch).toHaveBeenCalledOnce(); }); - it('har tomtilstander når det ikke finnes data ennå', () => { - renderLoaded({ ...mockLandingData, topRated: [], recentReviews: [] }); - expect(screen.getByText('Ingen viner har fått nok dommer ennå.')).toBeInTheDocument(); - expect(screen.getByText('Ingen har gitt en dom ennå. Bli den første!')).toBeInTheDocument(); + it('har tomtilstand når det ikke finnes viner', () => { + renderLoaded({ ...landingFixture, topRated: [] }); + expect(screen.getByText('Vi fant ingen viner å vise akkurat nå.')).toBeInTheDocument(); + }); + + it('skjuler «Nylig dømt» når det ikke finnes dommer', () => { + renderLoaded({ ...landingFixture, recentReviews: [] }); + expect(screen.queryByRole('region', { name: 'Nylig dømt' })).not.toBeInTheDocument(); + }); + + it('viser ikke skjelett for «Nylig dømt» mens data lastes', () => { + renderWithRouter(); + expect(screen.queryByRole('region', { name: 'Nylig dømt' })).not.toBeInTheDocument(); }); }); diff --git a/frontend/src/features/landing/LandingView.tsx b/frontend/src/features/landing/LandingView.tsx index 7c7e7df..041698a 100644 --- a/frontend/src/features/landing/LandingView.tsx +++ b/frontend/src/features/landing/LandingView.tsx @@ -29,7 +29,7 @@ export function LandingView({ data, loading, error, refetch, now }: LandingViewP {/* Live-region finnes alltid i DOM-en, så endringer annonseres (WCAG 4.1.3). */}

- {loading ? 'Laster favoritter og nye dommer …' : ''} + {loading ? 'Laster viner …' : ''}

{data && } @@ -38,7 +38,7 @@ export function LandingView({ data, loading, error, refetch, now }: LandingViewP {error ? (
-

Vi fikk ikke hentet favorittene og de nyeste dommene akkurat nå.

+

Vi fikk ikke hentet vinene akkurat nå.

{refetch && (