// FE-W4STAR-001 to FE-W4STAR-014 import { describe, it, expect, vi, beforeEach } from 'vitest' import type { PlaceRatingVote } from '@trek/shared' import { render, screen, fireEvent, within } from '../../../tests/helpers/render' import { useAuthStore } from '../../store/authStore' import PlaceRating, { Stars } from './StarRating' const VOTES: PlaceRatingVote[] = [ { user_id: 1, username: 'ada', avatar: 'ada.png', rating: 5 }, { user_id: 2, username: 'bob', avatar: null, rating: 3 }, ] beforeEach(() => { useAuthStore.setState({ user: { id: 1, username: 'ada' } as never }) }) describe('Stars', () => { it('FE-W4STAR-001: renders five stars and is not a radiogroup in display mode', () => { const { container } = render() expect(container.querySelectorAll('svg')).toHaveLength(3 + 5) expect(container.querySelector('[role="radiogroup"]')).toBeNull() expect(container.querySelectorAll('button')).toHaveLength(0) }) it('FE-W4STAR-002: clips the overlay for a fractional average', () => { const { container } = render() const overlays = Array.from(container.querySelectorAll('span > span')) as HTMLElement[] const widths = overlays.map(o => o.style.width).filter(Boolean) expect(widths).toHaveLength(4) expect(widths.slice(0, 3)).toEqual(['100%', '100%', '100%']) expect(Math.round(parseFloat(widths[3]))).toBe(40) }) it('FE-W4STAR-003: exposes a radiogroup with five radios when interactive', () => { render( {}} ariaLabel="Your rating" />) expect(screen.getByRole('radiogroup', { name: 'Your rating' })).toBeInTheDocument() expect(screen.getAllByRole('radio')).toHaveLength(5) }) it('FE-W4STAR-004: marks the own vote as checked', () => { render( {}} myRating={4} />) expect(screen.getByRole('radio', { name: '4' })).toHaveAttribute('aria-checked', 'true') expect(screen.getByRole('radio', { name: '2' })).toHaveAttribute('aria-checked', 'false') }) it('FE-W4STAR-005: casting a new vote reports the star index', () => { const onRate = vi.fn() render() fireEvent.click(screen.getByRole('radio', { name: '4' })) expect(onRate).toHaveBeenCalledWith(4) }) it('FE-W4STAR-006: clicking the own vote clears it', () => { const onRate = vi.fn() render() fireEvent.click(screen.getByRole('radio', { name: '3' })) expect(onRate).toHaveBeenCalledWith(null) }) it('FE-W4STAR-007: hovering previews the hovered value and leaving restores it', () => { const { container } = render( {}} />) const filled = () => Array.from(container.querySelectorAll('span > span')).filter(o => (o as HTMLElement).style.width).length expect(filled()).toBe(1) fireEvent.pointerEnter(screen.getByRole('radio', { name: '4' }), { pointerType: 'mouse' }) expect(filled()).toBe(4) fireEvent.pointerLeave(container.querySelector('[role="radiogroup"]')!, { pointerType: 'mouse' }) expect(filled()).toBe(1) }) it('FE-W4STAR-016: a finger previews nothing, so a tap cannot leave the stars filled', () => { // A tap fires an enter with no leave after it: the preview used to stick, // and an unrated place showed five full stars beside "Not rated yet". const { container } = render( {}} />) const filled = () => Array.from(container.querySelectorAll('span > span')).filter(o => (o as HTMLElement).style.width).length fireEvent.pointerEnter(screen.getByRole('radio', { name: '5' }), { pointerType: 'touch' }) expect(filled()).toBe(0) }) it('FE-W4STAR-008: focus previews and blur clears the preview', () => { const { container } = render( {}} />) const filled = () => Array.from(container.querySelectorAll('span > span')).filter(o => (o as HTMLElement).style.width).length fireEvent.focus(screen.getByRole('radio', { name: '5' })) expect(filled()).toBe(5) fireEvent.blur(screen.getByRole('radio', { name: '5' })) expect(filled()).toBe(0) }) }) describe('PlaceRating', () => { it('FE-W4STAR-009: shows the rounded average and the vote count', () => { render() // The average is rendered through toLocaleString, so build the expectation the same way. expect(screen.getByText('(2)').parentElement).toHaveTextContent(`${(4.3).toLocaleString()} (2)`) }) it('FE-W4STAR-010: shows the not-rated hint when there is no average', () => { render() expect(screen.getByText(/not rated/i)).toBeInTheDocument() }) it('FE-W4STAR-011: renders the voter avatar strip with image and initial fallback', () => { const { container } = render() expect(container.querySelector('img')).toHaveAttribute('src', '/uploads/avatars/ada.png') expect(screen.getByText('B')).toBeInTheDocument() }) it('FE-W4STAR-012: hides the avatar strip in compact mode', () => { const { container } = render() expect(container.querySelector('img')).toBeNull() }) it('FE-W4STAR-013: hovering opens a voter tooltip that marks the current user', () => { const { container } = render() fireEvent.mouseEnter(container.firstElementChild!) const tip = screen.getByRole('tooltip') expect(within(tip).getByText('ada (you)')).toBeInTheDocument() expect(within(tip).getByText('bob')).toBeInTheDocument() fireEvent.mouseLeave(container.firstElementChild!) expect(screen.queryByRole('tooltip')).toBeNull() }) it('FE-W4STAR-014: does not open a tooltip when nobody voted', () => { const { container } = render() fireEvent.mouseEnter(container.firstElementChild!) expect(screen.queryByRole('tooltip')).toBeNull() }) it('FE-W4STAR-015: pre-selects the signed-in user own vote in the stars', () => { useAuthStore.setState({ user: { id: 2, username: 'bob' } as never }) render( {}} />) expect(screen.getByRole('radio', { name: '3' })).toHaveAttribute('aria-checked', 'true') }) })