|
| 1 | +import {render, screen} from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import type {IncidentDetail} from '../queries/incidentDetailQueryOptions'; |
| 5 | + |
| 6 | +import {ParticipantsList} from './ParticipantsList'; |
| 7 | + |
| 8 | +const mockParticipants: IncidentDetail['participants'] = [ |
| 9 | + {name: 'John Smith', avatar_url: null, role: 'Captain'}, |
| 10 | + {name: 'Jane Doe', avatar_url: null, role: 'Reporter'}, |
| 11 | + {name: 'Alice Brown', avatar_url: null, role: 'Participant'}, |
| 12 | + {name: 'Charlie Davis', avatar_url: null, role: 'Participant'}, |
| 13 | + {name: 'Eva Foster', avatar_url: null, role: 'Participant'}, |
| 14 | + {name: 'Frank Garcia', avatar_url: null, role: 'Participant'}, |
| 15 | + {name: 'Grace Lee', avatar_url: null, role: 'Participant'}, |
| 16 | + {name: 'Henry Wilson', avatar_url: null, role: 'Participant'}, |
| 17 | +]; |
| 18 | + |
| 19 | +describe('ParticipantsList', () => { |
| 20 | + it('returns null when participants array is empty', () => { |
| 21 | + const {container} = render(<ParticipantsList participants={[]} />); |
| 22 | + expect(container.firstChild).toBeNull(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('renders all participants when 5 or fewer', () => { |
| 26 | + const fiveParticipants = mockParticipants.slice(0, 5); |
| 27 | + render(<ParticipantsList participants={fiveParticipants} />); |
| 28 | + |
| 29 | + expect(screen.getByText('John Smith')).toBeInTheDocument(); |
| 30 | + expect(screen.getByText('Eva Foster')).toBeInTheDocument(); |
| 31 | + expect(screen.queryByRole('button')).not.toBeInTheDocument(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('shows only first 5 participants when more than 5 exist', () => { |
| 35 | + render(<ParticipantsList participants={mockParticipants} />); |
| 36 | + |
| 37 | + expect(screen.getByText('John Smith')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText('Eva Foster')).toBeInTheDocument(); |
| 39 | + expect(screen.queryByText('Frank Garcia')).not.toBeInTheDocument(); |
| 40 | + expect(screen.queryByText('Grace Lee')).not.toBeInTheDocument(); |
| 41 | + expect(screen.queryByText('Henry Wilson')).not.toBeInTheDocument(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('shows "Show X more participants" button when more than 5 participants', () => { |
| 45 | + render(<ParticipantsList participants={mockParticipants} />); |
| 46 | + |
| 47 | + expect( |
| 48 | + screen.getByRole('button', {name: 'Show 3 more participants'}) |
| 49 | + ).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('expands to show all participants when button is clicked', async () => { |
| 53 | + const user = userEvent.setup(); |
| 54 | + render(<ParticipantsList participants={mockParticipants} />); |
| 55 | + |
| 56 | + await user.click(screen.getByRole('button', {name: 'Show 3 more participants'})); |
| 57 | + |
| 58 | + expect(screen.getByText('Frank Garcia')).toBeInTheDocument(); |
| 59 | + expect(screen.getByText('Grace Lee')).toBeInTheDocument(); |
| 60 | + expect(screen.getByText('Henry Wilson')).toBeInTheDocument(); |
| 61 | + expect( |
| 62 | + screen.getByRole('button', {name: 'Show fewer participants'}) |
| 63 | + ).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('collapses back to 5 participants when "Show fewer" is clicked', async () => { |
| 67 | + const user = userEvent.setup(); |
| 68 | + render(<ParticipantsList participants={mockParticipants} />); |
| 69 | + |
| 70 | + await user.click(screen.getByRole('button', {name: 'Show 3 more participants'})); |
| 71 | + await user.click(screen.getByRole('button', {name: 'Show fewer participants'})); |
| 72 | + |
| 73 | + expect(screen.queryByText('Frank Garcia')).not.toBeInTheDocument(); |
| 74 | + expect( |
| 75 | + screen.getByRole('button', {name: 'Show 3 more participants'}) |
| 76 | + ).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('displays participant roles for non-Participant roles', () => { |
| 80 | + render(<ParticipantsList participants={mockParticipants.slice(0, 3)} />); |
| 81 | + |
| 82 | + expect(screen.getByText('Captain')).toBeInTheDocument(); |
| 83 | + expect(screen.getByText('Reporter')).toBeInTheDocument(); |
| 84 | + expect(screen.queryByText('Participant')).not.toBeInTheDocument(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments