|
1 | | -import { render, screen } from '@testing-library/react'; |
2 | | -import { describe, it, expect, vi } from 'vitest'; |
| 1 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, it, expect, vi } from 'vitest'; |
3 | 3 | import CopyRepoButton from './CopyRepoButton'; |
4 | 4 |
|
5 | 5 | vi.mock('lucide-react', () => ({ |
6 | 6 | Copy: () => <svg data-testid="copy-icon" />, |
7 | 7 | })); |
8 | 8 |
|
| 9 | +beforeEach(() => { |
| 10 | + vi.clearAllMocks(); |
| 11 | + Object.assign(navigator, { |
| 12 | + clipboard: { |
| 13 | + writeText: vi.fn().mockResolvedValue(undefined), |
| 14 | + }, |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
9 | 18 | describe('CopyRepoButton Massive Scaling', () => { |
10 | 19 | it('renders 100 buttons without crashing', () => { |
11 | 20 | render( |
@@ -63,4 +72,35 @@ describe('CopyRepoButton Massive Scaling', () => { |
63 | 72 | } |
64 | 73 | }).not.toThrow(); |
65 | 74 | }); |
| 75 | + |
| 76 | + it('shows copied state after a successful clipboard write', async () => { |
| 77 | + render(<CopyRepoButton />); |
| 78 | + |
| 79 | + fireEvent.click(screen.getByRole('button', { name: /copy url/i })); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith( |
| 83 | + 'https://github.com/JhaSourav07/commitpulse' |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + expect(screen.getByText('Copied!')).toBeDefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('shows an error state when clipboard write fails', async () => { |
| 91 | + vi.mocked(navigator.clipboard.writeText).mockRejectedValueOnce(new Error('Permission denied')); |
| 92 | + |
| 93 | + render(<CopyRepoButton />); |
| 94 | + |
| 95 | + fireEvent.click(screen.getByRole('button', { name: /copy url/i })); |
| 96 | + |
| 97 | + await waitFor(() => { |
| 98 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith( |
| 99 | + 'https://github.com/JhaSourav07/commitpulse' |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + expect(screen.queryByText('Copied!')).toBeNull(); |
| 104 | + expect(screen.getByText('Copy failed')).toBeDefined(); |
| 105 | + }); |
66 | 106 | }); |
0 commit comments