From e24c99dd06882a6b43848f473eed60f74630c56c Mon Sep 17 00:00:00 2001 From: SivertEikrem Date: Fri, 13 Nov 2026 11:45:27 +0100 Subject: [PATCH 1/2] fix(reviews): keep the status message visible to screen readers (#51) --- .../wine/components/StudentReviews.module.css | 4 - .../wine/components/StudentReviews.test.tsx | 170 ++++++++++++++++++ .../wine/components/StudentReviews.tsx | 2 +- 3 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 frontend/src/features/wine/components/StudentReviews.test.tsx diff --git a/frontend/src/features/wine/components/StudentReviews.module.css b/frontend/src/features/wine/components/StudentReviews.module.css index 760a067..9cbcf4e 100644 --- a/frontend/src/features/wine/components/StudentReviews.module.css +++ b/frontend/src/features/wine/components/StudentReviews.module.css @@ -10,10 +10,6 @@ font-weight: 600; } -.message:empty { - display: none; -} - .list { display: grid; gap: var(--space-3); diff --git a/frontend/src/features/wine/components/StudentReviews.test.tsx b/frontend/src/features/wine/components/StudentReviews.test.tsx new file mode 100644 index 0000000..a9cf463 --- /dev/null +++ b/frontend/src/features/wine/components/StudentReviews.test.tsx @@ -0,0 +1,170 @@ +import { describe, expect, it } from 'vitest'; +import { render, screen, waitFor, within } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import type { MockLink } from '@apollo/client/testing'; +import { MockedProvider } from '@apollo/client/testing/react'; +import { axe } from 'vitest-axe'; +import { USER_ID_KEY } from '../../../hooks/useUserId'; +import { createApolloCache } from '../../../lib/apollo'; +import { + DELETE_REVIEW_MUTATION, + SAVE_REVIEW_MUTATION, + WINE_REVIEWS_QUERY, +} from '../api/reviewQueries'; +import { StudentReviews } from './StudentReviews'; + +const WINE_ID = '100062'; +const USER_ID = '3f2a9c1e-8b4d-4e2a-9f1c-2b7d6e5a4c3b'; +const NOW = new Date('2026-11-13T12:00:00.000Z'); + +const review = (rating: number, comment: string | null, id = 'r1') => ({ + __typename: 'Review', + id, + rating, + comment, + author: 'Student #1', + updatedAt: '2026-11-13T11:00:00.000Z', +}); + +/** One answer to the reviews query: the wine's reviews and the user's own review. */ +function reviews( + list: ReturnType[], + mine: { id: string; rating: number; comment: string | null } | null = null, + distribution: { rating: number; count: number }[] = [], +): MockLink.MockedResponse { + const ratings = list.map((r) => r.rating); + return { + request: { query: WINE_REVIEWS_QUERY, variables: { id: WINE_ID, userId: USER_ID } }, + result: { + data: { + wine: { + __typename: 'Wine', + id: WINE_ID, + studentRating: { + __typename: 'StudentRating', + average: ratings.length ? ratings.reduce((a, b) => a + b, 0) / ratings.length : null, + count: ratings.length, + }, + ratingDistribution: distribution.map((d) => ({ __typename: 'RatingCount', ...d })), + reviews: list, + }, + myReview: mine && { __typename: 'Review', ...mine }, + }, + }, + }; +} + +function renderReviews(mocks: MockLink.MockedResponse[]) { + window.localStorage.setItem(USER_ID_KEY, USER_ID); + const user = userEvent.setup(); + const result = render( + + + , + ); + return { user, ...result }; +} + +/** The reviews are in an
    , the distribution in a
      , so this picks the reviews. */ +const reviewList = () => screen.findByRole('list', { name: 'Studentenes dommer' }); + +describe('StudentReviews', () => { + it('shows the average, the distribution and the newest reviews', async () => { + renderReviews([ + reviews([review(4, 'God til pizza'), review(3, null, 'r2')], null, [ + { rating: 4, count: 1 }, + { rating: 3, count: 1 }, + ]), + ]); + + expect(await screen.findByText(/av 5 · 2 dommer/)).toBeInTheDocument(); + expect(screen.getByRole('heading', { name: 'Alle vurderinger' })).toBeInTheDocument(); + expect(within(await reviewList()).getAllByRole('listitem')).toHaveLength(2); + expect(screen.getByText('God til pizza')).toBeInTheDocument(); + }); + + it('explains when nobody has reviewed the wine', async () => { + renderReviews([reviews([])]); + expect(await screen.findByText(/Ingen studenter har dømt denne vinen/)).toBeInTheDocument(); + }); + + it('saves a review and reports it in a live region that screen readers follow', async () => { + const saved = { id: 'r9', rating: 3.5, comment: null }; + const { user } = renderReviews([ + reviews([]), + { + request: { + query: SAVE_REVIEW_MUTATION, + variables: { input: { wineId: WINE_ID, userId: USER_ID, rating: 3.5, comment: null } }, + }, + result: { data: { saveReview: { __typename: 'Review', ...saved } } }, + }, + reviews([review(3.5, null, 'r9')], saved), + ]); + + await user.click(await screen.findByRole('radio', { name: '3,5 av 5 stjerner' })); + + // The live region must already be in the DOM, or the message is never announced. + const status = screen.getByRole('status'); + expect(status).toBeEmptyDOMElement(); + + await user.click(screen.getByRole('button', { name: 'Lagre dommen' })); + + expect(await screen.findByRole('button', { name: 'Oppdater dommen' })).toBeInTheDocument(); + expect(screen.getByRole('status')).toHaveTextContent('Dommen er lagret.'); + }); + + it('tells the user when saving fails', async () => { + const { user } = renderReviews([ + reviews([]), + { + request: { + query: SAVE_REVIEW_MUTATION, + variables: { input: { wineId: WINE_ID, userId: USER_ID, rating: 5, comment: null } }, + }, + error: new Error('Nede'), + }, + ]); + + await user.click(await screen.findByRole('radio', { name: '5 av 5 stjerner' })); + await user.click(screen.getByRole('button', { name: 'Lagre dommen' })); + + await waitFor(() => + expect(screen.getByRole('status')).toHaveTextContent('Vi fikk ikke lagret dommen'), + ); + }); + + it('deletes the user\u2019s own review', async () => { + const mine = { id: 'r1', rating: 4, comment: 'God' }; + const { user } = renderReviews([ + reviews([review(4, 'God')], mine), + { + request: { query: DELETE_REVIEW_MUTATION, variables: { wineId: WINE_ID, userId: USER_ID } }, + result: { data: { deleteReview: true } }, + }, + reviews([]), + ]); + + await user.click(await screen.findByRole('button', { name: 'Slett dommen' })); + await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Dommen er slettet.')); + }); + + it('offers a new attempt when the reviews cannot be fetched', async () => { + renderReviews([ + { + request: { query: WINE_REVIEWS_QUERY, variables: { id: WINE_ID, userId: USER_ID } }, + error: new Error('Nede'), + }, + ]); + expect(await screen.findByRole('alert')).toHaveTextContent('Vi fikk ikke hentet dommene'); + expect(screen.getByRole('button', { name: 'Prøv igjen' })).toBeInTheDocument(); + }); + + it('has no axe violations', async () => { + const { container } = renderReviews([ + reviews([review(4, 'God')], null, [{ rating: 4, count: 1 }]), + ]); + await reviewList(); + expect(await axe(container)).toHaveNoViolations(); + }); +}); diff --git a/frontend/src/features/wine/components/StudentReviews.tsx b/frontend/src/features/wine/components/StudentReviews.tsx index f6a59b9..f211ba2 100644 --- a/frontend/src/features/wine/components/StudentReviews.tsx +++ b/frontend/src/features/wine/components/StudentReviews.tsx @@ -77,7 +77,7 @@ export function StudentReviews({ wineId, now }: StudentReviewsProps) {

      {reviews.length > 0 ? ( -
        +
          {reviews.map((review) => (
        1. {' '} From ac022a242f1cc47c62a137bac32b28082cfde295 Mon Sep 17 00:00:00 2001 From: SivertEikrem Date: Sat, 14 Nov 2026 09:21:30 +0100 Subject: [PATCH 2/2] fix(reviews): keep the live region mounted while reviews are refetched (#51) --- .../src/features/wine/api/useWineReviews.ts | 3 ++ .../wine/components/StudentReviews.module.css | 24 +++++++++++++++ .../wine/components/StudentReviews.tsx | 30 ++++++++++++------- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/frontend/src/features/wine/api/useWineReviews.ts b/frontend/src/features/wine/api/useWineReviews.ts index 24a5da6..69fa51e 100644 --- a/frontend/src/features/wine/api/useWineReviews.ts +++ b/frontend/src/features/wine/api/useWineReviews.ts @@ -20,6 +20,8 @@ export interface WineReviewsResult { reviews: ApiReview[]; distribution: RatingCount[]; myReview: WineReviewsData['myReview']; + /** True once the reviews have been fetched. Stays true while they are fetched again. */ + hasData: boolean; loading: boolean; error: Error | undefined; refetch: () => void; @@ -83,6 +85,7 @@ export function useWineReviews(wineId: string): WineReviewsResult { reviews: data?.wine?.reviews ?? [], distribution: data?.wine?.ratingDistribution ?? [], myReview: data?.myReview ?? null, + hasData: data !== undefined, loading, error, // A failed retry already shows up in `error`, so the rejected promise needs no handling. diff --git a/frontend/src/features/wine/components/StudentReviews.module.css b/frontend/src/features/wine/components/StudentReviews.module.css index 9cbcf4e..caa9a69 100644 --- a/frontend/src/features/wine/components/StudentReviews.module.css +++ b/frontend/src/features/wine/components/StudentReviews.module.css @@ -3,6 +3,8 @@ align-content: start; gap: var(--space-4); min-width: 0; + position: relative; + grid-auto-flow: row; } .summary { @@ -48,3 +50,25 @@ .subtitle { font-size: var(--text-lg); } + +.message { + font-weight: 600; +} + +/* + * Empty: keep it in the DOM and in the accessibility tree, but out of the grid, so it + * adds no gap. Absolutely positioned children are not grid items. `display: none` + * would take it out of the accessibility tree, and the message would never be announced. + */ +.message:empty { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip-path: inset(50%); +} + +.message { + font-weight: 600; + order: 1; +} \ No newline at end of file diff --git a/frontend/src/features/wine/components/StudentReviews.tsx b/frontend/src/features/wine/components/StudentReviews.tsx index f211ba2..d953c45 100644 --- a/frontend/src/features/wine/components/StudentReviews.tsx +++ b/frontend/src/features/wine/components/StudentReviews.tsx @@ -21,6 +21,7 @@ export function StudentReviews({ wineId, now }: StudentReviewsProps) { reviews, distribution, myReview, + hasData, loading, error, refetch, @@ -46,14 +47,28 @@ export function StudentReviews({ wineId, now }: StudentReviewsProps) {

          Vurderinger

          - {loading && } + {/* + * Outside every condition, so this element is never replaced. A live region that + * is removed and built again is not announced by screen readers. + */} +

          + {message} +

          - {error && } + {/* Only on the first load. A later fetch keeps the content, so focus stays where it is. */} + {loading && !hasData && } - {!loading && !error && ( + {/* A failed fetch after the first one keeps the content; the live region reports the failure. */} + {error && !hasData && ( + + )} + + {hasData && ( <> -

          Studentenes dommer

          +

          + Studentenes dommer +

          {formatRatingSummary(students ?? { average: null, count: 0 }, { @@ -71,13 +86,8 @@ export function StudentReviews({ wineId, now }: StudentReviewsProps) { onDelete={handleDelete} /> - {/* Always in the DOM, so screen readers announce the message when it changes. */} -

          - {message} -

          - {reviews.length > 0 ? ( -
            +
              {reviews.map((review) => (
            1. {' '}