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 760a067..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 { @@ -10,10 +12,6 @@ font-weight: 600; } -.message:empty { - display: none; -} - .list { display: grid; gap: var(--space-3); @@ -52,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.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