|
| 1 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import CopyRepoButton from './CopyRepoButton'; |
| 5 | + |
| 6 | +vi.mock('lucide-react', () => ({ |
| 7 | + Copy: () => <svg aria-hidden="true" data-testid="copy-icon" />, |
| 8 | +})); |
| 9 | + |
| 10 | +Object.assign(navigator, { |
| 11 | + clipboard: { |
| 12 | + writeText: vi.fn().mockResolvedValue(undefined), |
| 13 | + }, |
| 14 | +}); |
| 15 | + |
| 16 | +describe('CopyRepoButton accessibility behavior', () => { |
| 17 | + it('renders as an accessible button with visible label', () => { |
| 18 | + render(<CopyRepoButton />); |
| 19 | + |
| 20 | + expect(screen.getByRole('button', { name: /copy url/i })).toBeDefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('keeps decorative copy icon hidden from assistive technology', () => { |
| 24 | + render(<CopyRepoButton />); |
| 25 | + |
| 26 | + expect(screen.getByTestId('copy-icon').getAttribute('aria-hidden')).toBe('true'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('is keyboard focusable through the native button element', () => { |
| 30 | + render(<CopyRepoButton />); |
| 31 | + |
| 32 | + const button = screen.getByRole('button', { name: /copy url/i }); |
| 33 | + button.focus(); |
| 34 | + |
| 35 | + expect(document.activeElement).toBe(button); |
| 36 | + }); |
| 37 | + |
| 38 | + it('updates accessible button name after successful copy action', async () => { |
| 39 | + render(<CopyRepoButton />); |
| 40 | + |
| 41 | + const button = screen.getByRole('button', { name: /copy url/i }); |
| 42 | + fireEvent.click(button); |
| 43 | + |
| 44 | + expect(await screen.findByRole('button', { name: /copied/i })).toBeDefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('preserves semantic button role after interaction', async () => { |
| 48 | + render(<CopyRepoButton />); |
| 49 | + |
| 50 | + fireEvent.click(screen.getByRole('button', { name: /copy url/i })); |
| 51 | + |
| 52 | + expect(await screen.findByRole('button', { name: /copied/i })).toBeDefined(); |
| 53 | + expect(screen.getAllByRole('button')).toHaveLength(1); |
| 54 | + }); |
| 55 | +}); |
0 commit comments