Skip to content

Commit 2d31efb

Browse files
authored
test(copyrepobutton): add accessibility coverage (JhaSourav07#3013)
1 parent 889cb41 commit 2d31efb

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)