From 80ff17605fa7f25f2c4bfbfbaa04c339d87648e3 Mon Sep 17 00:00:00 2001 From: Kristian Date: Thu, 29 Oct 2026 14:57:57 +0100 Subject: [PATCH 1/5] fix: can scroll on map to zoom --- apps/frontend/src/components/Map.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/frontend/src/components/Map.tsx b/apps/frontend/src/components/Map.tsx index ab28da3..c29a4ea 100644 --- a/apps/frontend/src/components/Map.tsx +++ b/apps/frontend/src/components/Map.tsx @@ -67,7 +67,7 @@ export default function Map({ Date: Thu, 29 Oct 2026 15:40:23 +0100 Subject: [PATCH 2/5] add component test --- .github/workflows/ci.yaml | 3 + README.md | 11 + apps/frontend/package.json | 10 +- .../__tests__/AddReviewDialog.test.tsx | 183 +++++ .../components/__tests__/Breadcrumbs.test.tsx | 52 ++ .../src/components/__tests__/Footer.test.tsx | 16 + .../src/components/__tests__/Header.test.tsx | 19 + .../src/components/__tests__/Map.test.tsx | 158 +++++ .../__tests__/MockLocationNotice.test.tsx | 31 + .../__tests__/NearbyVenues.test.tsx | 138 ++++ .../components/__tests__/SearchBox.test.tsx | 225 ++++++ .../components/__tests__/ThemeToggle.test.tsx | 57 ++ .../components/__tests__/VenueCard.test.tsx | 79 +++ .../components/__tests__/ViewVenue.test.tsx | 92 +++ apps/frontend/src/test/fixtures.ts | 82 +++ apps/frontend/src/test/render.tsx | 83 +++ apps/frontend/src/test/setup.ts | 28 + apps/frontend/tsconfig.json | 4 +- apps/frontend/vitest.config.ts | 20 + package.json | 2 + pnpm-lock.yaml | 669 ++++++++++++++++++ 21 files changed, 1959 insertions(+), 3 deletions(-) create mode 100644 apps/frontend/src/components/__tests__/AddReviewDialog.test.tsx create mode 100644 apps/frontend/src/components/__tests__/Breadcrumbs.test.tsx create mode 100644 apps/frontend/src/components/__tests__/Footer.test.tsx create mode 100644 apps/frontend/src/components/__tests__/Header.test.tsx create mode 100644 apps/frontend/src/components/__tests__/Map.test.tsx create mode 100644 apps/frontend/src/components/__tests__/MockLocationNotice.test.tsx create mode 100644 apps/frontend/src/components/__tests__/NearbyVenues.test.tsx create mode 100644 apps/frontend/src/components/__tests__/SearchBox.test.tsx create mode 100644 apps/frontend/src/components/__tests__/ThemeToggle.test.tsx create mode 100644 apps/frontend/src/components/__tests__/VenueCard.test.tsx create mode 100644 apps/frontend/src/components/__tests__/ViewVenue.test.tsx create mode 100644 apps/frontend/src/test/fixtures.ts create mode 100644 apps/frontend/src/test/render.tsx create mode 100644 apps/frontend/src/test/setup.ts create mode 100644 apps/frontend/vitest.config.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e73b206..11c837c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,3 +37,6 @@ jobs: - name: Typecheck run: pnpm typecheck + + - name: Test + run: pnpm test diff --git a/README.md b/README.md index a690cdb..aa770fb 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,8 @@ Frontend: http://localhost:3000 · GraphQL API: http://localhost:3001 | `pnpm build` | Build all workspaces that define a build | | `pnpm typecheck` | Typecheck all workspaces | | `pnpm lint` / `pnpm format` / `pnpm check` | Lint, auto-fix format, and check formatting | +| `pnpm test` | Run the frontend component tests once | +| `pnpm test:watch` | Re-run tests while developing | | `pnpm db:setup` | Provision PostgreSQL natively on Ubuntu (see below) | | `pnpm db:generate` | Regenerate the Prisma client and Pothos types | | `pnpm db:push` | Push the Prisma schema to the database (prototyping) | @@ -109,6 +111,15 @@ pnpm format # Runs the prettier formatter pnpm check # Runs the prettier formatter in check mode, so will not write to any files ``` +### Testing + +The frontend uses [Vitest](https://vitest.dev/) with [Testing Library](https://testing-library.com/docs/react-testing-library/intro/) and jsdom. Component tests live in `apps/frontend/src/components/__tests__`, and shared fixtures and providers are in `apps/frontend/src/test`. The tests cover rendering, user interaction, form validation, routing and the map wrappers without needing a running backend. + +```bash +pnpm test # Run all component tests once +pnpm test:watch # Re-run tests while developing +``` + ### Deployment The deployment of both the backend and frontend is automated using the github action found in `./.github/workflows/deploy-to-vm.yaml` diff --git a/apps/frontend/package.json b/apps/frontend/package.json index 4171bf1..6edb098 100644 --- a/apps/frontend/package.json +++ b/apps/frontend/package.json @@ -10,6 +10,8 @@ "generate-routes": "tsr generate", "build": "vite build", "preview": "vite preview", + "test": "vitest run", + "test:watch": "vitest", "typecheck": "tsc --noEmit" }, "dependencies": { @@ -32,12 +34,18 @@ "@tailwindcss/typography": "^0.5.20", "@tanstack/devtools-vite": "latest", "@tanstack/router-cli": "^1.167.39", + "@testing-library/dom": "^10.4.2", + "@testing-library/jest-dom": "^7.0.1", + "@testing-library/react": "^16.3.3", + "@testing-library/user-event": "^14.6.8", "@types/leaflet": "^1.9.22", "@types/node": "^22.20.4", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", "@vitejs/plugin-react": "^6.1.1", + "jsdom": "^30.1.3", "typescript": "^6.0.3", - "vite": "^8.3.1" + "vite": "^8.3.1", + "vitest": "^5.0.5" } } diff --git a/apps/frontend/src/components/__tests__/AddReviewDialog.test.tsx b/apps/frontend/src/components/__tests__/AddReviewDialog.test.tsx new file mode 100644 index 0000000..ae3d639 --- /dev/null +++ b/apps/frontend/src/components/__tests__/AddReviewDialog.test.tsx @@ -0,0 +1,183 @@ +import { fireEvent, screen, waitFor } from '@testing-library/react' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { graphqlFetch } from '#/graphql/client' +import { CREATE_REVIEW } from '#/graphql/venues' +import { makeReview } from '#/test/fixtures' +import { renderWithProviders } from '#/test/render' +import AddReviewDialog from '../AddReviewDialog' + +vi.mock('#/graphql/client', () => ({ + graphqlFetch: vi.fn(), +})) + +const mockGraphqlFetch = vi.mocked(graphqlFetch) +const USERNAME_STORAGE_KEY = 'g-spotter:review-username' + +async function openDialog() { + const utils = renderWithProviders( + , + ) + const { user } = utils + + await user.click(await screen.findByRole('button', { name: 'Add review' })) + + return { ...utils, dialog: screen.getByRole('dialog') } +} + +function submitForm(dialog: HTMLElement) { + const form = dialog.querySelector('form') + + if (!form) throw new Error('The review dialog has no form') + + fireEvent.submit(form) +} + +async function fillValidReview( + user: ReturnType['user'], +) { + await user.type(screen.getByLabelText('Your name'), 'Kari') + await user.type(screen.getByLabelText('Your review'), 'Great beer') + await user.click(screen.getByRole('radio', { name: '4 out of 5 stars' })) +} + +describe('AddReviewDialog', () => { + beforeEach(() => { + mockGraphqlFetch.mockReset() + }) + + it('opens the dialog and restores a previously used name', async () => { + window.localStorage.setItem(USERNAME_STORAGE_KEY, 'Ola') + + const { dialog } = await openDialog() + + expect( + screen.getByRole('heading', { name: 'Add a review' }), + ).toBeInTheDocument() + expect(dialog).toHaveAttribute('open') + expect(screen.getByLabelText('Your name')).toHaveValue('Ola') + expect( + screen.getByText('Share your thoughts about Studentersamfundet.'), + ).toBeInTheDocument() + }) + + it('requires a name', async () => { + const { dialog } = await openDialog() + + submitForm(dialog) + + expect(await screen.findByRole('alert')).toHaveTextContent( + 'Please enter your name.', + ) + expect(screen.getByLabelText('Your name')).toHaveFocus() + }) + + it('requires a rating', async () => { + const { user, dialog } = await openDialog() + + await user.type(screen.getByLabelText('Your name'), 'Kari') + submitForm(dialog) + + expect(await screen.findByRole('alert')).toHaveTextContent( + 'Please choose a rating.', + ) + }) + + it('requires a review body', async () => { + const { user, dialog } = await openDialog() + + await user.type(screen.getByLabelText('Your name'), 'Kari') + await user.click(screen.getByRole('radio', { name: '4 out of 5 stars' })) + submitForm(dialog) + + expect(await screen.findByRole('alert')).toHaveTextContent( + 'Please write a short review.', + ) + }) + + it('submits a trimmed review, saves the name and closes the dialog', async () => { + mockGraphqlFetch.mockResolvedValue({ + createReview: makeReview({ + body: 'Great beer', + rating: 4, + username: 'Kari', + }), + }) + + const { user, dialog, queryClient } = await openDialog() + const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries') + + await user.type(screen.getByLabelText('Your name'), ' Kari ') + await user.type(screen.getByLabelText('Your review'), ' Great beer ') + await user.click(screen.getByRole('radio', { name: '4 out of 5 stars' })) + + submitForm(dialog) + + await waitFor(() => { + expect(mockGraphqlFetch).toHaveBeenCalledWith(CREATE_REVIEW, { + input: { + venueId: 'venue-1', + username: 'Kari', + body: 'Great beer', + rating: 4, + }, + }) + }) + + expect(window.localStorage.getItem(USERNAME_STORAGE_KEY)).toBe('Kari') + + await waitFor(() => { + expect(dialog).not.toHaveAttribute('open') + }) + + expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ['venues'] }) + }) + + it('shows the pending state while the review is being posted', async () => { + let resolveRequest: (value: { + createReview: ReturnType + }) => void = () => {} + + mockGraphqlFetch.mockReturnValue( + new Promise((resolve) => { + resolveRequest = resolve + }), + ) + + const { user, dialog } = await openDialog() + await fillValidReview(user) + submitForm(dialog) + + expect( + await screen.findByRole('button', { name: 'Posting…' }), + ).toBeDisabled() + + resolveRequest({ createReview: makeReview() }) + + await waitFor(() => { + expect(dialog).not.toHaveAttribute('open') + }) + }) + + it('shows an error and keeps the dialog open when posting fails', async () => { + mockGraphqlFetch.mockRejectedValue(new Error('Request failed')) + + const { user, dialog } = await openDialog() + await fillValidReview(user) + submitForm(dialog) + + expect(await screen.findByRole('alert')).toHaveTextContent( + 'Could not post your review. Please try again.', + ) + expect(dialog).toHaveAttribute('open') + expect(screen.getByRole('button', { name: 'Post review' })).toBeEnabled() + }) + + it('closes the dialog with the close button', async () => { + const { user, dialog } = await openDialog() + + await user.click(screen.getByRole('button', { name: 'Close dialog' })) + + expect(dialog).not.toHaveAttribute('open') + }) +}) diff --git a/apps/frontend/src/components/__tests__/Breadcrumbs.test.tsx b/apps/frontend/src/components/__tests__/Breadcrumbs.test.tsx new file mode 100644 index 0000000..f8b2d67 --- /dev/null +++ b/apps/frontend/src/components/__tests__/Breadcrumbs.test.tsx @@ -0,0 +1,52 @@ +import { screen, within } from '@testing-library/react' +import { describe, expect, it } from 'vitest' + +import Breadcrumbs from '../Breadcrumbs' +import { renderWithProviders } from '#/test/render' + +describe('Breadcrumbs', () => { + it('renders a link for items with a city and marks the last item as current', async () => { + renderWithProviders( + , + ) + + const navigation = await screen.findByRole('navigation', { + name: 'Breadcrumb', + }) + const items = within(navigation).getAllByRole('listitem') + + expect(items).toHaveLength(2) + expect( + within(items[0]).getByRole('link', { name: 'Trondheim' }), + ).toHaveAttribute('href', '/trondheim/venues') + expect(within(items[1]).getByText('Studentersamfundet')).toHaveAttribute( + 'aria-current', + 'page', + ) + }) + + it('does not mark an item without a city as current when it is not last', async () => { + renderWithProviders( + , + ) + + expect(await screen.findByText('Sentrum')).not.toHaveAttribute( + 'aria-current', + ) + expect(screen.getByText('Studentersamfundet')).toHaveAttribute( + 'aria-current', + 'page', + ) + }) +}) diff --git a/apps/frontend/src/components/__tests__/Footer.test.tsx b/apps/frontend/src/components/__tests__/Footer.test.tsx new file mode 100644 index 0000000..98099cc --- /dev/null +++ b/apps/frontend/src/components/__tests__/Footer.test.tsx @@ -0,0 +1,16 @@ +import { render, screen } from '@testing-library/react' +import { describe, expect, it } from 'vitest' + +import Footer from '../Footer' + +describe('Footer', () => { + it('renders the copyright notice with the current year', () => { + render(