diff --git a/client/src/components/ReviewForm.css b/client/src/components/ReviewForm.css index 6fa4480..1367d6e 100644 --- a/client/src/components/ReviewForm.css +++ b/client/src/components/ReviewForm.css @@ -1,13 +1,19 @@ .review-form { - border-top: 1px solid var(--border); + border: 1px solid var(--border); + border-radius: 4px; + background: var(--surface); + min-width: 0; + overflow-wrap: anywhere; margin-top: 1.25rem; - padding-top: 1.25rem; + padding: clamp(0.75rem, 2.5vw, 1.5rem); } .review-form h3 { font-size: 1rem; + margin: 0 0 1rem; } .review-form fieldset { border: 0; + min-width: 0; margin: 0 0 1rem; padding: 0; } @@ -20,14 +26,35 @@ .review-form__ratings { display: flex; flex-wrap: wrap; - gap: 0.4rem 0.75rem; + gap: 0.375rem; } .review-form__ratings label { - align-items: center; + position: relative; cursor: pointer; - display: flex; - gap: 0.25rem; - padding: 0.35rem 0.5rem; + display: grid; + place-items: center; + min-width: 2.75rem; + min-height: 2.75rem; + padding: 0.25rem; + color: var(--border-strong); + border-radius: 3px; +} +.review-form__ratings .review-form__star--filled { + color: var(--accent); +} +.review-form__ratings label:has(input:focus-visible) { + outline: 3px solid var(--focus); + outline-offset: 3px; +} +.review-form__ratings svg { + display: block; + width: 1.5rem; + height: 1.5rem; +} +.review-form__rating-hint { + color: var(--muted); + font-size: 0.875rem; + margin: 0.5rem 0 0; } .review-form textarea { background: var(--surface); @@ -39,6 +66,8 @@ padding: 0.65rem 0.75rem; resize: vertical; width: 100%; + min-width: 0; + max-width: 100%; } .review-form__actions { display: flex; @@ -46,6 +75,18 @@ gap: 0.6rem; margin-top: 1rem; } +.review-form__actions button { + max-width: 100%; + min-height: 2.75rem; + white-space: normal; + overflow-wrap: anywhere; +} +.review-form__actions .primary-button[aria-disabled='true'] { + background: var(--surface-raised); + border-color: var(--border-strong); + color: var(--muted); + cursor: default; +} .secondary-button { background: transparent; border: 1px solid var(--accent); diff --git a/client/src/components/ReviewForm.test.tsx b/client/src/components/ReviewForm.test.tsx index 5703111..4a1a006 100644 --- a/client/src/components/ReviewForm.test.tsx +++ b/client/src/components/ReviewForm.test.tsx @@ -1,8 +1,11 @@ import { MockedProvider } from '@apollo/client/testing/react'; -import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; import { expect, it, vi } from 'vitest'; -import { UPSERT_REVIEW } from '../api/reviews.js'; +import userEvent from '@testing-library/user-event'; +import axe from 'axe-core'; + +import { DELETE_REVIEW, UPSERT_REVIEW } from '../api/reviews.js'; import ReviewForm from './ReviewForm.js'; it('requires an accessible 1 to 5 star choice before it saves', () => { @@ -24,20 +27,124 @@ it('requires an accessible 1 to 5 star choice before it saves', () => { ); }); -it('saves the selected rating and optional comment', async () => { - const onChanged = vi.fn(() => Promise.resolve(undefined)); - render( +it.each([ + { review: null, comment: '' }, + { review: null, comment: 'A classic.' }, + { review: { id: '1', rating: 2, comment: 'Before' }, comment: 'A classic.' }, +])('publishes or updates a review with %j', async ({ review, comment }) => { + let finishRefresh!: () => void; + const refresh = new Promise((resolve) => { + finishRefresh = resolve; + }); + const onChanged = vi.fn(() => refresh); + const result = vi.fn(() => ({ + data: { + upsertReview: { + review: { id: '1', rating: 5, comment }, + album: { id: '1' }, + }, + }, + })); + const { rerender } = render( + + , + ); + + if (review) { + expect(screen.getByRole('radio', { name: '2 stars' })).toBeChecked(); + expect(screen.getByRole('textbox')).toHaveValue('Before'); + } + fireEvent.click(screen.getByRole('radio', { name: '5 stars' })); + fireEvent.change(screen.getByRole('textbox', { name: /comment/i }), { + target: { value: comment }, + }); + const button = screen.getByRole('button', { + name: review ? 'Save changes' : 'Publish review', + }); + const user = userEvent.setup(); + button.focus(); + await user.keyboard('{Enter}'); + + await waitFor(() => expect(onChanged).toHaveBeenCalledOnce()); + expect(button).toHaveFocus(); + expect(button).not.toBeDisabled(); + expect(button).toHaveAttribute('aria-disabled', 'true'); + expect(screen.getByRole('textbox')).toBeDisabled(); + await user.keyboard(' '); + fireEvent.submit(button.closest('form')!); + if (review) fireEvent.click(screen.getByRole('button', { name: 'Delete review' })); + if (review) { + rerender( + + + , + ); + } + await act(async () => finishRefresh()); + const status = await screen.findByRole('status'); + expect(status).toHaveTextContent( + review ? 'Your review was updated.' : 'Your review was published.', + ); + expect(status).toHaveFocus(); + expect(result).toHaveBeenCalledOnce(); + expect(onChanged).toHaveBeenCalledOnce(); + expect(button).toHaveAttribute('aria-disabled', review ? 'true' : 'false'); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); +}); + +it('uses native radio keyboard navigation and exposes the selected rating', async () => { + const { container } = render( + +
+ +
+
, + ); + const user = userEvent.setup(); + await user.tab(); + expect(screen.getByRole('radio', { name: '1 star' })).toHaveFocus(); + await user.keyboard('{ArrowRight}{ArrowRight}{ArrowLeft}'); + const two = screen.getByRole('radio', { name: '2 stars' }); + expect(two).toBeChecked(); + expect(two).toHaveFocus(); + expect(screen.getByRole('group', { name: 'Rating' })).toHaveAccessibleDescription( + '2 out of 5 stars', + ); + await user.tab(); + expect(screen.getByRole('textbox')).toHaveFocus(); + const audit = await axe.run(container, { + rules: { 'color-contrast': { enabled: false } }, + }); + expect(audit.violations.map((v) => v.id)).toEqual([]); +}); + +it('blocks unchanged saves and updates availability when edits are reverted', async () => { + const requested = vi.fn(() => true); + render( + { }, ]} > - + {})} + /> , ); + const button = screen.getByRole('button', { name: 'Save changes' }); + const comment = screen.getByRole('textbox'); + button.focus(); + expect(button).toHaveAttribute('aria-disabled', 'true'); + expect(button).not.toBeDisabled(); + expect(screen.getByRole('button', { name: 'Delete review' })).toHaveAttribute( + 'aria-disabled', + 'false', + ); + await userEvent.setup().keyboard('{Enter}'); fireEvent.click(screen.getByRole('radio', { name: '5 stars' })); - fireEvent.change(screen.getByRole('textbox', { name: /comment/i }), { - target: { value: 'A classic.' }, - }); - fireEvent.click(screen.getByRole('button', { name: 'Publish review' })); + expect(button).toHaveAttribute('aria-disabled', 'false'); + fireEvent.click(screen.getByRole('radio', { name: '4 stars' })); + expect(button).toHaveAttribute('aria-disabled', 'true'); + fireEvent.change(comment, { target: { value: 'An edit' } }); + expect(button).toHaveAttribute('aria-disabled', 'false'); + fireEvent.change(comment, { target: { value: '' } }); + expect(button).toHaveAttribute('aria-disabled', 'true'); + fireEvent.submit(button.closest('form')!); + expect(requested).not.toHaveBeenCalled(); +}); +it('deletes an existing review and clears the form', async () => { + let finishRefresh!: () => void; + const refresh = new Promise((resolve) => { + finishRefresh = resolve; + }); + const onChanged = vi.fn(() => refresh); + const result = vi.fn(() => ({ + data: { deleteReview: { deletedReviewId: 'r1', album: { id: '1' } } }, + })); + render( + + + , + ); + const user = userEvent.setup(); + const button = screen.getByRole('button', { name: 'Delete review' }); + button.focus(); + await user.keyboard('{Enter}'); await waitFor(() => expect(onChanged).toHaveBeenCalledOnce()); + expect(button).toHaveFocus(); + expect(button).not.toBeDisabled(); + expect(button).toHaveAttribute('aria-disabled', 'true'); + await user.keyboard(' '); + fireEvent.submit(button.closest('form')!); + await act(async () => finishRefresh()); const status = await screen.findByRole('status'); - expect(status).toHaveTextContent('Your review was published.'); + expect(status).toHaveTextContent('Your review was deleted.'); expect(status).toHaveFocus(); + expect(onChanged).toHaveBeenCalledOnce(); + expect(result).toHaveBeenCalledOnce(); + expect(button).toHaveAttribute('aria-disabled', 'false'); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); + expect(screen.getByRole('textbox')).toHaveValue(''); + expect(screen.queryByRole('radio', { checked: true })).not.toBeInTheDocument(); }); + +it.each(['publish', 'update', 'delete'])( + 'keeps keyboard focus after a %s error and allows retry', + async (action) => { + const onChanged = vi.fn(async () => {}); + const request = + action !== 'delete' + ? { + query: UPSERT_REVIEW, + variables: { input: { albumId: '1', rating: 4, comment: 'My review' } }, + } + : { query: DELETE_REVIEW, variables: { id: 'r1' } }; + render( + + + , + ); + fireEvent.click(screen.getByRole('radio', { name: '4 stars' })); + fireEvent.change(screen.getByRole('textbox'), { target: { value: 'My review' } }); + const button = screen.getByRole('button', { + name: + action === 'publish' + ? 'Publish review' + : action === 'update' + ? 'Save changes' + : 'Delete review', + }); + const user = userEvent.setup(); + button.focus(); + await user.keyboard('{Enter}'); + expect(await screen.findByRole('alert')).toHaveTextContent('Offline'); + expect(button).toHaveFocus(); + expect(button).not.toBeDisabled(); + expect(button).toHaveAttribute('aria-disabled', 'false'); + expect(screen.getByRole('textbox')).toHaveValue('My review'); + expect(screen.getByRole('radio', { name: '4 stars' })).toBeChecked(); + expect(onChanged).not.toHaveBeenCalled(); + await user.keyboard(' '); + const status = await screen.findByRole('status'); + expect(status).toHaveTextContent( + action === 'delete' + ? 'Your review was deleted.' + : action === 'publish' + ? 'Your review was published.' + : 'Your review was updated.', + ); + expect(status).toHaveFocus(); + expect(onChanged).toHaveBeenCalledOnce(); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); + }, +); diff --git a/client/src/components/ReviewForm.tsx b/client/src/components/ReviewForm.tsx index 80ca9c2..902160b 100644 --- a/client/src/components/ReviewForm.tsx +++ b/client/src/components/ReviewForm.tsx @@ -19,10 +19,15 @@ export default function ReviewForm({ albumId, review, onChanged }: Props) { const [comment, setComment] = useState(review?.comment ?? ''); const [error, setError] = useState(null); const [status, setStatus] = useState(null); + const [busy, setBusy] = useState(false); const statusRef = useRef(null); - const [saveReview, saveState] = useMutation(UPSERT_REVIEW); - const [deleteReview, deleteState] = useMutation(DELETE_REVIEW); - const busy = saveState.loading || deleteState.loading; + // Block repeated activations before React renders the busy state. + const mutationInFlight = useRef(false); + const [saveReview] = useMutation(UPSERT_REVIEW); + const [deleteReview] = useMutation(DELETE_REVIEW); + // The server trims outer whitespace before storing a comment. + const unchanged = + review !== null && rating === review.rating && comment.trim() === review.comment; useEffect(() => { if (status) statusRef.current?.focus(); @@ -34,11 +39,14 @@ export default function ReviewForm({ albumId, review, onChanged }: Props) { async function submit(event: React.FormEvent) { event.preventDefault(); + if (mutationInFlight.current || unchanged) return; if (rating < 1 || rating > 5) { setError('Choose a rating from 1 to 5 stars.'); return; } + mutationInFlight.current = true; + setBusy(true); setError(null); setStatus(null); try { @@ -49,11 +57,16 @@ export default function ReviewForm({ albumId, review, onChanged }: Props) { setError( caught instanceof Error ? caught.message : 'Your review could not be saved.', ); + } finally { + mutationInFlight.current = false; + setBusy(false); } } async function remove() { - if (!review) return; + if (!review || mutationInFlight.current) return; + mutationInFlight.current = true; + setBusy(true); setError(null); setStatus(null); try { @@ -66,28 +79,56 @@ export default function ReviewForm({ albumId, review, onChanged }: Props) { setError( caught instanceof Error ? caught.message : 'Your review could not be deleted.', ); + } finally { + mutationInFlight.current = false; + setBusy(false); } } return ( -
void submit(event)}> + void submit(event)} + >

{review ? 'Update your review' : 'Write a review'}

-
+
Rating
{[1, 2, 3, 4, 5].map((value) => ( -
+

+ {rating ? `${rating} out of 5 stars` : 'Select a rating'} +