// @vitest-environment jsdom /** * The shared reaction row (COMM-2): the pills in the order first chosen, the * viewer's lit, who reacted, toggling, and no row when nobody reacted. */ import { afterEach, describe, expect, it, vi } from 'vitest' import { cleanup, fireEvent, render, screen } from '@testing-library/react' import { ReactionRow, whoReacted } from './ReactionRow' const OPTIONS = [ { emoji: 'πŸ‘', label: 'Makes sense' }, { emoji: 'πŸ™', label: 'Thank you' }, ] const NAMES: Record = { a: 'Person one', b: 'Person two', me: 'Me' } const nameOf = (id: string) => NAMES[id] ?? 'Someone' afterEach(() => cleanup()) describe('the pills', () => { it('draws each emoji with its count in the order first chosen, the viewer’s pressed', () => { render( {}} />, ) const pills = screen.getAllByRole('button').filter((b) => b.getAttribute('aria-label') !== 'Add a reaction') expect(pills.map((p) => p.getAttribute('aria-label'))).toEqual(['Thank you, 1', 'Makes sense, 2']) expect(pills.map((p) => p.getAttribute('aria-pressed'))).toEqual(['false', 'true']) }) it('a click toggles your reaction; read-only, the pills are disabled and nothing adds one', () => { const onToggle = vi.fn() const { rerender } = render() fireEvent.click(screen.getByRole('button', { name: 'Makes sense, 1' })) expect(onToggle).toHaveBeenCalledWith('πŸ‘') rerender() expect((screen.getByRole('button', { name: 'Makes sense, 1' }) as HTMLButtonElement).disabled).toBe(true) expect(screen.queryByRole('button', { name: 'Add a reaction' })).toBeNull() }) it('nobody reacted: no row at all', () => { const { container } = render( {}} />) expect(container.innerHTML).toBe('') }) }) describe('who reacted', () => { it('names the others, then you, with what the emoji means', () => { expect(whoReacted({ emoji: 'πŸ‘', by: ['a', 'me', 'b'] }, nameOf, OPTIONS, 'me')).toBe('Person one, Person two and you reacted with Makes sense') expect(whoReacted({ emoji: 'πŸ¦†', by: ['me'] }, nameOf, OPTIONS, 'me')).toBe('You reacted with πŸ¦†') }) it('says nothing when nobody holds the emoji', () => { expect(whoReacted({ emoji: 'πŸ‘', by: [] }, nameOf, OPTIONS, 'me')).toBe('') }) it('counts the names past eight', () => { const many = Array.from({ length: 11 }, (_, i) => `p${i}`) expect(whoReacted({ emoji: 'πŸ‘', by: many }, () => 'P', OPTIONS)).toBe('P, P, P, P, P, P, P, P and 3 more reacted with Makes sense') }) })